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;
pages.Sort(comparer);

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:

        [AssemblyInitialize]
        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));
        }

        [TestInitialize]
        public void Setup()
        {
            ...
            EPiServer.Globalization.ContentLanguage.PreferredCulture = new System.Globalization.CultureInfo("sv-SE");
        }
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s