views:

95

answers:

3

More than a question, per se, this is an attempt to compare notes with other people. I wrote a generic History class that emulates the functionality of a browser's history. I am trying to wrap my head around how far to go when writing unit tests for it. I am using NUnit. Please share your testing approaches below.

The full code for the History class is here (http://pastebin.com/ZGKK2V84).

+2  A: 

What specifically about it being a generic class makes different to test compared to a non-generic class in your eyes? I fail to see the problem :)

Just fill in T in the unit tests as some type, such as int so you have an instance of History<int>, and then test all of the interesting logic of the class as normal. E.g. Can't go backwards when no history exists, going forwards means you can move backwards and so on. The usual stuff :) Get a book or read some unit testing tutorials if you're not sure what to do.

Mark Simpson
I didn't intend to convey that there was a problem. As to your question: "What specifically about it being a generic class makes different to test compared to a non-generic class in your eyes?" This is what I'm trying to figure out... I suspect that there is no difference, but would like to compare notes with folks to see if I going in the right direction.
Jonas Gorauskas
I think Reed is right. The only difference between a generic class and a non-generic class in testing is what you encapsulate into it. He suggests a reference type and a value type. I concur.
dviljoen
Yup that's a fair comment :)
Mark Simpson
+7  A: 

Although this question is a little vague, in general, with a class like this, I would propose testing the following, at a minimum:

  • Test each method and property in your public API, verifying that each changes the internal collections appropriately when adding "real" data
  • Test adding invalid data to each method (should this accept null references, for example?), making sure you either get appropriate exceptions or the behavior you desire
  • Given it's a generic class, I would test this with both a value type and a reference type (unless you decide to add a constraint to one or the other)
Reed Copsey
A: 

Write a generic test class that mirrors the generic class. Then create another test class that instantiates that test class with a couple of different type arguments (a value type and a reference type of some sort are a good start).

munificent