南希模块单元测试。单元测试中的例外情况

时间:2013-03-06 10:39:36

标签: c# unit-testing liquid nancy

找不到测试不起作用的原因。有人知道为什么会这样吗?这两个测试之间的区别在于视图。首先是“.html”页面,第二个是“.liquid”。在我的项目中,我使用“.liquid”,因此“.html”仅用于测试正确的工作测试。 我有一个南希模块

public sealed class Module : NancyModule
{
    public Module(IBackend storage)
    {
        Get["/"] = _ => View["Create.liquid"];     
        Get["/Test"] = _ => View["TestHtml.html"];
    }
}

测试

[Test]
public void test_html()
{
    // Given
    var bootstrapper = new ConfigurableBootstrapper(with =>
    {
        var module = new Module(new Endpoint());
        with.Module(module);
    });
    browser = new Browser(bootstrapper);

    // When
    var result = browser.Get("/Test", with =>
    {
        with.HttpRequest();
    });

    // Then
    Assert.AreEqual(HttpStatusCode.OK, result.StatusCode);
}

[Test]
public void test_liquid()
{
    // Given
    var bootstrapper = new ConfigurableBootstrapper(with =>
    {
        var module = new Module(new Endpoint());
        with.Module(module);
    });
    browser = new Browser(bootstrapper);

    // When
    var result = browser.Get("/", with =>
    {
        with.HttpRequest();
    });

    // Then
    Assert.AreEqual(HttpStatusCode.OK, result.StatusCode);
}

第二次测试有例外

System.Exception : ConfigurableBootstrapper Exception
----> Nancy.RequestExecutionException : Oh noes!
----> Nancy.ViewEngines.ViewNotFoundException : Unable to locate view 'Create.liquid'
Currently available view engine extensions: sshtml,html,htm
Locations inspected: ,,,,,,,,views/Module/Create.liquid-en-    US,views/Module/Create.liquid,Module/Create.liquid-en-US,Module/Create.liquid,views/Create.liquid-en-US,views/Create.liquid,Create.liquid-en-US,Create.liquid
Root path: D:\Projects\epm-vsp-pasta\Tests\bin\Debug

使用堆栈跟踪

at Nancy.Testing.PassThroughStatusCodeHandler.Handle(HttpStatusCode statusCode, NancyContext context) in d:\Nancy-master\src\Nancy.Testing\PassThroughStatusHandler.cs: line 22
at Nancy.NancyEngine.CheckStatusCodeHandler(NancyContext context) in d:\Nancy-master\src\Nancy\NancyEngine.cs: line 219
at Nancy.NancyEngine.HandleRequest(Request request, Func`2 preRequest) in d:\Nancy-master\src\Nancy\NancyEngine.cs: line 112
at Nancy.NancyEngine.HandleRequest(Request request) in d:\Nancy-master\src\Nancy\NancyEngine.cs: line 77
at Nancy.Testing.Browser.HandleRequest(String method, String path, Action`1 browserContext) in d:\Nancy-master\src\Nancy.Testing\Browser.cs: line 125
at Nancy.Testing.Browser.Get(String path, Action`1 browserContext) in d:\Nancy-master\src\Nancy.Testing\Browser.cs: line 62
at Tests.TestModule.test_liquid() in TestModule.cs: line 111 --RequestExecutionException
at Nancy.NancyEngine.InvokeOnErrorHook(NancyContext context, ErrorPipeline pipeline, Exception ex) in d:\Nancy-master\src\Nancy\NancyEngine.cs: line 272
--ViewNotFoundException    

3 个答案:

答案 0 :(得分:2)

这与.NET程序集加载有关。由于Nancy.ViewEngines.Dotliquid程序集中没有直接使用类型,因此.NET编译器认为它可以是智能的,并且不包括程序集元数据中的引用。这导致程序集在运行时根本没有加载到应用程序域中。

它适用于.html文件的原因是管理.html扩展名的SuperSimpleViewEngine内置于Nancy.dll中并且已加载。

您可以通过显式使用程序集中的类型来解决此问题,例如在测试代码中添加类似var foo = typeof(DotLiquidViewEngine)的内容,使用可配置引导程序设置上的ViewEngine<DotLiquidViewEngine>()属性。

对于我们的下一个版本0.17,我们通过扫描“bin”文件夹中的程序集并显式加载任何引用Nancy *程序集的程序集,添加了尽可能减少此效果的代码。

希望这有帮助

答案 1 :(得分:1)

看起来您没有从测试项目中引用液体视图引擎:

  

目前可用的视图引擎扩展:sshtml,html,htm

答案 2 :(得分:0)

如果页面没有复制到输出目录,我发现会发生这种情况。

即。检查Visual Studio中.liquid文件的属性,并确保它具有相同的&#39;复制到输出目录&#39;设置为工作.html文件。

相关问题