2013-02-28 65 views
3

我想不通为什么这个异常被抛出...... 我有一个单元测试:序列包含多个元素的NancyBootstrapperBase类

[Test] 
public void Should_return_status_ok_when_route_exists() 
{ 
    // Given 
    var bootstrapper = new DefaultNancyBootstrapper(); 
    var browser = new Browser(bootstrapper); 

    // When 
    var result = browser.Get("/", with => 
             { 
              with.HttpRequest(); 
             }); 
    // Then 
    Assert.AreEqual(HttpStatusCode.OK, result.StatusCode); 

} 

虽然浏览器变量被分配,除了在扔与后续堆栈跟踪南希引导程序的基类

System.InvalidOperationException:序列包含多个元素

at System.Linq.Enumerable.SingleOrDefault(IEnumerable`1 source) 
at Nancy.Bootstrapper.NancyBootstrapperBase`1.GetRootPathProvider() in NancyBootstrapperBase.cs: line 558 
at Nancy.Bootstrapper.NancyBootstrapperBase`1.get_RootPathProvider() in NancyBootstrapperBase.cs: line 172 
at Nancy.Bootstrapper.NancyBootstrapperBase`1.GetAdditionalInstances() in NancyBootstrapperBase.cs: line 514 
at Nancy.Bootstrapper.NancyBootstrapperBase`1.Initialise() in NancyBootstrapperBase.cs: line 242 
at Nancy.Testing.Browser..ctor(INancyBootstrapper bootstrapper) in Browser.cs: line 39 
at Tests.Tests.Should_return_status_ok_when_route_exists() in Tests.cs: line 34 
+0

您是否正在运行您应用程序所在的项目中的Nancy.Testing?如果不是,那么你是否参考了也引用Nancy.Testing的项目中的主机程序集(Nancy.Hosting.xxxx)? – TheCodeJunkie 2013-02-28 17:51:46

+0

我没有跑Nancy.Testing。我应该这样做吗?我只是添加了Nancy,Nancy.Authentication.Forms,Nancy.Testing对我的测试项目的引用,并运行我在VS中使用Resharper编写的测试。这是不正确的? – 2013-03-01 09:55:07

+0

如果您只在运行测试时引用了Nancy,Nancy.Authentication.Forms和Nancy.Testing并且遇到了这个问题,那么我们需要了解有关您的项目设置的更多信息,以了解它发生的原因。代码是否可用? – TheCodeJunkie 2013-03-01 10:49:00

回答

1

我最近有同样的问题。我试图通过像这样解析Nancy模块来测试我的引导程序:

[Test] 
public void PushModule_Is_Resolvable() 
{ 
    var pushModule = mUnderTest.GetModule(typeof (PushModule), new NancyContext()); 
    Assert.That(pushModule, Is.Not.Null); 
} 

这产生了问题中描述的异常。在阅读克里斯的回答后,我刚刚配置该DLL不会被复制到Visual Studio中的输出文件夹:

  • 在测试的项目中找到Nancy.Hosting.Self引用。
  • 右键单击引用并选择“属性”。
  • Set 将本地复制复制到false
  • 清理并重建您的解决方案。

这解决了我的问题。 我会张贴此评论克里斯的答案,但因为这是我的第一篇文章,我只能张贴答案。

1

我已经用Nancy 0.16.1在一个新的测试程序集上看到了这一点,该程序集引用了Nancy,Nancy.Testing和项目引用(在VS2010中)到包含我的模块的主要服务程序集。

服务组件引用Nancy.Hosting.Self。

测试程序集的内部版本将Nancy.Hosting.Self.dll拖到构建文件夹中,并且测试失败,并显示您描述的错误。

手动移除生成文件夹中的主机DLL解决了错误,测试变为绿色,例如,删除MyTests\bin\Debug\Nancy.Hosting.Self.dll

1

我在测试一个引用Nancy.Hosting.Self的项目时经历过同样的情况。

我的解决方法是创建一个CustomBootstrapper并覆盖RootPathProvider属性。

class TestBootStrapper : DefaultNancyBootstrapper{ 
    protected override IRootPathProvider RootPathProvider { 
     get { 
      return new FakeRootPathProvider(); 
     } 
    } 
} 
相关问题