EPiServer  /  CMS August 11, 2010

EPiAbstractions.FixtureSupport – Easily create test data for EPiServer CMS

The FixtureSupport module in EPiAbstractions provides ways to easily create PageData objects of different types for usage in unit testing scenarios. The module currently consists of two classes for creating PageData objects, CreatePage and CreateSetOfPageData. It also contains a few classes with extension methods for PageData, PageRepository and collections.

The CreatePage class

To create a PageData object using the CreatePage class we simply call it’s OfType method and specify a type.

// using EPiAbstractions.FixtureSupport;

var page = CreatePage.OfType<PageData>();

Should we want a page of another type, such as the base class for typed pages from Page Type Builder, TypedPageData we simply specify a different type.

var page = CreatePage.OfType<TypedPageData>();

Note that TypedPageData is an abstract class which normally can’t be instantiated. The CreatePage class will solve that problem and create a proxy, thereby enabling you to instantiate it.

Page Type Builder supports injecting dependencies into PageData objects upon instantiation (read more about that here) which means that we can have constructors with parameters in our page type classes. To instantiate such a class simply pass whatever constructor parameters to the OfType method.

string someDependency = string.Empty;

var page = CreatePage.OfType<ArticlePage>(someDependency);

The OfType method will create most of the standard properties for PageData objects but, in most cases, without any values. While we can certainly set the values using dictionary access (page[“PropertyName”] = …) or just setting the value for properties that have setters (page.PageName = …) there are extension methods that allow you to set some values in a fairly readable way. To use these first call the With, ThatHas, AndHas, ThatIs or AndIs methods on PageData, like this:

var parentPage = CreatePage.OfType<PageData>()
    .ThatHas().PageLink(new PageReference(123));

var currentPage = CreatePage.OfType<TypedPageData>()
    .ThatHas().PageName("My name")
    .AndHas().PageLink(new PageReference(1234))
    .AndIs().NotVisibleInMenus()
    .AndIs().ChildOf(parentPage);

The CreateSetOfPageData class

CreateSetOfPageData works similarly to the CreatePage class but it creates several pages instead of a single one. To use it begin by specifying how many pages you want it to create using it’s Containing method and then invoke either the Pages method to get a collection of PageData objects or the PagesOfType<T> method to get a collection of pages of type T.

var pages = CreateSetOfPageData
    .Containing(3).Pages();

var articles = CreateSetOfPageData.
    Containing(3).PagesOfType<Article>();

Once you have a collection of of pages you can also perform some common setup operations on them such as specifying a common parent and whether they should be visible in menus or not.

var parentPage = CreatePage.OfType<PageData>()
    .ThatHas().PageLink(new PageReference(123));

var children = CreateSetOfPageData.Containing(5)
    .Pages().ThatAreAll().ChildrenOf(parentPage)
    .And().ThatAreAll().VisibleInMenus();

Future development

There is certainly a lot of room for development in this module and I know that there are several projects out there that have built similar functionality but not open sourced it. I’d love to get feedback on what should be added and I would be even happier for contributions and/or forks of the project on GitHub.

Tomorrow I’ll put up a post about the Fakes module together with which the FixtureSupport module really shines. Stay tuned!

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