你怎么能单元测试的ASP.NET Web API路由? [英] How can you unit test ASP.NET Web API routing?

查看:160
本文介绍了你怎么能单元测试的ASP.NET Web API路由?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试着写一些单元测试,以确保我的Web API的请求被路由到与预期参数预期的API控制器动作。

I'm trying to write some unit tests to ensure that requests made to my Web API are routed to the expected API controller action with the expected arguments.

我已经试过创建使用的HttpServer 类的测试,但我从服务器获取500响应,并没有信息调试问题。

I've tried to create a test using the HttpServer class, but I get 500 responses from the server and no information to debug the problem.

有没有一种方法来创建一个ASP.NET Web API站点的路由选择的单元测试?

Is there a way to create a unit tests for the routing of an ASP.NET Web API site?

在理想情况下,我想创建使用请求的HttpClient 键,让服务器处理请求并通过它通过预期的路由过程。

Ideally, I'd like to create a request using HttpClient and have the server handle the request and pass it through the expected routing process.

推荐答案

我已经写了有关测试路线和做几乎你问什么是博客文章:

I have written a blog post about testing routes and doing pretty much what you are asking about:

http://www.strathweb.com/2012/08/testing-routes-in-asp-net -Web-API /

希望它帮助。

另一个优势是,我用反射提供的操作方法 - 所以不是字符串使用的路线,你在一个强类型的方式添加。通过这种方法,如果你的动作名称不断变化,测试将无法编译,所以你会很容易就能发现错误。

Additional advantage is that I used reflection to provide action methods - so instead of using routes with strings, you do add them in a strongly typed manner. With this approach, if your action names ever change, the tests won't compile so you will easily be able to spot errors.

这篇关于你怎么能单元测试的ASP.NET Web API路由?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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