Skip to content

Dictionaries

ReadOnlyDictionaryAssertions<TDictionary, TKey, TValue> is available for IReadOnlyDictionary<TKey, TValue> and Dictionary<TKey, TValue> via .Should().

Key Presence

var dict = new Dictionary<string, int>
{
    ["one"] = 1,
    ["two"] = 2
};

dict.Should().ContainKey("one");
dict.Should().NotContainKey("three");

See ContainKey and NotContainKey.

Chaining

Chainable methods return a ReadOnlyDictionaryAssertionsChain<TDictionary, TKey, TValue>:

dict.Should()
    .ContainKey("one")
    .And.ContainKey("two")
    .And.NotContainKey("three");

All object-level assertions are also available:

Dictionary<string, int>? maybeNull = GetDict();
maybeNull.Should().NotBeNull().And.ContainKey("key");