Try our conversational search powered by Generative AI!

Mock ContentArea

Vote:
 

Hi, currently setting up some unit tests that involve looking at a contentarea of a page and im getting stuck on mocking the contentarea contentareaitems.

// pagetype instance, not loading for repo as just need the end object, not sure if that presents a problem later in test (i.e. when assigning contentarea).

var devPage = PageDataObjects.GetComingSoonDevelopmentPage(TestData.PseudoRandomString(10), ContactConsoleMode.Full);

// blockdata to return

var block = new ImageBlockData{

BlockImage =
new ContentReference(TestData.PseudoRandomInt(1, 100), TestData.PseudoRandomInt(1, 100)),
HoverCaption = TestData.PseudoRandomString(10),
HoverHeading = TestData.PseudoRandomString(10),
TargetUrl = new Url(TestData.PseudoRandomString(10))
};

// Setup the repository to return a start page with a preset property value
repo.Setup(r => r.Get(It.IsAny()))
.Returns(item);

// Setup the service locator to return our mock repository when an IContentRepository is requested
service.Setup(l => l.GetInstance()).Returns(repo.Object);

// Make use of our mock objects throughout EPiServer
ServiceLocator.SetLocator(service.Object);

// new instance of contentareaitem to return

var contentAreaItem = new ContentAreaItem()
{
ContentGroup = "ImageBlockData",
ContentLink =
new ContentReference(TestData.PseudoRandomInt(1, 100), TestData.PseudoRandomInt(1, 100))
};

contentAreaItem.GetContent(this.ContentRepository.Object);

var gallery = new ContentArea();
gallery.Items.Add(contentAreaItem);
devPage.Gallery = gallery;


Not sure if the above is the right approach for this or if im just missing some config / lines, but I get the following error "ClassFactory not initialized" when GetContent is called.

Cheers,


Adam

#112654
Nov 03, 2014 12:18
Vote:
 

Ok trying this another way as I only need to mock a specific property of a page, so changed my code to just this:

var devPage = new Mock<DevelopmentPageData>();

devPage.SetupAllProperties();
devPage.Object.SiteItemKey = TestData.PseudoRandomString(20);
devPage.Object.ConsoleMode = ContactConsoleMode.Full;
devPage.Object.DevelopmentState = DevelopmentStates.ComingSoon;

if (isVisible)
{

var contentAreaItem = new ContentAreaItem

{
ContentLink = new ContentReference(TestData.PseudoRandomInt())
};

var gallery = new ContentArea();
gallery.Items.Add(contentAreaItem);

devPage.Setup(x => x.Gallery)
.Returns(gallery);
}

So all i need is for devPage.Gallery (the contentarea property) to return my gallery contentarea object ive setup, trouble is when I try to add contentareaitem on gallery.items.add line i get a structure map error:

Activation error occurred while trying to get instance of type ISecuredFragmentMarkupGeneratorFactory, key "" 

#112674
Edited, Nov 03, 2014 18:26
Vote:
 

Hi Adam,

You need to mock the ContentArea in order to get round that error. The below code worked for me

        if (isVisible)
        {
            var contentAreaItem = new ContentAreaItem
            {
                ContentLink = new ContentReference(TestData.PseudoRandomInt())
            };

            galleryMock = new Mock<ContentArea>();
            galleryMock.Setup(x => x.Items)
                       .Returns(new[] { contentAreaItem });

            var gallery = new ContentArea();
            gallery.Items.Add(contentAreaItem);
            devPage.Setup(x => x.Gallery)
                   .Returns(gallery.Object);
        }

I know this is an old post, but just in case anybody needs the answer there it is.

#116526
Edited, Feb 01, 2015 2:16
* 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.