unit test and mocking startpage



I'm trying to make some unit tests to an episerver website. 

I need to mock the startpage since it is being used in a service layer like this:

            var contentLoader = ServiceLocator.Current.GetInstance();

            var startPage = contentLoader.Get(ContentReference.StartPage);

In the unit test I have managed to mock the IContentLoader (and IContentRepository) however mocking the startpage is causing problems. 

I'm mocking the startpage like this:

            SiteDefinition.Current = new SiteDefinition()


                StartPage = new PageReference(1928) 


(the number is the id of the startpage.)

This works only to a certain degree. The problem is that when I run the unit test and when the unit test process gets to below code in the service layer 

            var contentLoader = ServiceLocator.Current.GetInstance();

            var startPage = contentLoader.Get(ContentReference.StartPage);

...then var startPage is null (eventhough startpage is recognized by its id).

However if I debug the website in a normal way, (that is not running a unit test) then var startPage is initialized. 

Its like the contentLoader.Get only return the startpage when running in a browser, and not in a unit test.

hope it makes sense.



Feb 10, 2016 16:08


Modifying the SiteDefinition will only change the ContentReference you get from ContentReference.StartPage. contentLoader.Get<AnyContentType> will just return whatever content the specified ContentReference points to. Since this is a unit test where you are mocking the content loader you would have to setup .Get<StartPage> explictly to return the expected value. If you don't do that your the mock will return null by default (depending on the mocking framework I assume).


Per Gunsarfs 

Feb 10, 2016 17:04

Hi Per

Thanks for your quick reply. 

I have already tried going that route, however it cause problem with setting the value of the id (1928)

if I try below code in the unit test class

var mockContentRepsitoryStartPage = new Mock<IContentRepository>();

    mockContentRepsitoryStartPage.Setup(r => r.Get<StartPage>(ContentReference.StartPage)).Returns(new StartPage { });
var mockLocatorStartPage = new Mock<IServiceLocator>();
    mockLocatorStartPage.Setup(i => i.GetInstance<IContentRepository>()).Returns(mockContentRepsitoryStartPage.Object);
var startPage = ServiceLocator.Current.GetInstance<IContentRepository>().Get<StartPage>(ContentReference.StartPage);
startPage.SettingsPageReference.ID = 1928;

then when running the unit test I get an error saying:'System.NullReferenceException' 
the SettingsPageReference is null.
so Im kinda running around in circles.

Feb 10, 2016 18:12

You are creating an empty Start Page in first line of code. Try adding some more info there...

Feb 10, 2016 18:25

Yes, Daniel is correct. Or you could also change

startPage.SettingsPageReference.ID = 1928;


startPage.SettingsPageReference = new ContentReference(1928);
Edited, Feb 11, 2016 8:48


Well I tried some more going the direction you pointed out. And finally I got it working

For those in same situation in the future; The final code that makes it work in the unit test goes like this:

var mockContentLoaderStartPage = new Mock<IContentLoader>();
    mockContentLoaderStartPage.Setup(r => r.Get<StartPage>(ContentReference.StartPage)).Returns(new StartPage { });
var mockLocatorStartPage = new Mock<IServiceLocator>();
    mockLocatorStartPage.Setup(i => i.GetInstance<IContentLoader>()).Returns(mockContentLoaderStartPage.Object);
var startPage = ServiceLocator.Current.GetInstance<IContentLoader>().Get<StartPage>(ContentReference.StartPage);
startPage.SettingsPageReference = new PageReference(1928); //id is the id of the startpage

Thanks a lot Per

NB: @Daniel, thanks for your input, however Per's solution directed me to the answer.
Feb 11, 2016 9:11
mockContentLoaderStartPage.Setup(r => r.Get<StartPage>(ContentReference.StartPage)).Returns(new StartPage {SettingsPageReference = new PageReference(1928) });

or similar yes. Fill out the properties you need when you set up the mocked object. Nice that you got it working :)

Feb 11, 2016 9:33

Hi Daniel 

Thanks for the nice write-up :)

Feb 11, 2016 13:15
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.