Episerver is a fairly good platform if you want to follow TDD and get good test coverage in your project compared to a lot of other CMS systems I've used over the years.  A few areas like content references, selection factories, and initialization modules could have some better constructor support, but, with some minor tweaks to your code, it's relatively simple to test stuff.

One thing that you will likely need to test a lot is content areas.  First, testing content areas are possible and it's pretty easy, however, you may want to use a few helper methods to help keep the amount of set-up code you'll need to write to a minimum.  In today's post, I'll cover some useful methods you might want to consider using in your project.

Getting Items Of The Same Type

A lot of time you may want to configure a content area to only allow a certain type of block. In these instances, instead of writing a load of code to manually loop through a content area and return the blocks, like so:  

I won't write all the tedious test set-up code that you'll need to write in order to make this snippet testable, from my experience you'll likely need to use a minimum of 2 mock set-ups. The page will need to be mocked and the content area will need to be mocked The following helper method will help ensure you write slightly less set-up testing code:  

In here you can mock the IEpiserverContextResolver interface on the class so it returns a list. In a lot of codebases that I've reviewed, a lot of developers who are writing some tests, tend to not bother using this sort of approach of writing code to make their tests easier.

Wrap Any Code To Add Blocks To Content Areas

If you have any requirements to write some code that adds blocks to a content area, again I would recommend that you extract the code to add an item into a helper., like so:

The snippet above is nothing groundbreaking or really clever. Again, most developers tend to add items directly to the page object and then struggle to unit test the code. If you decided to use the snippet above instead of directly adding items to your page item, you inject the IEpiserverContextResolver into your class and then only add blocks to your content items using the above method. When you write your code following this approach, the number of mock objects that you'll need to create and the amount of set-up code will be simplified. I've worked on projects where trying to mock adding different types of blocks has resulted in 20-30 lines of code. After using this type of method, I've managed to get that down to under 10.

Wrap Any Code To Check If A Content Area Has Items

This is another easy snippet, wrapping any checks will mean you won't need to mock your content area and write all the test code to add items to the mock.

Unit Testing ContentAreas Takeaway

If you are working on a project that has a lot of unit tests then you are doing better than most people. 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 these patterns.. the less you will need to write tedious test set-up code. In this guide, I've detailed two methods, which admittedly are pretty simple, but will make your Episerver unit tests easier to write and maintain.