Skip to content
Cyrille DUPUYDAUBY edited this page Jan 26, 2020 · 5 revisions

The following checks can be used on generic dictionaries.

What is considered as a Dictionary?

For compatibility as well as convenience, NFluent has a broad acceptance for dictionaries:

  • IDictionary implementation (non generic).
  • IReadOnly<,> implementations.
  • IDictionary<,> implementations.
  • IEnumerable<,> implementations (as of V2.7).
  • Hashtable.

Contents

ContainsKey(expectedKey)

Checks that the sut is a dictionary containing expectedKey as a key; fails otherwise.

var dico = new Dictionary<string, string>();
dico["myKey"] = "myValue";
// this check succeeds
Check.That(dico).ContainsKey("myKey");
// this check fails
Check.That(dico).ContainsKey("otherKey");

ContainsPair(expectedKey, expectedValue)

Checks that the sut is a dictionary containing expectedKey as a key referring to expectedValue; fails otherwise.

var dico = new Dictionary<string, string>();
dico["myKey"] = "myValue";
// this check succeeds
Check.That(dico).ContainsPair("myKey", "myValue");
// those checks fail
Check.That(dico).ContainsPair("otherKey", "otherValue");
Check.That(dico).ContainsPair("myKey", "otherValue");
Check.That(dico).ContainsPair("otherKey", "myValue");

ContainsValue(expectedValue)

Checks that the sut is a dictionary containing expectedValue as a value; fails otherwise.

var dico = new Dictionary<string, string>();
dico["myKey"] = "myValue";
// this check succeeds
Check.That(dico).ContainsValue("myValue");
// this check fails
Check.That(dico).ContainsValue("otherValue");
Clone this wiki locally