问题
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?
回答1:
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:
- Hosting url for SignalR client - it won't work without the URL, where it could run
- Contract based testing - verification of the contracts on provider side. This also can be done only through started WebApp. (We're using Pact.Net)
来源:https://stackoverflow.com/questions/46088291/why-use-owin-testserver