Why use OWIN TestServer?
Asked Answered
R

1

8

This article shows how to host an entire web API stack in memory for testing using OWIN:

http://www.davidwhitney.co.uk/Blog/2015/01/07/testing-an-asp-net-webapi-app-in-memory/

Whereas this article shows using the OWIN TestServer to unit test controllers:

https://blog.jcorioland.io/archives/2014/04/01/using-owin-to-test-your-web-api-controllers.html

The difference I see is between the use of TestServer.Create and WebApp.Start<Startup>

What is the key difference and why would you choose one over the other?

Is it simply the difference between unit testing controller methods as web api calls versus end-to-end integration testing in memory?

Rupp answered 7/9, 2017 at 5:12 Comment(2)
I am trying to design a microservices solution and wondering about the same question... did you find any more information meanwhile?Loria
No, I didn't, I'm afraid.Rupp
O
6

When you do TestServer.Create<Startup>() - you start just the in-memory instance using your startup file. The HttpClient that is inside TestServer is enough for integration testing in-memory. We are starting all the testservers inside one process, so this is not a limitation (currently 4 test servers are running together).

When you do WebApp.Start<Startup>(Settings.WebApiUrl) - you start a web app on the url you provide. There is also another overload which accepts options: both urls and settings. We are using this option only for specific cases. Such as:

  1. Hosting url for SignalR client - it won't work without the URL, where it could run
  2. Contract based testing - verification of the contracts on provider side. This also can be done only through started WebApp. (We're using Pact.Net)
Oscillogram answered 22/1, 2018 at 14:29 Comment(1)
This explanation saves my day. When you say specific cases, is hosting web socket client included? I used TestServer.Create<Startup>() initially and I can get the web socket client connected. After switching to WebApp.Start<Startup>(Settings.WebApiUrl) eveything works like a charm. #63591961Bonaire

© 2022 - 2024 — McMap. All rights reserved.