Nancy.Testing是否支持嵌套的Razor视图? [英] Does Nancy.Testing support nested Razor views?

查看:103
本文介绍了Nancy.Testing是否支持嵌套的Razor视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个单元测试,尝试使用不正确的凭据登录并检查生成的响应正文中是否存在某些特定的错误框" html.这样就可以了.

I have a unit test that attempts to sign-in with incorrect credentials and checks the resulting response body for some specific 'errorbox' html. This works just fine.

[Fact]
public void SignIn__Should_display_error_message_when_error_passed()
{
    var browser = Fake.Browser();
    var response = browser.Get("/signin", with => with.Query("error", "true"));

    response.Body["#errorBox"]
            .ShouldExistOnce()
            .And.ShouldBeOfClass("floatingError")
            .And.ShouldContain("invalid", StringComparison.InvariantCultureIgnoreCase);
}

我的页面变得有些重复,所以我重新组织了一些事情以创建一组简单的嵌套剃刀视图,如下所示:

My pages were getting a bit repetitious so I re-organised things to create a simple set of nested razor views, like this:

登录视图

@inherits Nancy.ViewEngines.Razor.NancyRazorViewBase<dynamic>
@{
    Layout = "_Master.cshtml";
}
<h3>Sign In</h3>
<form class="nice" method="POST">
    ... [labels, inputs etc]
</form> 
@if (Model.HasError)
{
  <div id="errorBox" class="floatingError">Invalid UserName or Password</div>
}

_Master视图

@inherits Nancy.ViewEngines.Razor.NancyRazorViewBase<dynamic>
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8" />
        <title>@Model.Title</title>  
        ... [css javascript etc]
        @RenderSection("Head", required: false)      
    </head>

    <body>
        @RenderBody()        
    </body>
</html>

这仍然有效,并且我的单元测试仍然通过(与我一起忍受).时间的流逝和代码变得更加复杂,所以现在我需要重新组织.我在剃刀视图中添加了另一层嵌套.

This still works and my unit test still passes (bear with me). Time passes and the code gets more complex, so now I need to reorganise again. I have added another layer of nesting to my razor views.

_页面视图

@inherits Nancy.ViewEngines.Razor.NancyRazorViewBase<dynamic>
@{
    Layout = "_Master.cshtml";
}
@section Head{
    [... shared javascript, css]
    @RenderSection("Head", required: false)  
}
[.. some shared content]
@RenderBody()   

双重嵌套如下所示:

登录-> _页面-> _Master

现在我的单元测试失败,因为响应中不包含正确的html.在真实的浏览器(chrome)中运行代码并手动提供不正确的凭据会产生正确的响应,只有单元测试失败.

Now my unit test fails as the response does not contain the correct html. Running the code in an real browser (chrome) and manually supplying incorrect credentials produces the correct response, it is only the unit-test that fails.

向下钻取至response.Body.responseDocument.agilityPackDocumentInternal.DocumentNode.InnerHtml会显示此异常:

Drilling down to response.Body.responseDocument.agilityPackDocumentInternal.DocumentNode.InnerHtml shows this exception:

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

将SignIn视图重新指向使用_Master作为其布局(切出中间的_Page视图)会使单元测试再次通过.

Re-pointing the SignIn view back to using _Master as its layout (cutting out the middle _Page view) makes the Unit Test pass again.

但是_Page视图在代码方面没有执行任何操作,它只是添加了一些<script><style>标记并传递了接力棒.为了测试这些标签是否是问题,我将它们移至_Master视图,并将嵌套保持在一个级别(SignIn-> _Master),并通过了测试.

Yet the _Page view does nothing code-wise, it just adds some <script> and <style> tags and passes on the baton. To test if those tags are the problem, I shifted them to the _Master view and kept the nesting at one level (SignIn -> _Master) and the test passed.

那么,Nancy.Testing是否支持两个级别的剃须刀布局嵌套?否则,有人可以发现我的错误吗?

So, does Nancy.Testing support two levels of razor layout nesting? Otherwise, can anybody spot my mistake?

谢谢

PS 我不知道它是否已连接,但我怀疑可能是视图缓存(阅读

P.S. I don't know if it is connected but I suspected that view caching might be the problem (after reading Test driving Nancy Modules) I checked the value of Nancy.StaticConfiguration.DisableCaches and sure enough, it was set to false (despite my DEBUG symbol being switched on and running the test in Debug mode). I set this value to true in my custom unit test BootStrapper class but this made no difference.

protected override void ConfigureApplicationContainer(TinyIoCContainer container)
{
    base.ConfigureApplicationContainer(container);
    Nancy.StaticConfiguration.DisableCaches = true;
}

推荐答案

这个问题的答案可以在我的后续问题中找到:

The answer to this question can be found in my follow up question:

为什么Nancy.当剃刀布局中包含@using语句时,测试会失败?

这篇关于Nancy.Testing是否支持嵌套的Razor视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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