单元测试Asp。Net WebApi:如何使用[FromUri]参数测试方法的正确路由

[英]Unit testing Asp.Net WebApi: how to test correct routing of a method with [FromUri] parameters


I'd like to test this controller:

我想测试这个控制器:

[HttpGet]
public IList<Notification> GetNotificationsByCustomerAndId([FromUri] string[] name, [FromUri] int[] lastNotificationID)         
{
    return _storage.GetNotifications(name, lastNotificationID, _topX);
}

In particular, in this method I want to test that the array passed in input to form the request Url, is the same array that goes into routeData.Values. If for single valued parameters (not arrays) it works, but not working for arrays. If I debug Values I see only controller and action.

特别地,在这个方法中,我想要测试输入中传递来形成请求Url的数组是否与进入routeData.Values的数组相同。如果是单值参数(不是数组),它可以工作,但不能工作于数组。如果我调试值,我只看到控制器和操作。

[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);
}

Is there another way to unit test while you are passing arrays?

在传递数组时,是否有其他方法来进行单元测试?

3 个解决方案

#1


3  

Perhaps you can use List<string> rather than string[] as in this answer?

也许您可以使用List 而不是string[]作为答案?

Also, you might need to put name[] instead of name in the query string.

此外,您可能需要在查询字符串中放入name[]而不是name。

Edit
After looking into this, I'm wondering whether model binding of non-simple types is not done during the GetRouteData call -- after all, routing does not consider these types and you cannot create two routes that differ by eg. the number of elements in the passed array.

在研究这个之后进行编辑,我想知道在GetRouteData调用期间是否没有完成非简单类型的模型绑定——毕竟,路由不考虑这些类型,而且您不能创建两个与eg不同的路由。所传递数组中的元素数量。

So you should look into model binding instead of request routing. To test your code without actually performing the call, you could retrieve a ModelBinder object manually and use that to parse the URL. This test from the ASP.NET source code might be relevant for you.

因此,您应该研究模型绑定,而不是请求路由。要在不实际执行调用的情况下测试代码,可以手动检索ModelBinder对象,并使用该对象解析URL。来自ASP的测试。NET源代码可能与您相关。

#2


1  

I think that you should create a new method that will automatically determine the number of array elements and expose them to the url.

我认为您应该创建一个新方法,该方法将自动确定数组元素的数量,并将它们公开给url。

private static void ParameterSubstitution(string[] testName, string[] testNotification, ref string url)
{
    const string firstParametrName = "name";
    const string secondParametrName = "lastNotificationID";
    // first parametr
    url += string.Format("?{0}={1}", firstParametrName, string.Join(string.Format("&{0}=", firstParametrName), testName));
    // second parametr
    url += string.Format("&{0}={1}", secondParametrName, string.Join(string.Format("&{0}=",secondParametrName), testNotification));
}

and then you can use it like:

然后你可以这样使用:

var testName = new[] { "Name1", "Name2"};
var testNotification = new[] { "Notification1", "Notification2", "Notification3" };

var Url =
    @"http://www.testpincopallo.it/Notifications/GetByCustomerAndLastID/customersNotificationsInfos";

ParameterSubstitution(testName, testNotification, ref Url);

#3


1  

You can create list of query string parameters for array items and then join them using String.Join method with & as separator. This should get you the required query string easily.

您可以为数组项创建查询字符串参数列表,然后使用字符串连接它们。连接方法与& as分隔符。这将使您轻松获得所需的查询字符串。

[TestMethod]
        public void GetNotificationsByCustomerAndId_ArrayOverload_Should_Match_InputParameter_name()
        {
            string[] _testName = new string[] { _testCustomer, _testCustomerBis };

            // ASSUMING _testNotificationIDBis IS STRING ARRAY
            List<string> nParams = _testName.Select(n => string.Format("lastNotificationID={0}", n)).ToList<string>();
            string Url = string.Format(
               "http://www.testpincopallo.it/Notifications/GetByCustomerAndLastID/customersNotificationsInfos?name={0}&name={1}&{2}",
               _testName[0], _testName[1],
               String.Join("&", nParams.ToArray()));

            IHttpRouteData routeData = GetRouteData(Url);
            routeData.Values["name"].Should().Be(_testName);
        }

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.silva-art.net/blog/2015/12/02/53165a63e169704f701bf9292ad40698.html



 
© 2014-2019 ITdaan.com 粤ICP备14056181号