Try our conversational search powered by Generative AI!

EPiServer 7.18 unit tests ContentLanguage


Hello Everyone,

I updated EPiServer today from version 7.14.1 to 7.18.0 and now all my unit tests are failing!

EPiServer.ServiceLocation.ActivationExceptionActivation error occurred while trying to get instance of type ILanguageBranchRepository, key ""

I seem to be unable to create a fake ContentLanguage instance now in order to set ContentLanguage.Instance before my tests can run. It also does not seem possible to create a fake ILanguageBranchRepository. Previously it was fine to run

new ContentLanguage()

That constructor seems to be removed now and the new one requires an instance of an ILanguageBranchRepository.

new ContentLanguage(A.Fake<ILanguageBranchRepository>())

fails with the same error message though.

Here's my code that worked previously:

public CmsContext()
            //ContentReference.StartPage = new PageReference(4);
            this.StartPageUrl = "/";

            GlobalizationSettings.CultureLanguageCode = GlobalizationSettings.UICultureLanguageCode = MasterCulture.Name;
            ContentLanguage.Instance = A.Fake<ContentLanguage>(); // THIS DOES NOT WORK ANYMORE IN EPI 7.18!!!
            A.CallTo(() => ContentLanguage.Instance.FinalFallbackCulture).Returns(MasterCulture);

            Context.Current = A.Fake<IContext>();
            A.CallTo(() => Context.Current.RequestTime).Returns(DateTime.Now);

            this.ContentLoader = A.Fake<IContentLoader>();
            this.UrlResolver = A.Fake<UrlResolver>();

            this.TemplateResolver = A.Fake<TemplateResolver>();
            A.CallTo(() => ServiceLocator.Current.GetInstance<TemplateResolver>()).Returns(this.TemplateResolver);
            A.CallTo(() => this.UrlResolver.GetUrl(ContentReference.StartPage)).Returns(this.StartPageUrl);
Dec 02, 2014 15:57
<p>Hi Alexander,</p> <p>There was indeed a small change introduced in the ContentLanguage in version 7.18.0 that unfortunately is making it difficult to mock. A couple of hidden dependencies of the ContentLanguage class was exposed to make it more testable (for us), but it was regrettably made internal as it initially featured another internal class. I'll make it public and this change will hopefully make it to the 7.19.0 release or&nbsp;the one following that (7.19.1 or 7.20.0). For now, you may have to make due with the 7.17.0 version which I hope is OK.</p> <p>For reference, the constructor with the ILanguageBranchRepository dependency has been present for quite a while before 7.14.1 (EPiServer CMS 7?), so maybe your mock library (FakeItEasy?) is automatically mocking constructor arguments (FakeItEasy seems to do this). My guess is that your last error message is actually not exactly the same, but instead is saying that it can't get an instance of type HostLanguageResolver?</p>
Dec 02, 2014 23:42
<p>Ok, thank you for explaining and fixing this for the next release Henrik.</p> <p>You are correct about the error messages not being exactly the same.</p> <p></p> <p>I did not know that FakeItEasy actually mocks constructor arguments as well. It was just something I tried to see if it would solve the problem.</p> <p>In case it will help you fix this the actual full error when running the code in the code block in my original post&nbsp;is this one:</p> <p></p> <pre class="brush:plain;auto-links:false;toolbar:false" contenteditable="false">An exception of type 'FakeItEasy.Core.FakeCreationException' occurred in FakeItEasy.dll but was not handled in user code Additional information: Failed to create fake of type "EPiServer.Globalization.ContentLanguage". Below is a list of reasons for failure per attempted constructor: No constructor arguments failed: No usable default constructor was found on the type EPiServer.Globalization.ContentLanguage. An exception was caught during this call. Its message was: Exception has been thrown by the target of an invocation. Constructor with signature (EPiServer.DataAbstraction.ILanguageBranchRepository, EPiServer.Globalization.HostLanguageResolver, EPiServer.ServiceLocation.ServiceAccessor`1[System.Web.HttpRequestBase]) failed: No constructor matches the passed arguments for constructor. An exception was caught during this call. Its message was: Can not instantiate proxy of class: EPiServer.Globalization.ContentLanguage. Could not find a constructor that would match given arguments: Castle.Proxies.ObjectProxy Castle.Proxies.HostLanguageResolverProxy EPiServer.ServiceLocation.ServiceAccessor`1[System.Web.HttpRequestBase] Constructor with signature (EPiServer.DataAbstraction.ILanguageBranchRepository) failed: No constructor matches the passed arguments for constructor. An exception was caught during this call. Its message was: Exception has been thrown by the target of an invocation.</pre> <p></p>
Dec 03, 2014 11:22
<p>Thanks a lot EPiServer!<br /><br />After patching EPiServer.CMS.Core to 7.19.1 all my unit tests are now working again.</p> <p></p> <p><img src="" width="1552" alt="Unit tests working again" height="838" /></p> <p>The only failing test is failing because of a bug in the version of Firefox I'm running. That has nothing to do with EPiServer.<br /><br />Thanks again<br /><br />// Alex</p>
Jan 12, 2015 16:20
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.