Skip to content

Instantly share code, notes, and snippets.

@marcusoftnet
Created September 19, 2013 08:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marcusoftnet/6620378 to your computer and use it in GitHub Desktop.
Save marcusoftnet/6620378 to your computer and use it in GitHub Desktop.
TestingViewFactory
using Nancy;
using Nancy.Testing;
using Nancy.ViewEngines;
namespace ItemVariants.Admin.Tests.Utils
{
// From http://melinder.se/blog/2012/02/nancytesting-intercept-the-model-sent-to-a-view/
public class TestingViewFactory : IViewFactory
{
private readonly DefaultViewFactory _defaultViewFactory;
public TestingViewFactory(DefaultViewFactory defaultViewFactory)
{
_defaultViewFactory = defaultViewFactory;
}
public Response RenderView(string viewName, dynamic model, ViewLocationContext viewLocationContext)
{
// Intercept model
viewLocationContext.Context.Items["###ViewModel###"] = model;
viewLocationContext.Context.Items["###ViewName###"] = viewName;
viewLocationContext.Context.Items["###ModulePath###"] = viewLocationContext.ModulePath;
viewLocationContext.Context.Items["###ModuleName###"] = viewLocationContext.ModuleName;
return _defaultViewFactory.RenderView(viewName, model, viewLocationContext);
}
}
public static class BrowserResponseExtensions
{
public static dynamic GetModel<TType>(this BrowserResponse response)
{
return (TType)response.Context.Items["###ViewModel###"];
}
public static string GetViewName(this BrowserResponse response)
{
return response.Context.Items["###ViewName###"].ToString();
}
public static string GetModulePath(this BrowserResponse response)
{
return response.Context.Items["###ModulePath###"].ToString();
}
public static string GetModuleName(this BrowserResponse response)
{
return response.Context.Items["###ModuleName###"].ToString();
}
}
/*
[Fact]
public void should_return_an_event_view()
{
_response.GetViewName().Should().Equal("event.cshtml");
}
[Fact]
public void should_return_an_module_name()
{
_response.GetModuleName().Should().Equal("ItemVariantAdmin");
}*/
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment