为什么`DefaultNancyBoostrapper`找不到我的NancyModule

sco*_*coe 8 unit-testing nancy

我只是在南希弄湿了脚.我很高兴看到Wiki中的测试过程,但是当我尝试以下操作时,我无法通过测试来完成测试.

使用VS2010

  1. 创建Empty ASP.NET Web Application Project:Notify.App
  2. Install-Package Nancy.Hosting.AspNet
  3. 创建简单模块,如下所示:NotifyModule
  4. 创建Class Library项目:Notify.UnitTests
  5. Install-Package Nancy.Testing
  6. Install-Package XUnit
  7. 创建简单的第一个测试:BaseUrlSpec.cs

使用DefaultNancyBootstrapper测试失败了HttpStatusCode.NotFound.

如果我用以下内容替换bootstrapper定义:

var bootstrapper = new ConfigurableBootstrapper(
                          with => 
                             with.Module<NotifyModule>());
Run Code Online (Sandbox Code Playgroud)

然后测试通过. 我不明白为什么使用DefaultNancyBootstrapper的SDHP不起作用?我做错了什么让它破裂,还是我在理解中遗漏了细节?


NotifyModule

using Nancy;
public class NotifyModule : NancyModule {
    public NotifyModule() {
        Get["/"] = _ => HttpStatusCode.OK;
    }
}
Run Code Online (Sandbox Code Playgroud)

BaseUrlSpec

using Nancy;
using Nancy.Testing;
using Notify.App;
using Xunit;
public class BaseUrlSpec
{
    [Fact]
    public void ShouldRespondOk()
    {
        var bootstrapper = new DefaultNancyBoostrapper();
        var app = new Browser(bootstrapper);
        var response = app.Get("/", with => with.HttpRequest());
        var statusCode = response.StatusCode;
        Assert.Equal(HttpStatusCode.OK, statusCode);
    }
}
Run Code Online (Sandbox Code Playgroud)

Chr*_*dal 7

您需要确保加载包含您的路由的程序集.引用程序集中的类型可确保这一点,因此使用可配置引导程序的版本可以正常工作.要使另一个工作,只需在程序集中添加对某些类型的引用.无需实例化它.

  • 这与南希没有关系.在某些情况下,.net编译器将尝试智能,并且不在程序集清单中包含未使用类型的程序集.如果要使用this.GetType().Assembly.GetReferencedAssemblies()检查测试项目程序集,则不会看到包含模块的程序集(除非您明确使用了该类型).在0.17中,我们尝试通过加载\ bin中的所有内容并引用Nancy*程序集来解决此问题. (3认同)