I am trying to Unit Test precompiled Views, generated by RazorGenerator, that inherit from GlassView, based on this article: http://blog.davidebbo.com/2011/06/unit-test-your-mvc-views-using-razor.html.
I have created a model using the answer from: https://stackoverflow.com/questions/24658121/issue-unit-testing-sitecore-and-glass-with-nunit
// Instantiate the view directly.
var view = new _Views_Components_QuickAction_cshtml();
//Instantiate a TDS Code Generated Glass Model
var model = new QuickActionDatasource()
{
Icon= new IconAnimated(),
Link = new Link()
{
Text = "Link Text"
}
};
// Build a Glass model to pass to View - this works if you're intention is to test the model
ISitecoreService service = Substitute.For<ISitecoreService>();
service.GetItem<QuickActionDatasource>(Guid.Empty).Returns(model);
var result = service.GetItem<QuickActionDatasource>(Guid.Empty);
//Use Razor
HtmlDocument doc = view.RenderAsHtml(result);
// Use the HtmlAgilityPack object model to verify the view.
HtmlNode node = doc.DocumentNode.Element(".quickaction");
Assert.IsNotNull(node);
Currently this fails on GlassView in the InitHelpers method, on the call:
SitecoreContextFactory.Default.GetSitecoreContext()
Specific error:
throw new NotSupportedException("Sitecore Context Requires a Glass Context");
When it can't create a Context object from the GlassContextProvider.
What would be the best way to have the IntiHelper method initialize NSubstitute objects (i.e. an ISitecoreContextFactory), so that I can fake rendering? Can I substitute a GlassContext to pass through the MVC View rendering process?
Having the Views precompiled highlights Code Generation issues thrown at runtime. The Views are rendered via a Sitecore ViewRenderings, and are essentailly 'dumb' Presentation components with a datasource.