Implement unit tests

Unit test is a piece of code that tests a unit of work - a logical unit in the system.
All unit tests should be:

  • Fast and independent
  • Consistent and repeatable

The unit tests should not do any input or output, network communication, any kind of intensive or long-running operations. These scenarios are not in the domain of unit testing.

There are a couple of approaches you could follow in order to implement good unit tests and in easy way:

  • Maintain decoupled code
    If you follow this approach, you avoid calling any static methods and program everything against an interface. Later, during the unit testing, you can mock the implementation all of the dependencies easily.
  • Use advanced mocking
    You can take advantage of the advanced mocking capabilities of Progress Telerik JustMock to define the behavior of the static or instance method calls, static constructors, etc.
  • Use testable classes to mock the behavior of class internal logic
  • Use optional method delegates in the class constructor
    This approach is suitable only if you have to mock small number of classes.
PREREQUISITES: To use advanced mocking, perform the following:
  1. Download and install Telerik JustMock.
    For more information, Installation and Setup.
  2. In Visual Studio, enable the JustMock profiler. 
    For more information, see How to enable JustMock.
  3. Open your project in Visual Studio and add references to:
    • Microsoft.VisualStudio.QualityTools.UnitTestFramework
    • Telerik.JustMock
EXAMPLE: For the sample test class, see SampleClassUT.cs in Sitefinity documentation-samples on GitHub.
In this class, you implement a method, which has dependency on the SystemManager and you want to create a unit test for using the different approaches.

Want to learn more?

Sign up for our free beginner training. Boost your credentials through advanced courses and certification.
Register for Sitefinity training and certification.

Was this article helpful?