Products

Solutions

Resources

Partners

Community

About

New Community Website

Ordinarily, you'd be at the right spot, but we've recently launched a brand new community website... For the community, by the community.

Yay... Take Me to the Community!

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 (22)
Alec Whittington (11)
Alessandra Daniels (3)
Alex Shirley (10)
Andrew Hoefling (3)
Andrew Nurse (30)
Andy Tryba (1)
Anthony Glenwright (5)
Antonio Chagoury (28)
Ash Prasad (37)
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 (213)
Chris Paterra (55)
Clint Patterson (108)
Cuong Dang (21)
Daniel Bartholomew (2)
Daniel Mettler (181)
Daniel Valadas (48)
Dave Buckner (2)
David Poindexter (12)
David Rodriguez (3)
Dennis Shiao (1)
Doug Howell (11)
Erik van Ballegoij (30)
Ernst Peter Tamminga (80)
Francisco Perez Andres (17)
Geoff Barlow (12)
George Alatrash (12)
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)
Kelly Ford (4)
Ken Grierson (10)
Kevin Schreiner (6)
Leigh Pointer (31)
Lorraine Young (60)
Malik Khan (1)
Matt Rutledge (2)
Matthias Schlomann (16)
Mauricio Márquez (5)
Michael Doxsey (7)
Michael Tobisch (3)
Michael Washington (202)
Miguel Gatmaytan (3)
Mike Horton (19)
Mitchel Sellers (40)
Nathan Rover (3)
Navin V Nagiah (14)
Néstor Sánchez (31)
Nik Kalyani (14)
Oliver Hine (1)
Patricio F. Salinas (1)
Patrick Ryan (1)
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)
Sacha Trauwaen (1)
Salar Golestanian (4)
Sanjay Mehrotra (9)
Scott McCulloch (1)
Scott Schlesier (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)
Steven Fisher (1)
Tony Henrich (3)
Torsten Weggen (3)
Tycho de Waard (4)
Vicenç Masanas (27)
Vincent Nguyen (3)
Vitaly Kozadayev (6)
Will Morgenweck (40)
Will Strohl (180)
William Severance (5)
What is Liquid Content?
Find Out
What is Liquid Content?
Find Out
What is Liquid Content?
Find Out