This repository has been archived by the owner on Jan 24, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Nancy Testing View Location
Eric Domke edited this page Mar 3, 2017
·
2 revisions
By default Nancy unit tests will struggle to find view files when you run your tests. You can create the following file in your test project to help Nancy out.
public class TestingRootPathProvider : IRootPathProvider
{
private static readonly string RootPath;
static TestingRootPathProvider()
{
var directoryName = Path.GetDirectoryName(typeof (Bootstrapper).Assembly.CodeBase);
if (directoryName != null)
{
var assemblyPath = directoryName.Replace(@"file:\", string.Empty);
RootPath = Path.Combine(assemblyPath, "..", "..", "..", "Escape.Web");
}
}
public string GetRootPath()
{
return RootPath;
}
}
You will have to alter RootPath to match your solutions layout and target project's folder name. The idea is you want your view engine to not look in "bin/debug" for views but instead look in your target project for the views. This is accomplished by using ".." to go up a folder level until you can then go into your target project.
As documented on the root path wiki page, the bootstrapper needs to be told about this class. You can do this in a custom bootstrapper with the code
public class CustomBootstrapper : DefaultNancyBootstrapper
{
protected override IRootPathProvider RootPathProvider
{
get { return new CustomRootPathProvider(); }
}
}
Alternatively, in a unit test, it might look something like
[TestMethod()]
public void HomeModuleTest()
{
// When
var result = new Browser(with => {
with.RootPathProvider<TestingRootPathProvider>();
with.ViewFactory<TestingViewFactory>();
with.AllDiscoveredModules();
}).Get("/", with => {
with.HttpRequest();
});
// Then
Assert.AreEqual(HttpStatusCode.OK, result.StatusCode);
Assert.AreEqual("MyViewName.cshtml", result.GetViewName());
}
- Introduction
- Exploring the Nancy module
- Routing
- Taking a look at the DynamicDictionary
- Async
- View Engines
- Using Models
- Managing static content
- Authentication
- Lifecycle of a Nancy Application
- Bootstrapper
- Adding a custom FavIcon
- Diagnostics
- Generating a custom error page
- Localization
- SSL Behind Proxy
- Testing your application
- The cryptography helpers
- Validation
- Hosting Nancy with ASP.NET
- Hosting Nancy with WCF
- Hosting Nancy with Azure
- Hosting Nancy with Suave.IO
- Hosting Nancy with OWIN
- Hosting Nancy with Umbraco
- Hosting Nancy with Nginx on Ubuntu
- Hosting Nancy with FastCgi
- Self Hosting Nancy
- Implementing a Host
- Accessing the client certificate when using SSL
- Running Nancy on your Raspberry Pi
- Running Nancy with ASP.NET Core 3.1