unit-testing - 序列包含一个以上的元素 NancyBootstrapperBase 类

标签 unit-testing exception nancy

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

[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 基类中抛出并跟踪堆栈跟踪

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 模块来测试我的 Bootstrap :

[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 回答的评论发布的,但由于这是我的第一篇文章,我只能发布答案。

关于unit-testing - 序列包含一个以上的元素 NancyBootstrapperBase 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15138994/

相关文章:

.net - 如何在PresentationFramework中调试NullReferenceException?

razor - (Nancy) Razor View Engine 部分 View 渲染完整 HTML

c# - 从 nancy bootstrapper 渲染 View ?

java - Jmockit:如何验证在特定实例上调用了方法

c# - 如何对 ViewComponent.Invoke() 进行单元测试?

PHPUnit - 为什么 PHPUnit 似乎在严格模式下运行?

php - 运行 phpunit 时未找到 fatal error 'File/Iterator/Autoload.php'

php - 在文件未上传的 PHP 中尝试 catch 语句

android - 为什么我在使用旧版 android 的 ActionBar Sherlock 时会在 onCreate() 出现错误?

c# - 为什么没有查询参数传递到我的 NancyFX 模块?