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?

  1. Passing a null Term should throw an exception
  2. Passing a null ContentItem should throw an exception
  3. 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.

  1. Passing a null Term should throw an exception
  2. Passing a Term with a negative VocabularyId should throw an exception
  3. Passing a Term with a null or empty name should throw an exception
  4. If a valid simple Term is passed to the method then the AddSimpleTerm method of _DataService should be called
  5. If a valid simple Term is passed to the method then the TermId property should be set
  6. If a valid simple Term is passed to the method then the TermId should be returned
  7. If a valid hierarchical Term is passed to the method then the AddHeirarchcialTerm method of _DataService should be called
  8. If a valid hierarchical Term is passed to the method then the TermId property should be set
  9. If a valid hierarchical Term is passed to the method then the TermId should be returned
  10. 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.


Posted in: Testing  Tags: , , ,
Comments are closed

 Search Blog

 Calendar

«  February 2012  »
MoTuWeThFrSaSu
303112345
6789101112
13141516171819
20212223242526
2728291234
567891011
View posts in large calendar

 Tags

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2012 Thoughts from the Wet Coast