Fra*_*zzi 17 c# asp.net unit-testing asp.net-web-api
我想测试一下这个控制器:
[HttpGet]
public IList<Notification> GetNotificationsByCustomerAndId([FromUri] string[] name, [FromUri] int[] lastNotificationID)
{
return _storage.GetNotifications(name, lastNotificationID, _topX);
}
Run Code Online (Sandbox Code Playgroud)
特别是,在这个方法中,我想测试传入输入的数组以形成请求Url,是进入的相同数组routeData.Values.如果对于单值参数(不是数组),它可以工作,但不适用于数组.如果我调试Values我只看到controller和action.
[TestMethod]
public void GetNotificationsByCustomerAndId_ArrayOverload_Should_Match_InputParameter_name()
{
string[] _testName = new string[] { _testCustomer, _testCustomerBis };
string Url = string.Format(
"http://www.testpincopallo.it/Notifications/GetByCustomerAndLastID/customersNotificationsInfos?name={0}&name={1}&lastNotificationID={2}&lastNotificationID={3}",
_testName[0], _testName[1],
_testNotificationID, _testNotificationIDBis);
IHttpRouteData routeData = GetRouteData(Url);
routeData.Values["name"].Should().Be(_testName);
}
Run Code Online (Sandbox Code Playgroud)
在传递数组时,还有另一种单元测试方法吗?
也许您可以使用List<string>而不是像string[]这个答案一样?
此外,您可能需要将name[]代替放入name查询字符串中。
编辑
查看此内容后,我想知道在调用期间是否未完成非简单类型的模型绑定GetRouteData- 毕竟,路由不考虑这些类型,并且您不能创建两个不同的路由,例如。传递的数组中的元素数量。
因此,您应该研究模型绑定而不是请求路由。要在不实际执行调用的情况下测试代码,您可以ModelBinder手动检索对象并使用它来解析 URL。来自 ASP.NET 源代码的此测试可能与您相关。
| 归档时间: |
|
| 查看次数: |
1316 次 |
| 最近记录: |