0

I've just created an empty web api project with .net core 2.0. I have a default controller and I want now create an integration test.

[Route("api/[controller]")]
public class ValuesController : Controller
{
    // GET api/values
    [HttpGet]
    public IEnumerable<string> Get()
    {
        return new string[] { "value1", "value2" };
    }
}

The goal is self host in the integration then enter url api/values and check the return.

NB: I only use wep api 2 and owin, and it was quite easy to do this. But the following link: https://learn.microsoft.com/en-us/aspnet/core/fundamentals/owin says that .net core application should not use owin, so what should we use and how to it?

4
  • why don't you call the methods from your controller? Commented Sep 6, 2017 at 19:19
  • because if I instanciate the controler and call the Get() directly it does not test the route and the fact that is call on a Http GET. That would be more a unit test. Commented Sep 6, 2017 at 19:29
  • This link could help you learn.microsoft.com/en-us/aspnet/web-api/overview/… Commented Sep 7, 2017 at 4:10
  • Thanks but not really as it's for Web API 2 and it's UnitTesting. I'm interested by Integration Tests for .net core 2.0. Commented Sep 7, 2017 at 6:09

1 Answer 1

5

Take a look at the official documentation for integration testing with ASP.NET Core 2.0: https://learn.microsoft.com/en-us/aspnet/core/testing/integration-testing

The main idea is to create a TestServer instance with a WebHostBuilder configured with your Startup class. You can then get a HttpClient instance from the TestServer to call your self-hosted api

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

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.