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.
: To use advanced mocking, perform the following:
- Download and install Telerik JustMock.
For more information, Installation and Setup.
- In Visual Studio, enable the JustMock profiler.
For more information, see How to enable JustMock.
- Open your project in Visual Studio and add references to:
: For the sample test class, see
in Sitefinity documentation-samples
In this class, you implement a method, which has dependency on the
and you want to create a unit test for using the different approaches.