简体   繁体   中英

How to Assert Dictionary in c# nunit?

Do you know how I can Assert two dictionaries of type

Dictionary&ltstring,MyClass>

in my Unit test project?

I tried with CollectionsAssert but it didn' work for me.I guess that it takes to simple Dictionaries as parameters(eg Dictionary).I guess that the problem for me comes from the second parameter of the dictionary.Do you know how I can assert those two dictionaries?

You don't say what you want to assert about the two dictionaries, but I am assuming it's that they are equal.

Beginning with NUnit 3.0, you can assert that two dictionaries are equal like this...

Assert.That(dictionary1, Is.EqualTo(dictionary2));

This assertion checks 1. That the dictionaries contain the same number of entries. 2. That the dictionaries contain exactly the same keys. 3. That the values for each key are equal.

The third step depends on the definition of equality included in your class, if it has one. If you have not overloaded equality for the class, then object equality will, of course, be used.

If this is not enough information, please edit your question, which is unfortunately pretty vague. Saying you want to "assert" two objects has no meaning unless you specify what you want to assert about them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM