Make ContentArea.Add() virtual

Vote:
 

Make ContentArea.Add() virtual so that the testability becomes better. Right now you need to write a lot of code to inject the FragmentFactory. Maybe I ended up doing to much but this is where I got it to work just for doing contentarea.items.Add(new ContentAreaItem()) not throw any structuremap exceptions.

var publicStateAccessor = new Mock();
            var templateResolver = new Mock();
            var contentAccessEvaluator = new Mock();
            var permanentLinkMapper = new Mock();
            var contextModeResolver = new Mock();

            var templateControlLoader = new TemplateControlLoader(templateResolver.Object, new DisplayOptions());

            ContentFragmentFactory fragmentFactory = new ContentFragmentFactory(_contentRepository.Object, 
                templateControlLoader, 
                new DisplayOptions(), 
                publicStateAccessor.Object, 
                contentAccessEvaluator.Object, 
                permanentLinkMapper.Object, 
                contextModeResolver.Object);

            var securedFragmentMarkupGenerator = new Mock();
            var securedFragmentMarkupGeneratorFactory = new Mock();
            securedFragmentMarkupGeneratorFactory.Setup(x => x.CreateSecuredFragmentMarkupGenerator())
                .Returns(securedFragmentMarkupGenerator.Object);

            var serviceLocator = new Mock();

            serviceLocator.Setup(x => x.GetInstance())
                .Returns(securedFragmentMarkupGeneratorFactory.Object);

            ServiceLocator.SetLocator(serviceLocator.Object);
#149238
May 26, 2016 16:21
Vote:
 

Just wondering - what looks like your unit test? What you testing?

#149251
May 26, 2016 18:34
Vote:
 

Hi Valdis, thanks for asking (I know that it's easy to be thinking a bit to much or in the wrong direction when unit testing so I'm glad you ask), I'm testing that a function adds correct values to a page that I create programmatically. So one of the properties, that I want to verify is setting correct values to the newly created page, is a ContentArea. Here's part of the code that does the work.

            newsPage = newsPage.CreateWritableClone() as NewsPage;
            if (newsPage.ImagesContentArea == null)
            {
                newsPage.ImagesContentArea = _episerverProprtiesFactory.CreateContentArea();
            }

            newsPage.ImagesContentArea.Items.Add(new ContentAreaItem { ContentLink = newNewsImage.ContentLink });
            _contentRepository.Save(newsPage, SaveAction.ForceCurrentVersion, AccessLevel.Publish);

Worth mentioning is the _episerverPropertiesFactory.CreateContentArea() that is an interface so that I can mock it and return my own implementation of ContentArea, that is, so that I from my unit testing code can set the FragmentFactory easily when newing up the ContentArea in the code.

        private class StubContentArea : ContentArea
        {
            public StubContentArea(Injected<ContentFragmentFactory> contentFragmentFactory)
            {
                FragmentFactory = contentFragmentFactory;
            }
        }

and then I can do

_episerverProprtiesFactory.Setup(x => x.CreateContentArea())
    .Returns(new StubContentArea(fragmentFactory));

where the fragmentFactory is the mocked code in the first post.





#149257
May 27, 2016 7:16
This thread is locked and should be used for reference only.
* 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.