I am using Moq as a mocking framework. I have a situation where I want to execute two different methods, that both call methods an an interface. I want to make sure, that both methods call exactly the same methods on my interface, in the same order and with the same parameters. To illustrate, here is the code:
[TestMethod]
public void UnitTest()
{
var classToTest = new ClassToTest();
Mock<IMyInterface> mock1 = new Mock<IMyInterface>();
// Setup the mock
Mock<IMyInterface> mock2 = new Mock<IMyInterface>();
// Setup the mock
classToTest.MethodToTest(mock1.Object);
classToTest.DifferentMethodToTest(mock2.Object);
// here I need help:
mock1.Verify(theSameMethodsAsMock2);
}
For example, if IMyInterface
had two methods, Method1(int i)
and Method2(string s)
, the test should pass if MethodToTest
has the structure
void MethodToTest(IMyInterface x)
{
x.Method1(42);
x.Method2("example");
x.Method1(0);
}
and DifferentMethodToTest
looks like that:
void MethodToTest(IMyInterface x)
{
int a = 10 + 32;
x.Method1(a);
string s = "examples";
x.Method2(s.Substring(0, 7));
x.Method1(0);
// might also have some code here that not related to IMyInterface at all,
// e.g. calling methods in other classes and so on
}
Same order, same methods, same parameters. Is this possible with Moq? Or do I need another mocking framework?