This article is cross-posted from my personal blog.
One of the biggest challenges in writing Unit Tests - at least when you write them after you have created the actual code, rather than in a Test Driven Development style, is determining what should be tested.
Lets look at a simple example in TermController.
Figure 1: A simple method which needs Unit Testing
|
1: Public Sub AddTermToContent(ByVal term As Term, ByVal contentItem As ContentItem) _
2: Implements ITermController.AddTermToContent
3: 'Argument Contract
4: Requires.NotNull("term", term)
5: Requires.NotNull("contentItem", contentItem)
6:
7: _DataService.AddTermToContent(term, contentItem)
8: End Sub
|
What do we need to test?
- Passing a null Term should throw an exception
- Passing a null ContentItem should throw an exception
- Passing valid (non-null) arguments should cause the AddTermToContent method of the _DataService to be executed
Are there any more – should we test that adding the term to the content item actually results in the association being made, or should we test what happens if this term is already associated with the content item?
The short answer is No – we don’t need to test these cases as that is the responsibility of the DataService, the Controller’s responsibility is to validate the data passed, possibly execute some business logic, and pass control to the data layer, and this is all covered by these three tests. We would test these scenarios when writing tests for the DataService itself.
Lets look at a more complex method from the same class.
Figure 2: A more complex method which needs Unit Testing
|
1: Public Function AddTerm(ByVal term As Term) As Integer _
2: Implements ITermController.AddTerm
3: 'Argument Contract
4: Requires.NotNull("term", term)
5: Requires.PropertyNotNegative("term", "VocabularyId", term.VocabularyId)
6: Requires.PropertyNotNullOrEmpty("term", "Name", term.Name)
7:
8: If (term.IsHeirarchical) Then
9: term.TermId = _DataService.AddHeirarchicalTerm(term, _
10: UserController.GetCurrentUserInfo.UserID)
11: Else
12: term.TermId = _DataService.AddSimpleTerm(term, _
13: UserController.GetCurrentUserInfo.UserID)
14: End If
15:
16: 'Clear Cache
17: DataCache.RemoveCache(String.Format(_CacheKey, term.VocabularyId))
18:
19: Return term.TermId
20: End Function
|
In this example we have a much longer list of tests.
- Passing a null Term should throw an exception
- Passing a Term with a negative VocabularyId should throw an exception
- Passing a Term with a null or empty name should throw an exception
- If a valid simple Term is passed to the method then the AddSimpleTerm method of _DataService should be called
- If a valid simple Term is passed to the method then the TermId property should be set
- If a valid simple Term is passed to the method then the TermId should be returned
- If a valid hierarchical Term is passed to the method then the AddHeirarchcialTerm method of _DataService should be called
- If a valid hierarchical Term is passed to the method then the TermId property should be set
- If a valid hierarchical Term is passed to the method then the TermId should be returned
- If a valid Term is passed to the method then the Term cache should be cleared.
Both of these examples demonstrate that you need to analyze the method and determine what are its responsibilities when you determine what Unit Tests to write for it.