Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nancy testing project can't find views

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.

like image 288
Max Avatar asked May 05 '12 21:05

Max


1 Answers

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

like image 93
TheCodeJunkie Avatar answered Oct 15 '22 17:10

TheCodeJunkie