When implementing something that implements IDictionary, what should I unit test?
It seems to be overkill to test the entire interface, but then what do I know? I have only been unit testing for a few days...
Every public member of your IDictionary should be tested. You should also set up some tests to ensure that your IDictionary behaves the same as some other concrete implementation of an IDictionary. In fact, you could structure most of your tests like that:
void Test_IDictionary_Add(IDictionary a, IDictionary b)
{
string key = "Key1", badKey = 87;
int value = 9, badValue = "Horse";
a.Add(key, value);
b.Add(key, value);
Assert.That(a.Count, Is.EqualTo(b.Count));
Assert.That(a.Contains(key), Is.EqualTo(b.Contains(key)));
Assert.That(a.ContainsKey(key), Is.EqualTo(b.ContainsKey(key)));
Assert.That(a.ContainsValue(value), Is.EqualTo(b.ContainsValue(value)));
Assert.That(a.Contains(badKey), Is.EqualTo(b.Contains(badKey)));
Assert.That(a.ContainsValue(badValue), Is.EqualTo(b.ContainsValue(badValue)));
// ... and so on and so forth
}
[Test]
void MyDictionary_Add()
{
Test_IDictionary_Add(new MyDictionary(), new Hashtable());
}
Test all your interface points, but beware the temptation to test the framework.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With