Geeks With Blogs
Etienne Giust's .NET notes Some .NET tips and tricks for me to remember. That might help you too


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.


                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.

Posted on Sunday, June 3, 2012 10:35 PM | Back to top

Comments on this post: Rhino Mocks, AssertWasCalled with Arg Constraint on array parameter

No comments posted yet.
Your comment:
 (will show your gravatar)

Copyright © Etienne Giust | Powered by: