当剃刀布局中包含@using语句时,为什么Nancy.Testing失败? [英] Why does Nancy.Testing fail when @using statements are included in razor layouts?

查看:123
本文介绍了当剃刀布局中包含@using语句时,为什么Nancy.Testing失败?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在上一个问题中 Nancy.Testing支持嵌套的Razor视图吗?我概述了Nancy部门遇到的困难当呈现嵌套剃刀布局时,测试失败.

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.

经过调查,我现在可以完善该信息.问题与嵌套无关,其原因很简单:如果在剃刀布局或视图中包含@using语句,则从单元测试中访问时,Nancy将找不到引用的命名空间.

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.

例如:

主版面

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

查看

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

如您所料,这可以正常工作.但是,如果现在我们在主布局中添加了一些服务器端处理,并因此需要使用@using语句,则单元测试将失败(代码正常运行,只有单元测试失败):

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:

错误编译模板:(15,11)找不到类型或名称空间名称'uMentor'.

具有服务器端代码的主版面

@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>

我确保我的web.config(网站项目和单元测试项目)都具有提到的正确的剃刀程序集和名称空间:

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>

将所有内容剥离一遍,我可以构建最简单的"hello world"视图->布局,其中不包含任何内容,无需任何代码.单元测试通过(响应主体中包含正确的html).然后,我在视图或布局的顶部放置了一个多余的@using语句,并且测试失败(响应主体包含上面的错误消息).

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).

仅使用@的主版式-失败

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

因此,有证据表明,在剃刀视图或布局中放置@using语句会导致剃刀编译错误,该错误无提示地失败,并在响应正文中返回错误消息.仅当您的测试碰巧检查身体内容时,您才发现存在问题.

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.

谢谢您的帮助.

推荐答案

我的第一个猜测是将配置从web.config移至app.config. 这对我有用,尽管我不知道为什么.

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.

我也喜欢Nancy,它很棒,但是文档确实很烂.因此,在开发Nancy应用程序时,我将其源代码挂接到了项目中(当然只是相关部分),而遇到任何问题时,我都会进入Nancy的源代码中并自己找到问题.代码写得很好,很容易阅读.

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.

干杯!

这篇关于当剃刀布局中包含@using语句时,为什么Nancy.Testing失败?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆