Why use OWIN TestServer?

点点圈 提交于 2021-02-07 12:53:52

问题


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:

  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)


来源:https://stackoverflow.com/questions/46088291/why-use-owin-testserver

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!