Using Moq to Validate Separate Invocations with Distinct Arguments
- by Thermite
I'm trying to validate the values of arguments passed to subsequent mocked method invocations (of the same method), but cannot figure out a valid approach. A generic example follows:
public class Foo
{
[Dependency]
public Bar SomeBar
{
get;
set;
}
public void SomeMethod()
{
this.SomeBar.SomeOtherMethod("baz");
this.SomeBar.SomeOtherMethod("bag");
}
}
public class Bar
{
public void SomeOtherMethod(string input)
{
}
}
public class MoqTest
{
[TestMethod]
public void RunTest()
{
Mock<Bar> mock = new Mock<Bar>();
Foo f = new Foo();
mock.Setup(m => m.SomeOtherMethod(It.Is<string>("baz")));
mock.Setup(m => m.SomeOtherMethod(It.Is<string>("bag"))); // this of course overrides the first call
f.SomeMethod();
mock.VerifyAll();
}
}
Using a Function in the Setup might be an option, but then it seems I'd be reduced to some sort of global variable to know which argument/iteration I'm verifying. Maybe I'm overlooking the obvious within the Moq framework?