Search code examples
c#.netnunitnancy

TypeInitializationException when using Nancy.Testing with NUnit test runner


I am writing a test using version 1.2 of Nancy and Nancy.Testing via NuGet. My test is written with NUnit 2.6.4 and looks like this:

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

I have this exception:

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'.

And I think there's something wrong when using Nancy.Testing with NUnit, because the equivalent test in xUnit runs just fine.


Solution

  • It looked like NUnit was tryging to automatically add bin\Debug to the bin path. The way I solved it is to specify explicitly what kind of bin path to use. Here is the .nunit project file:

    <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>
    

    The XML attribute NUnitProject\Config\binpathtype had previously a value of Auto. When I changed it to Manual as above the exception was gone and my tests ran successfully.