Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

[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?

like image 458
Francesco Bonizzi Avatar asked Dec 02 '15 08:12

Francesco Bonizzi


2 Answers

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

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

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.

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.

like image 82
Martin Wiboe Avatar answered Oct 11 '22 12:10

Martin Wiboe


I think that you should create a new method that will automatically determine the number of array elements and expose them to the 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);
like image 41
Roman Marusyk Avatar answered Oct 11 '22 12:10

Roman Marusyk