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.

Increase your Sitefinity skills by signing up for our free trainings. Get Sitefinity-certified at Progress Education Community to boost your credentials.

Web Security for Sitefinity Administrators

The free standalone Web Security lesson teaches administrators how to protect your websites and Sitefinity instance from external threats. Learn to configure HTTPS, SSL, allow lists for trusted sites, and cookie security, among others.

Foundations of Sitefinity ASP.NET Core Development

The free on-demand video course teaches developers how to use Sitefinity .NET Core and leverage its decoupled architecture and new way of coding against the platform.

Was this article helpful?