【问题标题】:Sequence contains more than one element an NancyBootstrapperBase class序列包含多个元素 NancyBootstrapperBase 类
【发布时间】:2013-06-25 16:48:49
【问题描述】:

我不知道为什么会抛出这个异常...... 我有一个单元测试:

[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);

}

在分配浏览器变量时,异常在 Nancy Bootstrapper Base 类中引发了跟踪堆栈跟踪

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

【问题讨论】:

  • 您是否从您的应用程序所在的同一个项目中运行 Nancy.Testing?如果不是,那么您是否在引用 Nancy.Testing 的项目中引用了您的托管程序集 (Nancy.Hosting.xxxx)?
  • 我根本没有运行 Nancy.Testing。我应该这样做吗?我刚刚在我的测试项目中添加了 Nancy、Nancy.Authentication.Forms、Nancy.Testing 引用,并运行了我在 VS 中使用 Resharper 编写的测试。不正确?
  • 如果您只引用了 Nancy、Nancy.Authentication.Forms 和 Nancy.Testing 并在运行测试时遇到了这个问题,那么我们需要更多地了解您的项目设置以了解它发生的原因。代码在任何地方都可用吗?
  • 我在“Instant Nancy Web Development [eBook]”第 4 章代码中有这个问题,但是这本书和代码有 copylaws :/
  • 好的。就我而言,解决方案是将所有使用的 nuget 包更新到最新版本(打开 nuget 表单管理器,然后转到“更新”选项卡并更新您的 nuget;))

标签: unit-testing exception nancy


【解决方案1】:

我最近遇到了同样的问题。我正在通过尝试像这样解析 Nancy 模块来测试我的引导程序:

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

这产生了问题中描述的异常。在阅读了 Chris 的回答后,我刚刚将该 dll 配置为不复制到 Visual Studio 中的输出文件夹:

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

这为我解决了这个问题。 我会将此作为对 Chris 答案的评论发布,但由于这是我的第一篇文章,我只能发布答案。

【讨论】:

    【解决方案2】:

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

    服务程序集引用 Nancy.Hosting.Self。

    测试程序集的构建将 Nancy.Hosting.Self.dll 拉入构建文件夹,测试失败并出现您描述的错误。

    手动从构建文件夹中删除 Hosting dll 可以解决错误并且测试变为绿色,例如删除MyTests\bin\Debug\Nancy.Hosting.Self.dll

    【讨论】:

      【解决方案3】:

      我在测试一个引用 Nancy.Hosting.Self 的项目时也遇到了同样的情况。

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

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

      【讨论】:

        猜你喜欢
        • 2015-01-21
        • 2014-03-09
        • 2013-02-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多