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.
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.
Just stepping one step back..
What is your main goal for unit test? What functionality are you going to cover in that test?
Hello Danie
It may be worth reading this article: http://world.episerver.com/Blogs/Johan-Bjornfot/Dates1/2012/11/Shared-blocks--IContent/.
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.
David
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?
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 .ContentRepository .GetDefault<ImageBlock>(parentFolder); imageBlock.Image = image; imageBlock.AlternativeText = name; var content = imageBlock as IContent; content.Name = name; baseDependencies.ContentRepository .Save(content, SaveAction.Publish, AccessLevel.NoAccess);
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.
Thanks,
Danie
But I guess I can change the code to retrieve the IContent from the repo after saving it.
How did you setup unit test that line zero (where you receive instance of repository) will not throw an exception?
I've changed the code a bit:
return baseDependencies.ContentRepository .Save(content, SaveAction.Publish, AccessLevel.NoAccess);
The setup for the content repository for unit test:
ContentRepositoryMock.Setup(x => x.Save(It.IsAny<IContent>(), SaveAction.Publish, AccessLevel.NoAccess)) .Returns(blockContentReference)
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()" :)
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);
Hi,
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:
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.
Thanks