Manually assigning language to page for unit test

Vote:
 

Hi

I'm trying to create a page manually for unit test purposes. It need to have a language branch but I can't create it. I'm trying

  ProductPage product = new ProductPage();
  product.Property["ContentLink"] = new PropertyContentReference(PageReference.EmptyReference);
  product..Property["Language"] = new PropertyString("en");

But it is failing on this piece of code    

 ILocalizable localizable = content as ILocalizable;
        if (localizable != null)
        {
            this.Language = localizable.Language.Name;
        }

The Language is null so the .Name call throws an object can't be found error. I've also tried directly assigning in to the ILocalization cast page i,e,

((ILocalizable)product).Language = new CultureInfo("en")

But in that case the exception generated tells me that the language branch doesn' exist and can't be assigned.

Just for background I'm trying to create a ContentInLanguageReference object to mock out some episerver find results but I think the problems is more general so I am asking here.

Many Thanks for all/any help

 

 

#77706
Nov 25, 2013 10:45
Vote:
 

PageData differs a bit from other IContent implementations in meaning all its properties are backed by a PropertyData instance. So in test you need to add a corresponding PropertyData instance for the property like for example:

var page = new PageData();
page.Property.AddNoCheck(new PropertyString() { Name = MetaDataProperties.PageLanguageBranch, Value = "en" });
page.Property.AddNoCheck(new PropertyPageReference() { Name = MetaDataProperties.PageLink, Value = new PageReference(4) });
page.Property.AddNoCheck(new PropertyPageReference() { Name = MetaDataProperties.PageParentLink, Value = new PageReference(6) });

An alternative if you have a mocking framework like Moq is to create a mock of PageData like:

var page = new Mock<PageData>();
page.Setup(p => p.ContentLink).Returns(new ContentReference(6));

#77712
Nov 25, 2013 11:23
Vote:
 

Thank you so much for that. I was struggling generally with setting up page data. The mocking works well for me with Rhino Mocks

    var product = MockRepository.GenerateMock<PageData>();
    product.Stub(p => p.ContentLink).Return(new ContentReference(6));
    product.Stub(p => p.Language).Return(new CultureInfo("en"));

Thanks  

#77724
Nov 25, 2013 13:25
Vote:
 

Just as a note - the mocking technique above no longer appears to work in EPiServer 7.5 with objects derived from PageData (though page data itself seems to work still). Therefore the approach needs to be 

var page = new PageData();

page.Property.Add(new PropertyString() { Name = MetaDataProperties.PageLanguageBranch, Value = "en" });
});

    

#79960
Jan 10, 2014 16:48
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.