我想测试该控制器:单元测试Asp.Net的WebAPI:如何测试与[FromUri]的方法的正确路由参数
[HttpGet]
public IList<Notification> GetNotificationsByCustomerAndId([FromUri] string[] name, [FromUri] int[] lastNotificationID)
{
return _storage.GetNotifications(name, lastNotificationID, _topX);
}
特别地,在该方法我想测试数组传入输入以形成请求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);
}
在传递数组时,还有另一种单元测试的方法吗?
我相信有没有办法“单元”测试它,而不是你可以做你的方法/内存HttpServer集成测试。它看起来像使用http客户端使用查询参数/ POST Payload调用您的方法,并将来自服务器的结果与您期望的结果进行比较。 –