Skip to main content
 首页 » 编程设计

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

2024年12月31日5tuyile006

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

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