tags:

views:

214

answers:

1

I'm trying to use Moq 3.x, it works superbly. However, I have a problem I can't figure out how to solve. Given

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

I am trying 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 on passingTestSpec.Object.Run(... some action ...) returns false. It seems that the Moq library is trying to match the action to the argument I passed to Run() in the Setup() call, and fails. It actually doesn't matter what action I put in the Run() call... it's still returning false.

Any ideas?

[Edit] I just discovered something; if I replace the setup line with

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

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

+2  A: 

What's wrong with:

It.IsAny<Action<string, string>>()
Jim Arnold
LOL I should have known someone is going to find the solution... to think I wasted several hours on this :( Thanks, I'll go with that, it's better than mine.
Marcel Popescu