Nancy Unit Debugging Routing Error Details
Posted on: February 18, 2015
Unit tests are something that are easier than ever with the Owin standard because everything is modular which make mocking easily. However, some basic output may be not as intuitive as we are used to be. For example, during unit testing routing I stumble into an error which say that the Http Status was to 500 instead of 200. No exception was provided. How do you get the exception that thrown the Http Status Code 500? The solution is to get the output of the Html and see the error.
First, if you are unit testing routing, you should use the Nancy.Testing Nuget's package. This one give you a Browser object that allows to simulate an Http query. You can execute the path that you want to test by using the Get method. This return a BrowserResponse. Getting the status code is available by verifying the StatusCode.
// Arrange var browser = new Browser(bootstrapper); // Act var result = browser.Get("/", with => with.HttpRequest());// Assert Assert.AreEqual(HttpStatusCode.OK, result.StatusCode);
The problem is when the assert fail. You do not have any indication of why the Nancy Routing failed. If you are debugging and that you check the BrowserResponse object, you will see a Body property but it is of type Byte. This type is not humanely understandable. The trick is to use the extension method AsString() to get the Html output and read the error message.
var html = result.Body.AsString();
That say, most of the time is that the app.config does not have the reference to Razor page. You just need to include the Nancy.ViewEngines.Razor and be sure that the app.config has the Razor configuration and you will be able to have a HttpStatus of 200.
<system.web.webPages.razor> <pages pageBaseType="Nancy.ViewEngines.Razor.NancyRazorViewBase"> <namespaces> <add namespace="Nancy.ViewEngines.Razor" /> </namespaces> </pages> </system.web.webPages.razor>