Today, I had a hard time unit testing a function to make sure a Method with some array parameters was called.

Method to be called :

void AddUsersToRoles(string[] usernames, string[] roleNames);

 

I had previously used Arg<T>.Matches on complex types in other unit tests, but for some reason I was unable to find out how to apply the same logic with an array of strings.

 

It is actually quite simple to do, T really is a string[], so we use Arg<string[]>. As for the Matching part, a ToList() allows us to leverage the lambda expression.

 

sut.PermissionServices.AssertWasCalled(
                l => l.AddUsersToRoles(
                    Arg<string[]>.Matches(a => a.ToList().First() == UserId.ToString())
                    ,Arg<string[]>.Matches(a => a.ToList().First() == expectedRole1 && a.ToList()[1] == expectedRole2)
                    )
                    );

 

Of course, iw we expect an array with 2 or more values, the math would be something like : a => a.ToList()[0] == value1 && a.ToList()[1] == value2    … etc.