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.
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);
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();
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.
This blog is built with EPiServer Community, EPiServer CMS, ASP.NET MVC and a bunch of other great products. The source code is available for download at the projects page, where you also can read more about this site and my other projects.
read more
Comments
Marthin Freij 1 years ago
Great job! When do you sleep?
Erik Lidälv 1 years ago
As always - excellent work! I have a question... I would like to "merge" pages from EPiServer and URLs from a XML-file in a EPiServer PageTree control. I thought that it would be cool to just create "fake" pages from the data in the XML-file and then bind the control to the PageDataCollection. But it seems that the EPiServer controls "understands" that the pages are not real and therefore no pages are listed. Do you know if this can be solved? Or do you have a better idea? :) Thanks!
Erik
Joel Abrahamsson 1 years ago
The EPiServer controls filter for access rights and publish status which might be the cause of this problem. As of now I'm guessing that you will have problems getting this to work using the built in controls :(