Mock equivalent to Rhino Mock GetArgumentsForCallsMadeOn

trying to verify and argue and need to restore it. what is equivalent in moq? or a way to do it in Moq?

+4
source share
2 answers

calculated this using callback functions in Mock Setup

int captured_int;

mocked_obj.Setup(x => x.SomeMethod(It.IsAny<int>()))
    .Callback<int>(x => captured_int = x);

if your method has several parameters

int captured_int;
object captured_object;

mocked_obj.Setup(x => x.SomeMethod(It.IsAny<int>(), It.IsAny<object>()))
    .Callback<int, object>((i, o) => {
                                         captured_int = i;
                                         captured_object = o;
                                     });

then you can make statements about the captured values;

+7
source

Starting with Moq 4.9.0, you can access the call list of the simulated object and execute statements for them without the need for a callback:

[Test]
public void TestMoq()
{
    var someClass = new Mock<ISomeClass>();

    someClass.Object.SomeMethod(42, null);
    someClass.Object.SomeMethod(88, "Hello");

    // First invocation
    Assert.AreEqual(42, (int) someClass.Invocations[0].Arguments[0]);
    Assert.IsNull(someClass.Invocations[0].Arguments[1]);

    // Second invocation
    Assert.AreEqual(88, (int) someClass.Invocations[1].Arguments[0]);
    Assert.AreEqual("Hello", someClass.Invocations[1].Arguments[1]);
}

, , , , object s, , Callback. , Setup, .

0

All Articles