Verifying method with array passed by reference using Moq

Posted by kaa on Stack Overflow See other posts from Stack Overflow or by kaa
Published on 2010-04-12T10:41:42Z Indexed on 2010/04/12 10:43 UTC
Read the original article Hit count: 646

Filed under:
|

Given the following interface

public interface ISomething {
  void DoMany(string[] strs);
  void DoManyRef(ref string[] strs);
}

I would like to verify that the DoManyRef method is called, and passed any string array as the strs parameter. The following test fails:

public void CanVerifyMethodsWithArrayRefParameter() {
  var a = new Mock<ISomething>().Object;
  var strs = new string[0];
  a.DoManyRef(ref strs);
  var other = It.IsAny<string[]>();
  Mock.Get(a).Verify(t => t.DoManyRef(ref other));
}

While the following not requiring the array passed by reference passes:

public void CanVerifyMethodsWithArrayParameter() {
  var a = new Mock<ISomething>().Object;
  a.DoMany(new[] { "a", "b" });
  Mock.Get(a).Verify(t => t.DoMany(It.IsAny<string[]>()));
}

I am not able to change the interface to eliminate the by reference requirement.

© Stack Overflow or respective owner

Related posts about moq

Related posts about pass-by-reference