Hit a bit of a stumbling block when trying to test a Nancy module from a test project. My test code looks pretty standard:
[TestMethod]
public void Should_return_status_ok_when_route_exists()
{
// Given
var bootstrapper = new DefaultNancyBootstrapper();
var browser = new Browser(bootstrapper);
// When
var result = browser.Get("/", with =>
{
with.HttpRequest();
});
// Then
Assert.AreEqual(result.StatusCode, HttpStatusCode.OK);
}
I get an unable to locate view exception when my module tries to render the view. If I run the project normally the module finds the view. It's only when invoked from the test project that the module can't find it.
The problem is that the views aren't anywhere close your test project, and since the IRootPathProvider is pointing at the wrong place, it can't find them. Two ways to get around this is use the ConfigurableBootstrapper (which is more or less the same as the Default one, but the the possibility to override stuff when initialized) and tell it to use your custom root path provider
var bootstrapper = new ConfigurableBootstrapper(with => {
with.RootPathProvider<CustomRootPathProvider>();
});
You would then implement public class CustomRootPathProvider : IRootPathProvider
and point it in the right place.
The second solution would be to set your views to always copy to the output directory, I believe that should also solve it
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With