Automate Integration testing with ASP.NET In- memory Hosting(.net Framework)

Piyathida San-aoun
2 min readDec 28, 2020

--

Because, I have started to create a new integration test project and after I check The Project API, they implemented and configure Web API by calling GlobalConfiguration.Configure in the Application_Start method.

https://docs.microsoft.com/en-us/aspnet/web-api/overview/advanced/configuring-aspnet-web-api#webhost

So, For the Integration test, how we start, and can call API.

The configuration of the in-memory Web API host is almost the same as configuring self-host. We need to specify the routes to be used and that is done through the familiar HttpConfiguration. Server is instantiated by passing the configuration to the HttpServer constructor.

var config = new HttpConfiguration();config.DependencyResolver = new UnityDependencyResolver(UnityConfig.GetUnityContainer()); WebApiConfig.Register(config);
config.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always;
var _server = new HttpServer(config);

Now, I will be testing and get items from the repository via GET

var client = new HttpClient(_server);HttpResponseMessage response = client.GetAsync("http://localhost:53267/api/Emails").Result;Assert.NotNull(response.Content);
Assert.Equal(response.StatusCode.ToString(), "OK");

Thank you for reading and I hope, my content can help and if your project start API by Owin self-host you can look at my other content at link:

--

--

Piyathida San-aoun
Piyathida San-aoun

Written by Piyathida San-aoun

Software Developer Engineer In Test

No responses yet