In this tutorial, you will learn three tips that will make you more efficient when you are unit testing Episerver content areas. You will learn how to create a shared helper method that you can use when working with content areas. Using this helper will reduce the number of unit tests that you will need to write. Episerver is a fairly good platform if you want to follow TDD. As Episerver is written to interfaces and uses dependency injection, you can get good test coverage in your project. I can not say the same about some of the other CMS systems that I've used over the years. A few areas like content references, selection factories, and initialization modules could have some better constructor support, however, with some minor tweaks to your code, it's relatively simple to easily test your content areas and any custom logic that you might apply to them.

Checking For Specific Blocks

When building a website powered by Episerver, one thing that you will likely need to test a lot is content areas. Getting good test coverage on a content area is possible, however, you may want to use a few helper methods to help reduce the amount of boilerplate that you will need to write to a minimum. A lot of time you may want to configure a content area to only allow a certain type of block. one way of adding this validation would be to write some code that loops through the Items connection, like so:  

To test this type of code, you will need to use two mocks everywhere you use a content area. The containing page will need to be mocked and the content area will also need to be mocked. Instead of having to write this set-up code for each content area, you can extend the IEpiserverContextResolver instead. This will save you time. Using this helper will means you can test the logic once in one central location, rather than having to duplicate the logic throughout your codebase:  

Adding Items Into Content Areas

If you need to write code that adds blocks into a content area, I would recommend that you create a shared helper to perform this task, rather than duplicate the same code everywhere. This shared helper could be done using this method:

The snippet above is nothing groundbreaking and you may not see the value in it at first. To use this code, inject the IEpiserverContextResolver dependency into your class via a constructor. Always use this method to add blocks to your content items in code. Most developers tend to accidentally duplicate the code required to add items into content areas in each page controller, or, initialisation module. Using the snippet above will mean you only need to test this logic once! Meaning, the number of mock objects that you'll need to create and the amount of set-up code will be simplified 😊😊😊

Checking A Content Area Has Values

This is another useful testing snippet that will save you time. Wrapping any validation check into a shared helper will reduce the number of unit tests you will need to write as well. In this example, we check that the content area contains items:

If you are working on a project and you want to have high test coverage and you also have a lot of content areas, using a shared helper to perform content area validation and conditional checks will save you time. When you start unit testing Episerver there are a number of patterns and approaches you can use that will make writing your tests easier. The more you can follow the principle of write once and share, the less tedious test set-up code you will need to wrtite. In this guide, I've detailed two methods, which admittedly are pretty simple, however, both approaches will make your Episerver unit tests easier to write and maintain. Happy Coding 🤘