Beginning EPiServer Unit Testing: Page Filtering

I managed to solve the problem with ContentLanguage.PreferredCulture as described in my previous post. But six unit tests still failed. It turned a filter had been added to display only published pages like this:

new FilterPublished(PagePublishedStatus.Published).Filter(pages);

For me, this was harder than expected to fix. Pages were created in unit tests like this:

new BeslutsunderlagPage()

(We’re using PageTypeBuilder.) But these have version status “NotCreated” and are filtered out by the above code. After a lot of experimenting (because I’m a complete beginner at EPiServer) I came up with the following way to create these pages that works:

        private static TPage CreatePublishedPage<TPage>() where TPage : PageTypeBuilder.TypedPageData, new()
            var page = new TPage();
            var prop = new PropertyNumber((int)VersionStatus.Published);
            page.Property.Add("PageWorkStatus", prop);
            return page;

Beginning EPiServer Unit Testing: ContentLanguage.PreferredCulture

Yes, we had 348 working unit tests in our EPiServer solution. Then a developer had to solve a bug where pages weren’t correctly sorted. (Å and Ä were sorted as A and Ö as O.) The following code

new FilterPropertySort(“PageName”).Filter(pages);

was changed to

FilterPropertySort comparer = new FilterPropertySort(“PageName”);
comparer.CompareInfo = ContentLanguage.PreferredCulture.CompareInfo;

But now, unit tests started failing. They threw “EPiServer.BaseLibrary.ClassFactoryException: ClassFactory not initialized”. It was not hard to understand why, but how to solve it. It turned out to be relatively simple after looking at Unit test an EPiServer 6 website using Visual Studio 2010 and MSTest. Luckily, I didn’t have to initialize the whole hosting environment. The following code was sufficient:

        public static void Initialize(TestContext context)
            EPiServer.BaseLibrary.ClassFactory.Instance = new EPiServer.Implementation.DefaultBaseLibraryFactory(String.Empty);
            EPiServer.BaseLibrary.ClassFactory.RegisterClass(typeof(EPiServer.BaseLibrary.IRuntimeCache), typeof(EPiServer.Implementation.DefaultRuntimeCache));

        public void Setup()
            EPiServer.Globalization.ContentLanguage.PreferredCulture = new System.Globalization.CultureInfo("sv-SE");