DNN Community Blog

The Community Blog is a personal opinion of community members and by no means the official standpoint of DNN Corp or DNN Platform. This is a place to express personal thoughts about DNNPlatform, the community and its ecosystem. Do you have useful information that you would like to share with the DNN Community in a featured article or blog? If so, please contact .

The use of the Community Blog is covered by our Community Blog Guidelines - please read before commenting or posting.


Adventures in Testing - 4 - What Tests Should I Write?

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?

  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.

Comments

There are currently no comments, be the first to post one.

Comment Form

Only registered users may post comments.

NewsArchives


Aderson Oliveira (15)
Alec Whittington (11)
Alex Shirley (10)
Andrew Nurse (30)
Andy Tryba (1)
Anthony Glenwright (5)
Antonio Chagoury (28)
Ash Prasad (32)
Ben Schmidt (1)
Benjamin Hermann (25)
Benoit Sarton (9)
Beth Firebaugh (12)
Bill Walker (36)
Bob Kruger (5)
Bogdan Litescu (1)
Brian Dukes (2)
Brice Snow (1)
Bruce Chapman (20)
Bryan Andrews (1)
cathal connolly (55)
Charles Nurse (163)
Chris Hammond (209)
Chris Paterra (55)
Clinton Patterson (41)
Cuong Dang (21)
Daniel Bartholomew (2)
Daniel Mettler (154)
Dave Buckner (2)
David Poindexter (4)
David Rodriguez (3)
Dennis Shiao (1)
Doug Howell (11)
Erik van Ballegoij (30)
Ernst Peter Tamminga (74)
Geoff Barlow (10)
George Alatrash (6)
Gifford Watkins (3)
Gilles Le Pigocher (3)
Ian Robinson (7)
Israel Martinez (17)
Jan Blomquist (2)
Jan Jonas (3)
Jaspreet Bhatia (1)
Jenni Merrifield (6)
Joe Brinkman (274)
John Mitchell (1)
Jon Henning (14)
Jonathan Sheely (4)
Jordan Coopersmith (1)
Joseph Craig (2)
Kan Ma (1)
Keivan Beigi (3)
Ken Grierson (10)
Kevin Schreiner (6)
Leigh Pointer (31)
Lorraine Young (60)
Malik Khan (1)
Matthias Schlomann (15)
Mauricio Márquez (5)
Michael Doxsey (7)
Michael Tobisch (3)
Michael Washington (202)
Miguel Gatmaytan (3)
Mike Horton (19)
Mitchel Sellers (28)
Nathan Rover (3)
Navin V Nagiah (14)
Néstor Sánchez (31)
Nik Kalyani (14)
Peter Donker (54)
Philip Beadle (135)
Philipp Becker (4)
Richard Dumas (22)
Robert J Collins (5)
Roger Selwyn (8)
Ruben Lopez (1)
Ryan Martinez (1)
Salar Golestanian (4)
Sanjay Mehrotra (9)
Scott McCulloch (1)
Scott S (11)
Scott Wilkinson (3)
Scott Willhite (97)
Sebastian Leupold (80)
Shaun Walker (237)
Shawn Mehaffie (17)
Stefan Cullmann (12)
Stefan Kamphuis (12)
Steve Fabian (31)
Timo Breumelhof (24)
Tony Henrich (3)
Torsten Weggen (2)
Vicenç Masanas (27)
Vincent Nguyen (3)
Vitaly Kozadayev (6)
Will Morgenweck (40)
Will Strohl (165)
William Severance (5)

Content Layout

Subscribe to DNN Digest

Subscribe to DNN Digest

DNN Digest is our monthly email newsletter. It highlights news and content from around the DNN ecosystem, such as new modules and themes, messages from leadership, blog posts and notable tweets. Keep your finger on the pulse of the ecosystem by subscribing.  


Copyright 2017 by DNN Corp Terms of Use Privacy
What is Liquid Content?
Find Out
What is Liquid Content?
Find Out
What is Liquid Content?
Find Out