Hi
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).
Regards
Per Gunsarfs
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); ServiceLocator.SetLocator(mockLocatorStartPage.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.
You are creating an empty Start Page in first line of code. Try adding some more info there...
Yes, Daniel is correct. Or you could also change
startPage.SettingsPageReference.ID = 1928;
to
startPage.SettingsPageReference = new ContentReference(1928);
@Per
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); ServiceLocator.SetLocator(mockLocatorStartPage.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.
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 :)
Hi
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.
Sincerely
Mohammad