I have a test project that integration tests an ASP.NET MVC Core API service using the
Microsoft.AspNetCore.TestHost package. The tests fail when run in NCrunch: every HTTP request issued by a client is responded to with a 404 Not Found.
Microsoft.AspNetCore.TestHost provides an in-memory implementation of a web server, so that HTTP requests can be tested without needing to allocate a port from the network stack, etc. Tests using this package run quickly and reliably, meaning they can be run quite happily in NCrunch even though they're integration tests.
This is easily reproducible; another test project for a different, smaller API exhibits the same behaviour. Here is a sample test:
Code:
// Place in an ASP.NET Core project
// Required NuGet packages:
// - Microsoft.AspNetCore.Mvc
public class TestController : Controller
{
[HttpGet("/")]
public IActionResult Test()
{
return Ok();
}
}
// Place in an Xunit Test project
// Required NuGet packages:
// - Microsoft.AspNetCore.TestHost
// - Microsoft.AspNetCore.Mvc
public class TestHostFailure
{
private const string ControllerUrl = "/";
[Fact]
public async Task TestHost_Does_Not_Work_In_NCrunch()
{
var server = new TestServer(new WebHostBuilder()
.Configure(app =>
{
app.UseMvc();
})
.ConfigureServices(services =>
{
services.AddMvc();
}));
var client = server.CreateClient();
using (var response = await client.GetAsync(ControllerUrl))
{
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.True(response.IsSuccessStatusCode);
}
}
}
The tests only fail in NCrunch; however, the tests
pass if the TestController class is defined in the test project.