Try our conversational search powered by Generative AI!

unit testing creating a block programmatically



I'm writing unit tests where I need to create a block programmatically and then adds it to a content area on a commerce product/variant page.  It looks something like this when creating the block before adding it to a content area:

var imageBlock = contentRepository

imageBlock.Image = image;
imageBlock.AlternativeText = name;

var content = imageBlock as IContent;

The above works, but when I run my unit tests, content is null when I try to cast it to IContent.  I'm aware that EPiServer creates proxy objects for shared blocks in runtime.  I'm not sure how to get pass this or how to change my code.


Apr 03, 2015 10:34

What do you do later with IContent? Do you access any specific properties out of that interface? You can take a look at SharedBlockFactory.CreateSharedBlock(Type). Factoy has long list of dependnecies - so you may have to setup lot of things to get proper functionality out.

But basically - when shared blocks are created proxy class is created manually.

Apr 04, 2015 22:19

Thanks Valdis - After creating the block I need to get the content refernce.  That content reference is used to link the block to a content area.

Apr 06, 2015 19:41

Just stepping one step back..

What is your main goal for unit test? What functionality are you going to cover in that test?

Apr 06, 2015 21:58

Hello Danie

It may be worth reading this article:

Looking at the example code I think you may need to save the block before loading it from the content repo to get the IContent reference. 

Let me know how it goes.


Edited, Apr 07, 2015 11:01

Actually yes, ContentRepository should call shared block factory anyway under the hood - so returned object instance should be of type IContent as well. Anything special maybe about unit test setup?

Apr 07, 2015 12:50

Hi guys,

thanks for your replies

I've written a plugin for the editors to upload multiple images into the asset manager, then create a block for each image and then to link all the image blocks to a content area for a page.

The code below works, but its fails when I try to unit test it.

var imageBlock = baseDependencies

imageBlock.Image = image;
imageBlock.AlternativeText = name;

var content = imageBlock as IContent;

content.Name = name;


For my unit test I would like to to run through the code first without it throwing an exception.  But currently its failing because content is null when I cast it to IContent.  Once I get my unit test just to run through the code I want to test if the "Save" function is called creating the block.

I'm not sure how to setup my unit test or code so that I can cast the block to IContent without it being null.



Apr 07, 2015 18:56

But I guess I can change the code to retrieve the IContent from the repo after saving it.

Apr 07, 2015 19:03

How did you setup unit test that line zero (where you receive instance of repository) will not throw an exception?

Apr 07, 2015 22:09

I've changed the code a bit:

return baseDependencies.ContentRepository

The setup for the content repository for unit test:

ContentRepositoryMock.Setup(x => x.Save(It.IsAny<IContent>(), SaveAction.Publish, AccessLevel.NoAccess))

Apr 08, 2015 9:46

Who is "ContentRepositoryMock"? I just speculate that you are mocking content repository in a way that it actually will not produce IContent as result of "GetDefault()" :)

Apr 08, 2015 10:03

I think I know where I'm going wrong..

ContentRepositoryMock.Setup(x => x.GetDefault<ImageBlock>(folder.ContentLink)).Returns(GalleryImageBlock);

The above is my setup for GetDefault (yes, ContentRepository is the the content repository mock)

I think I should return an IContent object instead of an ImageBlock for the test.  But if I setup the GetDefault to return an IContent object I can't compile my code because the return type needs to be of type ImageBlock.

var content = new Mock<IContent>();
ContentRepositoryMock.Setup(x => x.GetDefault<ImageBlock>(folder.ContentLink)).Returns(content.Object);

Apr 08, 2015 10:18

Yes, this is the issue. If you want to return object that you can cast to IContent, you need to take a look at SharedBlockFactory. You have to produce mixin, create proxy and then return it. A bit more to setup.

Apr 08, 2015 11:51
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.