Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Mocking the GlassController Layout Item
[TestFixture] // using NSubstitute, NUnit and Fluent Assertions
public class GlassControllerLayoutItemTests
{
[Test]
public void GlassController_can_set_and_get_layout_item_from_datasource()
{
// Arrange
ID expectedId = new ID(Guid.NewGuid());
StubClass classToReturn = new StubClass();
var testHarness = new GlassControllerTestHarness();
testHarness.RenderingContextWrapper.HasDataSource.Returns(true);
testHarness.RenderingContextWrapper.GetDataSource().Returns(expectedId.ToString());
testHarness.SitecoreContext.GetItem<StubClass>(expectedId.ToString()).Returns(classToReturn);
// Act
var result = testHarness.GlassController.GetLayoutItem<StubClass>();
// Assert
result.Should().Be(classToReturn);
}
[Test]
public void GlassController_can_set_and_get_layout_item_from_context()
{
// Arrange
StubClass classToReturn = new StubClass();
var testHarness = new GlassControllerTestHarness();
testHarness.RenderingContextWrapper.HasDataSource.Returns(false);
testHarness.SitecoreContext.GetCurrentItem<StubClass>().Returns(classToReturn);
// Act
var result = testHarness.GlassController.GetLayoutItem<StubClass>();
// Assert
result.Should().Be(classToReturn);
}
public class StubClass
{
}
public class GlassControllerTestHarness
{
public GlassControllerTestHarness()
{
SitecoreContext = Substitute.For<ISitecoreContext>();
GlassHtml = Substitute.For<IGlassHtml>();
RenderingContextWrapper = Substitute.For<IRenderingContextWrapper>();
HttpContext = Substitute.For<HttpContextBase>();
GlassController = new GlassController(SitecoreContext, GlassHtml, RenderingContextWrapper, HttpContext);
}
public HttpContextBase HttpContext { get; private set; }
public IRenderingContextWrapper RenderingContextWrapper { get; private set; }
public IGlassHtml GlassHtml { get; private set; }
public ISitecoreContext SitecoreContext { get; private set; }
public GlassController GlassController { get; private set; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment