Blog Banner

Unit Testing Sitefinity with Telerik JustMock: Part 1 – Project Setup

Over the last few weeks I have been exploring different frameworks and tools available for writing unit tests in Sitefinity. Surprisingly, documentation and examples of unit testing Sitefinity are a little light. Sitefinity.com has a couple blog posts on the subject but they don’t exactly provide a quick start on how to unit test a specific piece of custom functionality. Fortunately, Telerik provides its own standalone mocking framework, JustMock, which developers can use to get Sitefinity functionality mocked and tested in an afternoon (one thing to note, this post uses the commercial version of JustMock, which has more features and functionality than JustMock Lite).  Since unit testing is a pretty large topic requiring some set up, I will be breaking this post into two parts. First, I will cover how to setup a Sitefinity project for unit testing. Next, I’ll cover a simple test implementation. The end goal is to provide a step-by-step guide for creating a unit test in Sitefinity. Before I get started, I’ll cover some key terms relating to unit testing.

Unit Testing – The process where the smallest parts of an application’s code base are tested. Single methods or functions are often good candidates for creating a unit test. A unit test should not depend on a database, web service, or external component to the application. A unit test is not an integration test. Unit testing should help programmers, whether they are familiar with the application or not, add or modify existing code and know if it breaks any existing behavior or requirements.

Dependency Injection – Allows an object to be separated from a dependant component, (database, service, other class, etc) which it normally uses to build itself and initialize properly. These dependencies are then injected into the object via constructor injection or set injection. Tests can then be isolated and are not dependant on the surrounding environment and components.

Mocks/Mocking – Mocks are the objects that step in for the existing components that a piece of code is dependent on.  Once a mock is in place over a dependency, it can be modified to return what’s needed for verifying a unit test.

Mocking Framework (e.g. JustMock) – Utilized for generating mocks in unit tests. Not a substitute for a unit testing framework.

Dependency Injection and Mocking play a large role in writing unit tests in Sitefinity (or any application). Since the purpose of a CMS is to update and serve data from its database, if unit tests are going to be written they will need to mock these data accessing classes and services. Any API call that relies on a Sitefinity manager or service will likely require a mock object to inject independently defined property values and behaviors. Once these managers and services are mocked, then tests can be assembled that are de-coupled from their dependencies.

Walkthrough: Project Setup

The following is an example walkthrough for adding a JustMock Unit Test project to an existing Sitefinity project. The next blog post will cover writing a unit test for a search reindex method in a custom search utility class. This walkthrough can act as a starting point for future unit tests projects.

1) Install Telerik JustMock (http://www.telerik.com/products/mocking/download.aspx).  There is a JustMock Lite version as well, but mocking Sitefinity will require a number of commercially supported features.

2) Once Telerik JustMock is installed, open a Sitefinity application in Visual Studio. Below is the solution for a newly created 8.0 site.

project_1

3) Add or identify custom Sitefinity logic. Since this new site doesn’t have anything custom yet, create a utility class for handling search related methods. Then add a “Reindex” method that can be passed the name of a search index to reindex it (feel free to port over the code specified below).

project_2

4) Now that the utility method is squared away, add a JustMock Unit Test project to the solution.

project_3

5) Add a reference in the Unit Test project to the Sitefinity web application.

project_4

project_6

6) Since tests are going to be written that use the Sitefinity API, make sure the unit test project has the same references to Telerik.Sitefinity libraries as SitefinityWebApp. Check the Telerik.Sitefinity.dll version and/or the site version to make sure you know the correct library to reference. In this example, I used 8.0.5719.0.

sf-version

7) The easiest way to get the necessary references is to configure a NuGet source for nuget.sitefinity.com and pull in the core libraries. My Sitefinity web application is referencing Telerik.Sitefinity 8.0.5719.0, ServiceStack 4.0.30319, and Telerik.OpenAccess 2015.1.225.1.

project_7

Once the source is configured, open Package Manager Console and execute the following with the unit test project name:

Get-Project DemoUnitTests | Install-Package Telerik.Sitefinity.Core -Version 8.0.5719.0
Get-Project DemoUnitTests | Install-Package Telerik.DataAccess.Core -Version 2015.1.225.1
Get-Project DemoUnitTests | Install-Package ServiceStack.Signed -Version 4.0.30


References should now be updated:

project_8

8) Create a new unit test class in the Unit Test project

project_9

With the SearchTests class added to the Unit Test project, everything should build and be ready for writing test methods. My next post will pick up here and cover how to mock specific Sitefinity API calls before we run our first set of tests.

Our marketing team is always on the prowl for useful facts and internet tidbits. Be sure to check our blog often for all of our updates and posts!

Write a review