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