https://blog.jcorioland.io/archives/2014/04/01/using-owin-to-test-your-web-api-controllers.html 我看到的区别是使用 TestServer.Create 和 WebApp.Start< Startup> 主要区别是什么?为什么要选择一个?这仅仅是Web api调用的单元测试控制器方法与内存中的端到端集成测试之间的区别吗?解决方案 TestServer.Create< Startup>()-您可以使用启动文件仅启动内存中的实例.TestServer内部的HttpClient足以进行内存中的集成测试.我们将在一个进程内启动所有测试服务器,因此这不是一个限制(当前有4个测试服务器同时运行).当您这样做时 WebApp.Start< Startup>(Settings.WebApiUrl)-您在提供的URL上启动Web应用程序.还有另一个接受选项的重载:URL和设置.我们仅在特定情况下使用此选项.如: SignalR客户端的托管网址-如果没有该网址,它将无法正常工作,它可以在哪里运行基于合同的测试-验证提供商合同边.这也只能通过启动的WebApp来完成.(是使用Pact.Net)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.htmlThe 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? 解决方案 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 doWebApp.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 runContract based testing - verification of the contracts on providerside. This also can be done only through started WebApp. (We'reusing Pact.Net) 这篇关于为什么要使用OWIN TestServer?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 09-24 20:17