Hi,
The GetProperties call is executed in the Kentico 'ComponentPropertiesRetriever' with the 'Retrieve' function. This is done by the 'SectionController' class.
The same for the 'PageTemplateController' that uses the 'Retrieve' function on the 'CurrentPageRetriever'.
So both these methods need to be mocked. That is done like this:
For the section:
private readonly IComponentPropertiesRetriever<CustomSectionProperties> propertiesRetriever = Substitute.For<IComponentPropertiesRetriever<CustomSectionProperties>>();
propertiesRetriever.Retrieve().Returns(new CustomSectionProperties { Title = TITLE });
For the page template:
private readonly ICurrentPageRetriever currentPageRetriever = Substitute.For<ICurrentPageRetriever>();
private readonly IPageBuilderFeature pageBuilder = Substitute.For<IPageBuilderFeature>();
currentPageRetriever.Retrieve(pageBuilder).Returns(article);
The tests that I created in the DancingGoat are like this:
Section test:
[TestFixture]
class CustomSectionControllerTests : UnitTests
{
private const string PARTIAL_VIEW_NAME = "Sections/_CustomSection";
private const string TITLE = "Test Title";
private readonly IComponentPropertiesRetriever<CustomSectionProperties> propertiesRetriever = Substitute.For<IComponentPropertiesRetriever<CustomSectionProperties>>();
[Test]
public void Index_ReturnsCorrectModel()
{
propertiesRetriever.Retrieve().Returns(new CustomSectionProperties { Title = TITLE });
var controller = new CustomSectionController(propertiesRetriever, Substitute.For<ICurrentPageRetriever>());
controller.ControllerContext = ControllerContextMock.GetControllerContext(controller);
controller.WithCallTo(c => c.Index())
.ShouldRenderPartialView(PARTIAL_VIEW_NAME)
.WithModel<CustomSectionViewModel>(m => m.Title == TITLE);
}
}
Template test:
[TestFixture]
class TemplateControllerTests : UnitTests
{
private const string VIEW_NAME = "PageTemplates/_Article";
private readonly ICurrentPageRetriever currentPageRetriever = Substitute.For<ICurrentPageRetriever>();
private readonly IPageBuilderFeature pageBuilder = Substitute.For<IPageBuilderFeature>();
[Test]
public void Index_ReturnsCorrectModel()
{
Fake().DocumentType<Article>(Article.CLASS_NAME);
Article article = new Article()
{
ArticleTitle = "Title 1",
ArticleText = "Text 1"
};
// Dont use the 'ControllerContextMock.GetControllerContext(controller)' because the 'IPageBuilderFeature' needs to be equal to the mocked 'currentPageRetriever.Retrieve(pageBuilder)'
var httpContext = Substitute.For<HttpContextBase>();
pageBuilder.EditMode.Returns(true);
httpContext.Kentico().SetFeature(pageBuilder);
currentPageRetriever.Retrieve(pageBuilder).Returns(article);
var controller = new ArticlePageTemplateController(currentPageRetriever);
controller.ControllerContext = new ControllerContext(httpContext, new RouteData(), controller);
controller.WithCallTo(c => c.Index())
.ShouldRenderView(VIEW_NAME)
.WithModel<ArticleViewModel>(m => m.Title == "Title 1" && m.Text == "Text 1");
}
}
Note that you need to create constructors in your section/template controllers for the unit tests.
/// <summary>
/// Creates an instance of <see cref="ArticlePageTemplateController"/> class.
/// </summary>
/// <param name="currentPageRetriever">Retriever for current page where is the widget used.</param>
/// <remarks>Use this constructor for tests to handle dependencies.</remarks>
public ArticlePageTemplateController(ICurrentPageRetriever currentPageRetriever) : base(currentPageRetriever)
{
}