Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Mocking the Context on GlassController<T>
[TestFixture] // using NSubstitute, NUnit and Fluent Assertions
public class TypedGlassControllerTests
{
[Test]
public void GlassController_can_set_and_get_context()
{
// Arrange
StubClass classToReturn = new StubClass();
var testHarness = new SingleTypedGlassControllerTestHarness();
testHarness.SitecoreContext.GetCurrentItem<StubClass>().Returns(classToReturn);
// Act
var result1 = testHarness.GlassController.Context;
var result2 = testHarness.GlassController.Context;
// Assert
result1.Should().Be(classToReturn);
result2.Should().BeSameAs(result1);
testHarness.SitecoreContext.Received(1).GetCurrentItem<StubClass>();
}
public class StubClass
{
}
public class SingleTypedGlassControllerTestHarness
{
public SingleTypedGlassControllerTestHarness()
{
SitecoreContext = Substitute.For<ISitecoreContext>();
GlassHtml = Substitute.For<IGlassHtml>();
RenderingContextWrapper = Substitute.For<IRenderingContextWrapper>();
HttpContext = Substitute.For<HttpContextBase>();
GlassController = new GlassController<StubClass>(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<StubClass> GlassController { get; private set; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment