0

我正在尝试测试传递给接口的参数是否正确:

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 或设置了日期,但我找不到任何东西。

4

2 回答 2

0

您正在传递mailerMock而不是传递expectedMailMessage给您的Send方法:

Mailer mailer = new Mailer(mailerMock.Object);
mailer.Send(expectedMailMessage);
于 2012-11-12T00:51:17.280 回答
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));

也更容易看到它在哪个属性上失败了。

于 2012-11-12T14:16:35.760 回答