How do I run in-memory integration tests using xUn

2019-08-14 12:27发布

问题:

I've already got unit tests working fine, but would like to do in memory hosting and use HttpClient for integration tests. Can't seem to find to much information about this for ASP.NET 5.

回答1:

Ended up using the following package instead of Kestrel:

Microsoft.AspNet.TestHost package

Example how I use it against my app:

[Fact]
public async void GetShouldReturnTwoValues()
{
  var server = TestServer.Create(app =>
  {
    var env = app.ApplicationServices.GetRequiredService<IHostingEnvironment>();
    new Startup(env).Configure(app, env);
  }, services => services .AddMvc());

  var result = await server.CreateClient().GetAsync("api/values/");

  Assert.True(result.IsSuccessStatusCode);
}

Repo:

https://github.com/aspnet/Hosting

Usage / Documentation:

https://github.com/aspnet/Hosting/blob/master/test/Microsoft.AspNetCore.TestHost.Tests/TestServerTests.cs



回答2:

I stumbled upon the same question. So I want to add for completeness that the name of the NuGet package changed to Microsoft.AspNetCore.TestHost.

The correct documentation: https://docs.asp.net/en/latest/testing/integration-testing.html