Closed
Description
The DictionaryContainsKeyConstraint
allows an assertion to be written against the presence of a key in a dictionary:
Assert.That(dictionary, Contains.Key("test"));
However it's not possible to directly write the inverse - that a dictionary does not contain a key:
Assert.That(dictionary, Does.Not.ContainKey("test"));
The closest is probably to inspect the dictionary's Keys
property and assert on that:
Assert.That(dictionary.Keys, Does.Not.Contains("test"));
Aside from the lack of symmetry, the grammar of this approach is slightly jarring and a dedicated ContainKey
method would be preferable.
As an aside it's possible to solve this locally in the test code using an extension method:
public static class Extensions
{
public static DictionaryContainsKeyConstraint ContainKey(this ConstraintExpression constraint, string value)
{
var keyConstraint = new DictionaryContainsKeyConstraint(value);
constraint.Append(keyConstraint);
return keyConstraint;
}
}