What is the inverse of Contains.Key() for a Dictionary?

516 views
Skip to first unread message

Chris Oldwood

unread,
Aug 17, 2016, 11:11:03 AM8/17/16
to NUnit-Discuss
I have one test that checks for the presence of a key like this:

{
  var dictionary = new Dictionary<string, object>();

  dictionary.Add("test", new object());

  Assert.That(dictionary, Contains.Key("test"));
}

However I cannot work out what the correct fu is for saying "does not contain the key X". After I get past "Does.Not" there only appears to be constraints for checking generic collections, not dictionaries.

I can of course write:

Assert.That(dictionary.ContainsKey("test"), Is.False);

but what I expected to write was something like this:

Assert.That(dictionary, Does.Not.ContainKey("test"));

So, what am I missing here? :o)

Cheers,

Chris Oldwood

Charlie Poole

unread,
Aug 17, 2016, 12:07:43 PM8/17/16
to NUnit-Discuss
Hi Chris,

You are correct that this is missing per se. However you can use

Assert.That(dictionary.Keys, Does.Not.Contains("test"));

Sorry about the bad grammar. :-)

The Contains class is pretty primitive. It could probably use "No" as
an addition and ConstraintExpression (the Type you get after the No)
should have Key in it or something similar.

If you can come up with a set of dictionary-related expressions you
would like to use, please file an issue and we'll see what we can do.

Charlie
> --
> You received this message because you are subscribed to the Google Groups
> "NUnit-Discuss" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to nunit-discus...@googlegroups.com.
> To post to this group, send email to nunit-...@googlegroups.com.
> Visit this group at https://groups.google.com/group/nunit-discuss.
> For more options, visit https://groups.google.com/d/optout.

Chris Oldwood

unread,
Aug 22, 2016, 8:45:49 AM8/22/16
to NUnit-Discuss
Thanks Charlie.

In the end I created a local extension method so I could write Does.Not.ContainKey("test") to see how it would work out:

public static class Extensions
{
    public static DictionaryContainsKeyConstraint ContainKey(this ConstraintExpression constraint, string value)
    {
        var keyConstraint = new DictionaryContainsKeyConstraint(value);

        constraint.Append(keyConstraint);

        return keyConstraint;
    }
}

This (ContainKey) is the only one that I've come across so far that's missing. Would you still like an Issue raised just for that? If I have time I could look at adding it myself directly (instead of the extension method approach) and send a PR, would you be interested in that route?

Chris Oldwood

Rob Prouse

unread,
Aug 22, 2016, 10:58:13 AM8/22/16
to NUnit-Discuss
Chris,

A pull request would be welcome. Please enter an issue first so we can track the pull request against it. If you need any help with the pull request, just let me know.

Rob

To unsubscribe from this group and stop receiving emails from it, send an email to nunit-discuss+unsubscribe@googlegroups.com.

To post to this group, send email to nunit-...@googlegroups.com.
Visit this group at https://groups.google.com/group/nunit-discuss.
For more options, visit https://groups.google.com/d/optout.



--

Rob Prouse

 

I welcome VSRE emails. Learn more at http://vsre.info/

Charlie Poole

unread,
Aug 22, 2016, 10:59:39 AM8/22/16
to NUnit-Discuss

A PR would be great. Thanks.


To unsubscribe from this group and stop receiving emails from it, send an email to nunit-discuss+unsubscribe@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages