Rhino Mocks, AssertWasCalled with Arg Constraint on array parameter

Posted by Etienne Giust on Geeks with Blogs See other posts from Geeks with Blogs or by Etienne Giust
Published on Mon, 04 Jun 2012 09:35:57 GMT Indexed on 2012/06/04 16:44 UTC
Read the original article Hit count: 312

Filed under:

 

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.

© Geeks with Blogs or respective owner