London Dev Meetup Rescheduled! Due to unavoidable reasons, the event has been moved to 21st May. Speakers remain the same—any changes will be communicated. Seats are limited—register here to secure your spot!

Mocking IPageCriteriaQueryService

Vote:
0

Hi all

 

I'm trying to mock IPageCriteriaQueryService with RhinoMocks. At the moment I've got this (failing code)

            // Create a mock repository
            var mockRepository = MockRepository.GenerateMock<IPageCriteriaQueryService>();

            //.. setup stubs
            mockRepository.Stub(x => x.FindPagesWithCriteria(Arg<PageReference>.Is.Anything, Arg<PropertyCriteriaCollection>.Is.Anything)).Return(new PageDataCollection());

// mock service locator
             var mockLocator = MockRepository.GenerateMock<IServiceLocator>();
            mockLocator.Stub(x => x.GetInstance<T>()).Return(mockRepository);
            ServiceLocator.SetLocator(mockLocator);

    But it fails when setting up the mocks. It seems to want a language to be set i.e. this line in the stack trace - EPiServer.Globalization.ContentLanguage.get_PreferredCulture()

The full trace is

   at EPiServer.BaseLibrary.ClassFactory.get_Instance()
   at EPiServer.Globalization.ContentLanguage.get_PreferredCulture()
   at EPiServer.Core.PageCriteriaQueryServiceExtensions.FindPagesWithCriteria(IPageCriteriaQueryService queryService, PageReference pageLink, PropertyCriteriaCollection criterias)
   at NitbWebNUnit.Setup.ServiceLocatorSetup.<InitPageQueryService>b__3(IPageCriteriaQueryService x) in c:\code\NITB\Cds.Nitb.Web.NUnit\Setup\ServiceLocatorSetup.cs:line 68
   at Rhino.Mocks.RhinoMocksExtensions.Expect[T,R](T mock, Function`2 action)
   at Rhino.Mocks.RhinoMocksExtensions.Stub[T,R](T mock, Function`2 action)
   at NitbWebNUnit.Setup.ServiceLocatorSetup.InitPageQueryService() in c:\code\NITB\Cds.Nitb.Web.NUnit\Setup\ServiceLocatorSetup.cs:line 68
   at NitbWebNUnit.TidiMigrationTest.CanMigrateOneRecord() in c:\code\NITB\Cds.Nitb.Web.NUnit\TidiMigrationTest.cs:line 16

Can anyone cast any light on this.

Many thanks for any/all help

Tim B

 

 

#79247
Dec 11, 2013 16:17
Vote:
0

The method you are trying to mock is not actully on the interface but is an extension method to the interface. Try mock method with following signature instead:

EPiServer.Core.PageDataCollection FindPagesWithCriteria(EPiServer.Core.PageReference pageLink, PropertyCriteriaCollection criterias, string languageBranch, EPiServer.Core.ILanguageSelector selector)

#79253
Dec 11, 2013 17:12
Vote:
0

That's worked!! I would not have got that at all. I had to change the signature of FindPagesWithCriteria to include the languageBranch string but that's not a problem.

 

Thank you for that

#79295
Dec 12, 2013 14:55
This thread is locked and should be used for reference only. Please use the Episerver CMS 7 and earlier versions forum to open new discussions.
* 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.