【发布时间】:2016-03-06 10:38:47
【问题描述】:
我想测试这个控制器:
[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);
}
在传递数组时还有其他方法进行单元测试吗?
【问题讨论】:
-
我相信没有办法对它进行“单元”测试,相反,您可以为您的方法/.它看起来就像使用带有查询参数/ POST Payload 的 http 客户端调用您的方法,并将来自服务器的结果与您期望的结果进行比较。
标签: c# asp.net unit-testing asp.net-web-api