Mock with action argument

I am trying to use Moq 3.x, it works great. However, I have a problem, I can’t figure out how to solve it. Considering,

public interface ITestSpec
{
  bool Run(Action<string, string> onIncorrectResponse);
}

I am trying to do the following:

var passingTestSpec = new Mock<ITestSpec>();
passingTestSpec
  .Setup(m => m.Run(null))
  .Returns(true);

Action<string, string> fakeAction =
  (expected, actual) => { throw new Exception("Should not run"); };

Assert.IsTrue(passingTestSpec.Object.Run(fakeAction));

My problem is that any call when passing TestSpec.Object.Run (... some action ...) returns false. It seems that the Moq library is trying to map the action to the argument that I passed to Run () in the Setup () call, and fails. It doesn't really matter what action I injected into the Run () call ... it still returns false.

Any ideas?

[Edit] I just discovered something; if I replace the installation string with

  .Setup(m => m.Run(fakeAction))

passes the test. However, I cannot know with what call the .Run () method will be called, so this is not a solution. Does anyone know the It.IsAny equivalent for action?

+5
1

:

It.IsAny<Action<string, string>>()
+13

All Articles