Let AutoFixture populate your Episerver content types

den 17 juni 2016

Have you ever reflected over the repetitive work you do when you write a lot of tests? This is boring and time consuming and it got me thinking that there must be a library that addresses this issue. Luckily, there is!

A big part of my daily work is spent developing solutions based on Episerver CMS. When this is done I write all sort of tests where they are applicable. When you write a lot of test you spend most of the time in the arrange phase, setting up all preconditions and inputs for the given test. Wouldn't it be better to focus on what's being tested instead of the time arranging it? There is a library called AutoFixture that assists you with that.


AutoFixture is an open source library that helps you write maintainable tests. One powerful module of AutoFixture is AutoData. AutoData is an extension of xUnit’s DataAttribute which is an attribute that enables data-driven/parameterized tests with arbitrary data out of the box.

Some of my fellow coworkers started to use this library a while ago and it got me interested in starting to use it as well. AutoFixture minimizes the arrange phase of your unit tests in order to maximize maintainability, which was exactly what I was looking for. Not only for testing regular DTO’s but also to test properties on Episerver content types.

For you that are familiar with Episerver you know the concept of content types and that they are represented as classes with a bunch of properties. The properties are representing the actual data fields that the editors are working with.

Without any customizations, you are not able to use AutoData-attributes when testing values of Episerver content types. What happens is that AutoFixture attempts to populate properties, and if there are some dependencies that can’t be fulfilled it will fail.

I found out that this was the problem with content types. Properties on content types have dependencies on underlying systems, like a database context which is normally not available in a testing context. If you are interested in how AutoFixture creates objects you can read about it here.

Luckily AutoFixture is a well designed library, written by Mark Seemann, that allows you to customize the creation of objects before the actual test is executed.

I’m going to share my solution how I’ve managed to auto populate content types with the help of AutoFixture in this blog post. This assumes you to have some basic knowledge of how the library works. What I did was I created a custom ISpecimenBuilder that only addresses this specific problem: the properties of the content types.