Why does Nancy.Testing fail when @using statements are included in razor layouts?

In a previous question Does Nancy.Testing support nested Razor views? I outlined the difficulties I was having with Nancy unit tests failing when presented with nested razor layouts.

After investigation I can now refine that information. The problem is not related to nesting it is simply this: If you include a @using statement in a razor layout or view, then Nancy will fail to find that referenced namespace if accessed from a unit-test.

For example:

Master Layout

@inherits Nancy.ViewEngines.Razor.NancyRazorViewBase<dynamic>
<!DOCTYPE html>
<html lang="en">
    <head>
        <title>Test</title>    
    </head>
    <body>
        @RenderBody()        
    </body>
</html>

View

@inherits Nancy.ViewEngines.Razor.NancyRazorViewBase<dynamic>
@{
    Layout = "Master.cshtml";
}
hello World 

As you would expect, this works fine. However if we now add a bit of server side processing to the master layout, and so create the need for a @using statement, then unit tests fail (the code runs fine normally, only the unit tests fail) with:

Error Compiling Template: (15, 11) The type or namespace name 'uMentor' could not be found.

Master Layout with server-side code

@inherits Nancy.ViewEngines.Razor.NancyRazorViewBase<dynamic>
@using uMentor.Extensions
<!DOCTYPE html>
<html lang="en">
    <head>
        <title>@Model.Title</title>   
    </head>
    <body>
        @{
            var user = Html.GetCurrentUser();
        }
        @RenderBody()        
    </body>
</html>

I have ensured that my web.config (both the web-site project and the unit-test project) have the correct razor assemblies and namespaces mentioned:

Web.config

<configSections>
  <section name="razor" type="Nancy.ViewEngines.Razor.RazorConfigurationSection, Nancy.ViewEngines.Razor" />
</configSections>
<razor disableAutoIncludeModelNamespace="false">
  <assemblies>
    <add assembly="System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
    <add assembly="Nancy" />
    <add assembly="uMentor" />
  </assemblies>
  <namespaces>
    <add namespace="Nancy" />
    <add namespace="uMentor" />
    <add namespace="uMentor.Domain" />
    <add namespace="uMentor.Extensions" />
  </namespaces>
</razor>

Stripping everything back, I can build the simplest 'hello world' view -> layout with nothing in it, no code. The unit test passes (the response body has the correct html in it). I then put in a redundant @using statement at the top of the view or the layout and the test fails (the response body contains the error message above).

Master Layout with @using only - Fails

@inherits Nancy.ViewEngines.Razor.NancyRazorViewBase<dynamic>
@using uMentor.Extensions
<!DOCTYPE html>
<html lang="en">
    <head>
        <title>Test</title>   
    </head>
    <body>
        @RenderBody()        
    </body>
</html>

Therefore, the evidence suggests that placing a @using statement into a razor view or layout causes a razor compilation error that silently fails, returning the error message in the response body. Only if your test happens to check the body content will you find out there has been a problem.

Thank you for any help.

Answers


My first guess is to move the config from the web.config to the app.config. This worked for me, though I don't know why.

Also I love Nancy, it's brilliant, but the documentation sucks really. Because of this when I am developing Nancy apps, I hook it's source code into the project (only the relevant parts of course) and than when I face any problems I go into Nancy's source code and find the problem myself. The code is well written it's easy to read.

Cheers!


Need Your Help

Disable enumeration of properties in JavaScript without ECMAScript 5

javascript javascript-objects

With ECMAScript 5 we have Object.defineProperty, which allows us to describe how a property should respond to some actions. One of the possibilites from this is to disable enumeration of properties...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.