views:

94

answers:

3

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

+1  A: 

You should test everything you implement. If you have code that is called through the public interface, there should be a unit test that supports that. If you write the code, it is worth testing.

The exception might be simple properties ( Property {get; set;} )

Brian Genisio
Absolutely what? Are you saying he should write a specific test for each member of IDictionary? http://msdn.microsoft.com/en-us/library/system.collections.idictionary_members.aspx. That's tantamount to "[testing] the framework," as Chris says.
Chris
That is not at all "testing the framework". If you write code that implements the interface, you should write tests that tests your implementation. You are not, in any way, testing the framework in that case. You are testing YOUR code.
Brian Genisio
I'm still not clear on what you're saying. Tests that test your implementation, yes, but are you advocating writing individual tests for each member of the interface (e.g. "the entire interface")?
Chris
Yes. That is what unit tests are. They test the functionality of your code through the public interface. If you implement a method on the interface, it should have a test. Are you suggesting that you implement something on the interface (ie. write code) and not write tests?
Brian Genisio
I'm just trying to understand common unit testing practices better. :)
Chris
No problem. Testing the framework would be writing tests against Dictionary<TKey, TValue>, which there is no reason to do. In the OP's question, he wants to test MyDictionary : IDictionary. In that case you want to test the crap out of it to make sure it conforms to the interface.
Brian Genisio
+2  A: 

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

Chris Ballance
What do you consider "interface points?" The IDictionary members his code uses?
Chris
Anything he has written other than basic get/set/instantiate
Chris Ballance
+1  A: 

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.Contains(key), Is.EqualTo(a.ContainsKey(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());
}
sixlettervariables
Nice, thorough, specific answer.
Chris
This looks like we're testing the framework...
Chris Ballance
Not according to Brian (see comments).
Chris
(Chris B) He is not testing the framework. He is testing that the implementation of the interface (MyDictionary) is doing what he expects it to... that is, act like a Hashtable. This is more about testing the BEHAVIOR of the interface, but not testing Hashtable itself.
Brian Genisio
Just to be clear, what sixletter is suggesting is not the way that I was suggesting writing unit tests in my answer. I think that a sanity test like this is useful, but I believe that unit tests should be smaller, more concise tests of specific behaviors.
Brian Genisio
I would advocate both Brian's and mine. Brian's will pick up "duh" problems. Mine will (possibly) pick up more subtle issues. Brian's will require you to say what you expect. Mine assumes somebody else got it right, so "monkey see monkey do" == correct.
sixlettervariables