Speed up widget development with Sitefinity MCP server. Learn more...

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.
NEW TO SITEFINITY?

Want to learn more?

Enhance your Sitefinity skills by enrolling in free training sessions. Become Sitefinity certified through Progress Education Community to strengthen your professional credentials.

Get started with Integration Hub | Sitefinity Cloud

This free lesson teaches administrators, marketers, and other business professionals how to use the Integration hub service to create automated workflows between Sitefinity and other business systems.

Web Security for Sitefinity Administrators

This free lesson teaches administrators the basics about protecting your Sitefinity instance and its sites from external threats. 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 ASP.NET Core and take advantage of its decoupled architecture and modern development model.

Was this article helpful?