EPiServer  /  CMS March 07, 2013

Tests for the EPiServer 7 MVC templates

Example unit tests for the MVC version of the EPiServer 7 Alloy templates available for download.

During the development of the ASP.NET MVC templates for EPiServer 7 I created a number of tests, or executable specifications, in a separate project. As it may be of interest to others I've made it available for download on EPiServer World.

The tests uses a test framework, or rather a Context/Specification framework, named Machine.Specifications, or MSpec for short. In order to stub out CMS components such as IContentLoader the project also uses the mocking framework FakeItEasy.

The purpose of the tests isn't to provide a hundred percent code coverage for the production code. It does however offer decent coverage of the content area rendering functionality which is probably the most complex part of the Alloy templates, and also one of the parts most likely to be used in "real" projects.

It also features a couple of tests for the search page's controller. Unlike the content area rendering tests, which I found highly useful when building the templates, the purpose of these is primarily to provide examples of how it's possible to test controllers and similar components when using EPiServer CMS.

About MSpec

If you're familiar with traditional test frameworks such as NUnit, xUnit.NET or (god forbid) MS Test the tests may seem a bit odd. That's because MSpec isn't a conventional unit test framework but instead a framework for a concept known as Context/Specification.

With MSpec we create classes whose name should describe a certain context. That context is created by creating a delegate variable of type Establish.

public class given_a_search_query_which_matches_a_single_page : SearchPageControllerSpec
{
    Establish context = () =>
    {
        //Set things up
    };
};

Next we describe a specific action, such as a method call triggered by a user interaction.

Because of = () => result = controller.Index(new SearchPage(), SearchQuery);

Finally we verify that one or several things have happened as a result of the action.

It should_populate_the_view_model_with_a_single_hit = () =>
    ((SearchPageModel)result.Model).Hits.Count().ShouldEqual(1);

The syntax, using variables with long, descriptive names and delegates may seem strange. One reason for it though is that it helps us to be expressive and when running the tests/specifications the output can be used to see the purpose of the test.

For instance, the output from running the above specification looks like this:

given a search query which matches a single page
» should populate the view model with a single hit

Why MSpec?

EPiServer is, at least for the most part, test framework agnostic. Nor does EPiServer, as far as I know, make any specific recommendations regarding test frameworks.

I choose to use MSpec as I've used it a lot before and like it a lot. In fact, I've previously written about how much I like it.

Using the project

The primary reason for uploading the project is to make it possible to look at the code and perhaps copy parts of it. However, it may also be interesting to try it out with an unmodified version of the MVC Alloy templates.

While the download package for the MVC template doesn't contain the test project it actually contains all of its dependencies. Therefor all one has to do to use it with the Alloy project is to extract it to the solution root folder and include the project in the solution.

In order to run the specifications a MSpec compatible test runner is needed. There's a console runner available but I'd recommend either TestDriven.NET or the runner that's included with ReSharper.

PS. For updates about new posts, sites I find useful and the occasional rant you can follow me on Twitter. You are also most welcome to subscribe to the RSS-feed.

Joel Abrahamsson

Joel Abrahamsson

I'm a passionate web developer and systems architect living in Stockholm, Sweden. I work as CTO for a large media site and enjoy developing with all technologies, especially .NET, Node.js, and ElasticSearch. Read more

Comments

comments powered by Disqus

My book

Want a structured way to learn EPiServer 7 development? Check out my book on Leanpub!

More about EPiServer CMS