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.
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.