Thoughts from the Wet Coast

The musings of an ASP.NET Developer from Canada's We(s)t Coast

DNN Development Tips:5 - What Tests should I Write

Last Modified: May 2 2017
Oct 17 2014

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 void AddTermToContent(Term term, ContentItem contentItem)
   2:  {
   3:      //Argument Contract
   4:      Requires.NotNull("term", term);
   5:      Requires.NotNull("contentItem", contentItem);
   6:   
   7:      _DataService.AddTermToContent(term, contentItem);
   8:  }

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 int AddTerm(Term term)
   2:  {
   3:      //Argument Contract
   4:      Requires.NotNull("term", term);
   5:      Requires.PropertyNotNegative("term", "VocabularyId", term.VocabularyId);
   6:      Requires.PropertyNotNullOrEmpty("term", "Name", term.Name);
   7:   
   8:      term.Name = HttpUtility.HtmlEncode(term.Name);
   9:              
  10:      if ((term.IsHeirarchical))
  11:      {
  12:          term.TermId = _DataService.AddHeirarchicalTerm(term, UserController.Instance.GetCurrentUserInfo().UserID);
  13:      }
  14:      else
  15:      {
  16:          term.TermId = _DataService.AddSimpleTerm(term, UserController.Instance.GetCurrentUserInfo().UserID);
  17:      }
  18:   
  19:      //Clear Cache
  20:      DataCache.RemoveCache(string.Format(_CacheKey, term.VocabularyId));
  21:   
  22:      return term.TermId;
  23:  }

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.

Disclaimer

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

Categories

Tags