为什么要使用OWIN TestServer? [英] Why use OWIN TestServer?

查看:97
本文介绍了为什么要使用OWIN TestServer?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

本文介绍了如何在内存中托管整个Web API堆栈以使用OWIN进行测试:

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和设置.我们仅在特定情况下使用此选项.如:

  1. SignalR客户端的托管网址-如果没有该网址,它将无法正常工作,它可以在哪里运行
  2. 基于合同的测试-验证提供商合同边.这也只能通过启动的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.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?

解决方案

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)

这篇关于为什么要使用OWIN TestServer?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆