Back to home

Unit Testing becomes more magic with Auto Fixture Library

Overview

Unit testing with Sitecore has been there for quite sometime. We need to use the fakedb and the sitecore APIs extensively for the unit testing any sitecore solution. The fakebDb library will provide the test data in-memory and it has to run the real Sitecore API calls for the same. This usage of Sitecore API requires the license file for the unit testing which we will setting up in the root folder and use them in the app.config file etc., etc., quite boring right…

To get rid of this stuff let us see how we can use the recent library AutoFixture which can be used for the Unit Testing the sitecore project version 8.2+. More info on autofixture is here.

How to setup the Autofixture ?

We are going to use,

  • AutoFixture
  • NSubtitude
  • Xunit
  • Xunit.Runner.VisualStudio
  • FluentAssertions

For the ease of our use, we will be setting up a Testing project in our foundation layer of the sitecore solution. Once the test project is created, you need to install the Autofixture from the nuget,

Install-Package Sitecore.AutoFixture.NSubstitute

Its also recommended to add the Sitecore.Kernal reference as per the version of sitecore that you are using. The following references can be added as the PackageReference in the .csproj file. So when you build the project, its gets restored. Attaching the sample of my testing project file,

After this make sure to build the project and there are no errors. Now we have the base Testing project ready in the foundation layer of our sitecore solution.

Writing the Unit Testing

Let us see how to write the Unit testing using our Foundation testing project. I have created a simple component called NoticeBoard that simply displays an image and some text.

In my repository I have a very simple function that gets Field as an input and returns the value of it. Its looks like below,

Let us see how to write the unit testing for this simple function using the Auto Fixture.

Following the typical unit testing approach with fakeDb, the testing method might look like something below,

The arrange phase of the unit testing will be cumbersome for the larger methods. Let us see how it can be simplified using the Autofixture. Below is the modified test method using auto fixture. The method is decorated with the [AutoData] attribute hence the item is expected to get feed by the auto fixture itself. Thus making the arrange phase of unit testing more simpler way.

Though the above method is logically correct, when you run it, it will be failing with the below attached run time exception.

Autofixture did not provide a way to create an instance of the abstract class database. Nevertheless it offers a way where we can customize it. We have to create a class which will do this job like below. We will be doing it in the base foundation testing project. The class inherits the ICustomization interface, and we have customized the creation of this abstract database type as below,

After that the customized class has to be given to the fixture() method like below, so that we can use the same in our test methods as attributes.

Also I have introduced the Item extension class where you can find some extension methods to create an item and add some field to it for this unit testing and I have registered the class with the fixture.

The updated unit test looks like below and you can notice that the arrange phase of the unit testing is now more simpler than before. Also you can notice that I have used the custom attribute that we have created [DefaultAutoData].

In this way the arrange phase of the unit testing will be simpler. You can find the related code from my github.