使用默认/预期参数验证失败



任何人都可以帮助处理以下失败的代码。从我所看到的情况来看,我无法说出为什么我列出的预期参数不会导致该方法触发。

测试代码:

Mock<ExpiryNotifier> target = new Mock<ExpiryNotifier>();
Mock<MailServiceWrapper> mailMock = new Mock<MailServiceWrapper>();
mailMock.Verify(
    m => m.SendMail(
        It.IsAny<string>(),
        It.IsAny<string>(),
        It.IsAny<string[]>(),
        It.IsAny<string[]>(),
        It.IsAny<string[]>(),
        It.IsAny<string>(),
        It.IsAny<string>(),
        It.IsAny<string[]>()
    ), 
    Times.Exactly(1)
);
target.Setup(t => t.getMailService()).Returns(mailMock.Object);
target.Object.notify();

实际代码:

public virtual MailServiceWrapper getMailService()
{
    MailServiceWrapper MailService = new MailServiceWrapper();
    return MailService;
}
public string notify()
{
    string feed = loadFeed();
    MailServiceWrapper MailService = getMailService();
    MailService.SendMail(
                "sysmail.blah.net",
                "blah@blah.com",
                new string[] {"blah@blah.com"},
                new string[] { },
                new string[] { },
                "blah blah",
                "This is a blah blah email",
                new string[] { }
            );
}

方法签名:

public virtual string SendMail(string server, string from, string[] to, string[] ccs, string[] bccs, string title, string body, string[] attachments)
您需要在

执行测试后验证/断言:

// Arrange
Mock<ExpiryNotifier> target = new Mock<ExpiryNotifier>();
Mock<MailServiceWrapper> mailMock = new Mock<MailServiceWrapper>();
target.Setup(t => t.getMailService()).Returns(mailMock.Object);
// Act
target.Object.notify();
// Assert
mailMock.Verify(
            m => m.SendMail(
                It.IsAny<string>(),
                It.IsAny<string>(),
                It.IsAny<string[]>(),
                It.IsAny<string[]>(),
                It.IsAny<string[]>(),
                It.IsAny<string>(),
                It.IsAny<string>(),
                It.IsAny<string[]>()
            ), 
            Times.Exactly(1)
        );
.Verify()是一种事

调用的方法,以便确实验证您建议的方法是否被调用。

相关内容

  • 没有找到相关文章

最新更新