Created
September 19, 2013 08:01
-
-
Save marcusoftnet/6620378 to your computer and use it in GitHub Desktop.
TestingViewFactory
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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