使用Nancy.Testing与NUnit测试运行器时的TypeInitializationException

时间:2015-05-07 20:58:44

标签: c# .net nunit nancy

我正在使用Nancy的版本1.2和Nancy.Testing通过NuGet编写测试。我的测试用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中的等效测试运行得很好。

1 个答案:

答案 0 :(得分:2)

看起来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时,异常消失,我的测试成功运行。