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 wra开发者_C百科p 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).
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)
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.
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).
精彩评论