我正在尝试测试传递给接口的参数是否正确:
MailMessage expectedMailMessage = new MailMessage();
var mailerMock = new Mock<IMailSender>();
mailerMock.Setup(m => m.Send(expectedMailMessage)).Verifiable();
Mailer mailer = new Mailer(mailerMock.Object);
mailer.Send(expectedMailMessage);
mailerMock.Verify(m => m.Send(expectedMailMessage), Times.Once());
梅勒班:
public class Mailer : IMailer
{
private readonly IMailSender _mailSender;
public Mailer(IMailSender mailSender)
{
_mailSender = mailSender;
}
public void Send(MailMessage mailMessage)
{
_mailSender.Send(mailMessage);
}
}
但是这仍然失败了,问题是它很难调试。我检查是否有一些随机Guid生成或设置了日期,但我找不到任何东西。
答案 0 :(得分:0)
您正在将mailerMock
而不是expectedMailMessage
传递给Send
方法:
Mailer mailer = new Mailer(mailerMock.Object);
mailer.Send(expectedMailMessage);
答案 1 :(得分:0)
经过一番摆弄后,我放弃了想出来:
mailerMock.Verify(m => m.Send(It.Is<MailMessage>(msg => msg.Subject == expectedMailMessage.Subject));
mailerMock.Verify(m => m.Send(It.Is<MailMessage>(msg => msg.Sender == expectedMailMessage.Sender));
也更容易看到它失败的属性。