For the complete documentation index, see llms.txt. This page is also available as Markdown.

ContainKeyAndValue

ShouldContainKeyAndValue

var websters = new Dictionary<string, string> { { "Cromulent", "I never heard the word before moving to Springfield." } };
websters.ShouldContainKeyAndValue("Cromulent", "Fine, acceptable.");

snippet source | anchor

Exception

websters
    should contain key
"Cromulent"
    with value
"Fine, acceptable."
    but value was
"I never heard the word before moving to Springfield."

ShouldNotContainKeyAndValue

var websters = new Dictionary<string, string> { { "Chazzwazzers", "What Australians would have called a bull frog." } };
websters.ShouldNotContainValueForKey("Chazzwazzers", "What Australians would have called a bull frog.");

snippet source | anchor

Exception

Value comparison

The value is compared the same way ShouldBe compares it, not with object.Equals. Collection values are compared element-wise, numeric values honour ShouldlyConfiguration.DefaultFloatingPointTolerance, and IEquatable<T>/IComparable<T> are preferred over Equals:

This is not a member-wise comparison. A value that is neither a collection nor implements IEquatable<T>, IComparable<T>, or IComparable falls through to object.Equals, so a reference type that doesn't override it is still compared by reference. For member-wise comparison of a whole object graph, use ShouldBeEquivalentTo.

One caveat, shared with ShouldBe: the elements of a collection value are compared without their static element type, so an element type that implements IEquatable<T> but doesn't override Equals (which CA1067 flags) is compared by reference. ShouldBe on the value itself knows the element type and does honour it; it loses the same way one level deeper, on nested collections.

The key, by contrast, is always looked up with the dictionary's own key comparer.

Last updated