Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Unit Test an Implementation of IDictionary

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...

like image 392
David Williams Avatar asked Mar 16 '09 20:03

David Williams


2 Answers

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());
}
like image 69
user7116 Avatar answered Nov 05 '22 21:11

user7116


Test all your interface points, but beware the temptation to test the framework.

like image 29
Chris Ballance Avatar answered Nov 05 '22 19:11

Chris Ballance