How do I run in-memory integration tests using xUnit.net and ASP.NET 5?

大憨熊 提交于 2019-12-11 11:26:54

问题


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



来源:https://stackoverflow.com/questions/29968788/how-do-i-run-in-memory-integration-tests-using-xunit-net-and-asp-net-5

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!