如何使用 xUnit.net 和 ASP.NET 5 运行内存中集成测试?

nik*_*aia 3 xunit.net asp.net-core

我已经让单元测试工作正常,但想在内存托管中进行并使用 HttpClient 进行集成测试。似乎找不到关于 ASP.NET 5 的太多信息。

nik*_*aia 5

最终使用以下包而不是 Kestrel:

Microsoft.AspNet.TestHost 包

我如何在我的应用程序中使用它的示例:

[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);
}
Run Code Online (Sandbox Code Playgroud)

回购协议:

https://github.com/aspnet/Hosting

用法/文档:

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