将 Nancy.Testing 与 NUnit 测试运行程序一起使用时出现 TypeInitializationException

TypeInitializationException when using Nancy.Testing with NUnit test runner

我正在通过 NuGet 使用 NancyNancy.Testing 的 1.2 版编写测试。我的测试是用 NUnit 2.6.4 编写的,看起来像这样:

[Test]
public async Task ShouldReturnSuccessfullyAuthenticatedUser()
{
    // arrange
    var request = CreateRequest();
    var userDocument = CreateUserDocumentFrom(request);
    await userRepository.AddAsync(userDocument);

    // act
    var response = browser.Post(Paths.Login, with => with.JsonBody(request));

    // assert
    response.StatusCode.Should().Be(HttpStatusCode.OK);
}

我有这个例外:

System.TypeInitializationException : The type initializer for 'Nancy.Bootstrapper.AppDomainAssemblyTypeScanner' threw an exception.
  ----> System.IO.DirectoryNotFoundException : Could not find a part of the path 'E:\myProject\bin\Debug\bin\Debug'.

而且我认为将 Nancy.TestingNUnit 一起使用时有问题,因为 xUnit 中的等效测试运行得很好。

看起来 NUnit 正在尝试自动将 bin\Debug 添加到 bin 路径。我解决它的方法是明确指定要使用哪种 bin 路径。这是 .nunit 项目文件:

<NUnitProject>
  <Settings activeconfig="Debug" />
  <Config name="Debug" binpathtype="Manual">
    <assembly path="bin/Debug/MyUnitTests1.dll" />
    <assembly path="bin/Debug/MyUnitTests2.dll" />
  </Config>
  <Config name="Release" binpathtype="Manual">
    <assembly path="bin/Release/MyUnitTests1.dll" />
    <assembly path="bin/Release/MyUnitTests2.dll" />
  </Config>
</NUnitProject>

XML 属性 NUnitProject\Config\binpathtype 之前的值为 Auto。当我如上所述将其更改为 Manual 时,异常消失并且我的测试 运行 成功。