Web API中的单元测试自定义路由 [英] Unit test Custom Route in Web API

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

问题描述

我已经花了半天时间,没有任何成功.您如何在Web API中对测试路由进行单元化?

I've spent already half day on this without any success. How do you unit test routes in Web API?

我的意思是给出以下URI:

I mean given the following URI:

~/Test/Get/2

我想对Test控制器捕获上述URL进行单元测试,并接受一个接受int参数的Get动作.

I want to unit test that the above url is captured by the Test controller and Get action accepting an int parameter.

推荐答案

为了测试路线,我创建了一个库来帮助您确定这些断言-MyWebApi:

For testing the routes, I have created a library to help you with the assertions - MyWebApi: https://github.com/ivaylokenov/MyWebApi

基本上,您可以执行以下操作:

Basically, you can do the following:

MyWebApi
    .Routes()
    .ShouldMap("api/WebApiController/SomeAction/5")
    .WithJsonContent(@"{""SomeInt"": 1, ""SomeString"": ""Test""}")
    .And()
    .WithHttpMethod(HttpMethod.Post)
    .To(c => c.SomeAction(5, new RequestModel
    {
        SomeInt = 1,
        SomeString = "Test"
    }));

如果要自己实现,可以在此处查看代码并复制它: https://github.com/ivaylokenov/MyWebApi/blob/master/src/MyWebApi/Utilities/RouteResolvers/InternalRouteResolver.cs

If you want to implement it yourself, you can see the code here and copy it: https://github.com/ivaylokenov/MyWebApi/blob/master/src/MyWebApi/Utilities/RouteResolvers/InternalRouteResolver.cs

这篇关于Web API中的单元测试自定义路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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