简体   繁体   English

用于 Moq Callback() 的替换 Rhino 模拟 GetArgumentsForCallsMadeOn() 未收到原始 arguments

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

I am trying to replace this Rhino Mocks implementation:我正在尝试替换这个 Rhino Mocks 实现:

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.问题是,我的 Moq 实施没有收到原始 arguments。

Probably I have some issues with Moq's Callback() method.可能我对 Moq 的Callback()方法有一些问题。

  • What do I do wrong?我做错了什么?
  • Should I use some different Moq method?我应该使用一些不同的起订量方法吗?

There is a built-in feature called Capture.In , which can save all method calls' parameters into collections.有一个名为Capture.In的内置功能,可以将所有方法调用的参数保存到 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.在尝试检查捕获的参数之前,请确保您正在通过 SUT 调用存根的ExecuteHttpWebRequestAndReturn方法。

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.我不得不将其移至另一种方法,以便更快地进行设置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM