【问题标题】:Nancy Module Unit Testing. Exception in unit test南希模块单元测试。单元测试中的异常
【发布时间】:2013-02-21 02:34:46
【问题描述】:

找不到测试不起作用的原因。有人知道为什么会这样吗?这两个测试之间的区别仅在于视图。第一个是“.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    

【问题讨论】:

    标签: c# unit-testing liquid nancy


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

      您似乎没有从您的测试项目中引用液体视图引擎:

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

      【讨论】:

      • 但我的测试 html 页面与所有流动视图位于同一文件夹中。而且我从我的测试中得到它没有任何问题......或者我不明白你?
      • 你不明白我的意思。您需要添加对液体程序集的引用或将其 nuget 添加到您的测试项目中。
      • 喜欢 1 个答案?我将 DotLiquid 和 Nancy.ViewEngines.DotLiquid 组件添加到了我的测试项目中,并按照他在第一个答案中所说的进行了操作,但不幸的是,所有测试都因这些异常而失败。
      【解决方案3】:

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

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

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

      对于我们的下一个版本 0.17,我们添加了代码以尽可能减少这种影响,方法是扫描“bin”文件夹中的程序集并显式加载任何引用 Nancy* 程序集的程序集。

      希望对你有帮助

      【讨论】:

      • 我按照你的建议去做,但现在我所有的测试都失败了,出现了这个异常 System.TypeLoadException:无法从程序集“Nancy,Version=0.16.1.0,Culture=”加载类型“Nancy.IStaticContentProvider”中立,PublicKeyToken=null'。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-01-14
      • 2011-06-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-10
      • 1970-01-01
      相关资源
      最近更新 更多