Replacement Rhino Mocks GetArgumentsForCallsMadeOn() for Moq Callback() not receiving original arguments

371 views Asked by At

I am trying to replace this Rhino Mocks implementation:

private bool IsHandshakeCalled()
{
    var args = httpExecutorBuilderStub.GetArgumentsForCallsMadeOn(st => st.ExecuteHttpWebRequestAndReturn(
        Arg<HttpMethod>.Is.Anything, Arg<string>.Is.Anything, Arg<string>.Is.Anything));

    if (args.Count > 0)
    {
        return args[0][0].Equals(HttpMethod.POST) &&
               args[0][1].Equals("/api/v2/connection/command") &&
               args[0][2].Equals(JsonConvert.SerializeObject(new HandshakeRequestDto(500)));
    }
    return false;
}

with the following Moq implementation:

private bool IsHandshakeCalled()
{
    HttpMethod? capturedHttp = null;
    string? capturedString1 = null;
    string? capturedString2 = null;

    httpExecutorBuilderStub.Setup(st => st.ExecuteHttpWebRequestAndReturn(
        It.IsAny<HttpMethod>(), It.IsAny<string>(), It.IsAny<string>()))
        .Callback<HttpMethod, string, string>((h, s1, s2) => {
            capturedHttp = h;
            capturedString1 = s1;
            capturedString2 = s2;
        });

    if (capturedHttp != null)
    {
        return capturedHttp.Equals(HttpMethod.POST) &&
            capturedString1.Equals("/api/v2/connection/command") &&
            capturedString2.Equals(JsonConvert.SerializeObject(new HandshakeRequestDto(500)));
    }
    return false;
}

The problem is, my Moq implementation is not receiving original arguments.

Probably I have some issues with Moq's Callback() method.

  • What do I do wrong?
  • Should I use some different Moq method?
2

There are 2 answers

0
kingepard On BEST ANSWER

The problem was in setup-ing the mock in the wrong place. I had to move it to another method so it is setup-ed sooner.

0
Peter Csala On

There is a built-in feature called Capture.In, which can save all method calls' parameters into collections.

For example:

//Arrange
const string inputString = "ThisWillHandled";
var args = new List<string>();
mockedHandler.Setup(handler => handler.Handle(Capture.In(args)));

var SUT = new SUT(mockedHandler.Object);

//Act
SUT.TheAction(inputString);

//Assert
Assert.AreEqual(args.Count, 1);
Assert.AreEqual(args.First(), inputString);


Applying this pattern to your code it would look like this:

private bool IsHandshakeCalled()
{
    //Arrange
    var capturedHttps  = new List<HttpMethod>();
    var capturedStrings1 = new List<string>();
    var capturedStrings2 = new List<string>;

    httpExecutorBuilderStub.Setup(st => st.ExecuteHttpWebRequestAndReturn(
            Capture.In(capturedHttps), Capture.In(capturedStrings1), Capture.In(capturedStrings2)));

    //Act
    //Call the stub via the SUT

    //Assert
    if (capturedHttps.Count == 1)
    {
        return capturedHttps.Single().Equals(HttpMethod.POST) &&
               capturedStrings1.Single().Equals("/api/v2/connection/command") &&
               capturedStrings2.Single().Equals(JsonConvert.SerializeObject(new HandshakeRequestDto(500)));
    }
    return false;
}

Please make sure that you are calling the stub's ExecuteHttpWebRequestAndReturn method via the SUT before you try to examine the captured parameters.