Nancy test project cannot find views

Hit a little stumbling block when trying to test the 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 am unable to find a view exception when my module tries to display a view. If I usually run a project, the module finds this view. This is only when called from a test project that the module cannot find.

+6
source share
1 answer

The problem is that the views don't close your test project anywhere, and since IRootPathProvider points to the wrong place, it cannot find them. Two ways to get around this is to use a ConfigurableBootstrapper (which more or less matches the default setting, but the ability to override the data during initialization) and tell it to use its own root path provider

 var bootstrapper = new ConfigurableBootstrapper(with => { with.RootPathProvider<CustomRootPathProvider>(); }); 

Then you implement the public class CustomRootPathProvider : IRootPathProvider and point it in the right place.

The second solution is to ensure that your views are always copied to the output directory, I believe that it should also solve it.

+8
source

Source: https://habr.com/ru/post/914989/


All Articles