关于 c#:Moq – 在 setup\\’s return 中使用 It.IsAny 时会发生什么? | 珊瑚贝

Moq – What happens when using It.IsAny in a setup’s return?


我正在使用 Moq 在 C# 中执行单元测试。特别是一项测试,我在 System.Net.Mail.SmtpClient 上创建了一个接口package器,以便可以对其进行模拟。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public class SmtpClient : ISmtpClient
{
    public string Host { get; set; }
    public int Port { get; set; }
    public ICredentialsByHost Credentials { get; set; }
    public bool EnableSsl { get; set; }

    public void Send(MailMessage mail)
    {
        var smtpClient = new System.Net.Mail.SmtpClient
        {
            Host = Host,
            Port = Port,
            Credentials = Credentials,
            EnableSsl = EnableSsl
        };

        smtpClient.Send(mail);
    }
}

在我对这个package器的测试中,为了确保调用方法 Send(),我模拟了接口,并且在设置模拟时,我使用 Setup() 将值分配给那个物体。在所有文档中,我看到这些设置的 .Return() 正在返回这些方法所期望的类型的特定值。但是,在我进一步理解之前,我在返回中使用了 It.IsAny<T>。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
[ClassInitialize]
public static void ClassInitialize(TestContext testContext)
{
    _smtpClientMock = new Mock<ISmtpClient>(MockBehavior.Strict);
    _smtpClientMock.Setup(x => x.Port).Returns(8080);
    _smtpClientMock.Setup(x => x.EnableSsl).Returns(false);
    _smtpClientMock.Setup(x => x.Host).Returns(“host”);
    _smtpClientMock.Setup(x => x.Credentials).Returns(It.IsAny<NetworkCredential>());

    _smtpClientMock.Setup(mockSend => mockSend.Send(It.IsAny<MailMessage>()));
}

[TestMethod]
public void WithValidMailMessageObject_WhenSendIsCalled_EmailClientCallsSmptClientToSendEmail()
{
    //Arrange

    //Act
    _smtpClientMock.Object.Send(new MailMessage());
    //Assert
    _smtpClientMock.Verify(checkMethodIsCalled => checkMethodIsCalled.Send(It.IsAny<MailMessage>()), Times.Once);
}

我注意到测试通过了。由于我没有在其他地方看到这个,我知道这不是最佳实践。我要问的是,为什么不使用它,以及在 Moq 的 Setup() 或模拟对象的 Return 内使用 It.IsAny<T>() 会出现什么问题?

  • 它旨在用于在最小起订量表达式中过滤参数。当作为设置或验证表达式之外的值传递时,它传递通用参数的默认值。所以对于引用类型,它将传递 null
  • 调用 send 方法这一事实并不意味着您发送电子邮件的代码实际上正在工作。你花了太多时间担心一些几乎无关紧要的事情。单元测试功能,调用方法的测试并不那么相关。
  • 该测试在技术上只是测试模拟框架的行为是否符合预期。
  • 如果每个人都能理解,根据我的问题,实际测试和正在测试的内容是不相关的。考虑到在网上找不到其他示例,我将最简单的测试放在一起,以演示在返回中使用”It.IsAny()”。从这个测试中唯一需要注意的是,在返回中使用 IsAny 不会使测试失败。
  • 在这种情况下,它不会通过测试。这是您所展示的唯一内容
  • @Nkosi 我觉得这很好地回答了我的问题。您介意将其发布为答案吗?另外要理解,返回中的IsAny是传递了一个空值,但还是指定的类型,所以在所有情况下都应该传递,还是有明确的情况下它不起作用?


It 用于在 Moq 表达式中过滤和匹配参数。

Allows the specification of a matching condition for an argument in a method invocation, rather than a specific argument value.”It” refers to the argument being matched.

It.IsAny<T>() 通常在方法调用的实际参数值不相关时使用。当作为 Setup 或 Verify 表达式之外的值传递时,It.IsAny<T>() 传递通用参数的默认值。所以对于引用类型,它将传递 null 等等。

虽然在您的场景中它不会失败,但通常建议不要将 It 类用于传递给模拟依赖项的匹配参数之外的任何内容。

通常在执行测试时使用 Returns 来返回使用值。如果在调用模拟时被测对象期望一个值,而模拟是 Setup 返回 It.IsAny<T>(),则测试将以意外的方式运行。

给出以下简单示例

1
2
3
4
5
6
7
8
9
10
11
12
13
public interface IDependency {
    string SomeMethod();
}

public MyClass {
    public bool MyMethod(IDependency input) {            
        var value = input.SomeMethod();

        var result =“Output” + value.ToUpper(); //<– value should not be null

        return result != null;
    }
}

由于 It.IsAny<T>()

使用不当,以下测试将失败并返回 NullReferenceException

1
2
3
4
5
6
7
8
9
10
11
12
13
14
[TestMethod]
public void MyMethod_Should_Return_True() {
    //Arrange
    var mock = new Mock<IDependency>();
    mock.Setup(_ => _.SomeMethod()).Returns(It.IsAny<string>());
    var subject = new MyClass();
    var expected = true;

    //Act
    var actual = subject.MyMethod(mock.Object);

    //Assert
    Assert.AreEqual(expected, actual);
}

  • 有没有办法在 Returns() 中将参数从 SomeMethod() 传递给 SomeOtherMethod()。类似的东西:mock.Setup(_ => _.SomeMethod(It.IsAny<int>(), It.IsAny<string>())) .Returns(SomeOtherMethod(It.IsAny<int>(), It .IsAny<字符串>()));


来源:https://www.codenong.com/49761943/

微信公众号
手机浏览(小程序)

Warning: get_headers(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(): Failed to enable crypto in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(https://static.shanhubei.com/qrcode/qrcode_viewid_9243.jpg): failed to open stream: operation failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57
0
分享到:
没有账号? 忘记密码?