3

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.

2 Answers 2

5

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

Sign up to request clarification or add additional context in comments.

1 Comment

I am currently attempting to do this integration test with ASP.NET 5 beta7 and I found the problem with my controllers cannot found the views because they attempt to search them within the test project instead on the web project. Anyone had face this issue?
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.