views:

484

answers:

1

I'm having problems verifying Ienumerable / Array type parameters when setting up expectation for methods call on my mock objects. I think since it's matching different references it doesn't consider it a match. I just want it to match the contents of the array, sometimes I don't even care about the order.

mockDataWriter.Setup(m => m.UpdateFiles(new string[]{"file2.txt","file1.txt"} ) );

Ideally I want something that works like the following, I could probably write an extension method to do this.

It.Contains(new string[]{"file2.txt","file1.txt"})

It.ContainsInOrder(new string[]{"file2.txt","file1.txt"})

The only built in way I can match these right now is with the predicate feature, but it seems this problem is common enough it should be built in.

Is there a built in way to match these types, or extension library I can use. If not I'll just write an extension method or something.

Thanks

+1  A: 

Had to implement some custom matchers, haven't found any other built in way to accomplish this in version 3. Used http://code.google.com/p/moq/wiki/QuickStart as a resource.

public T[] MatchCollection<T>(T[] expectation)
{
  return Match<T[]>.Create(inputCollection => (expectation.All((i) => inputCollection.Contains(i))));
}

public IEnumerable<T> MatchCollection<T>(IEnumerable<T> expectation)
{
  return Match<T[]>.Create(inputCollection => (expectation.All((i) => inputCollection.Contains(i))));
}


public void MyTest()
{

...

mockDataWriter.Setup(m => m.UpdateFiles(MatchCollection(new string[]{"file2.txt","file1.txt"}) ) );

...


}
Ryu