Skip to content

Instantly share code, notes, and snippets.

@cardinal252
Last active September 26, 2017 14:38
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cardinal252/7b5ef23dca7902088dcd to your computer and use it in GitHub Desktop.
Save cardinal252/7b5ef23dca7902088dcd to your computer and use it in GitHub Desktop.
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