Nancy testing project can't find views

夙愿已清 提交于 2019-12-07 03:16:29

问题


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.


回答1:


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



来源:https://stackoverflow.com/questions/10466315/nancy-testing-project-cant-find-views

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!