如何使用moq模拟新实例

时间:2017-04-03 12:06:21

标签: c# unit-testing moq

我想通过模拟MailMessageSmptpClient

来使用moq测试以下方法
public void SendEmail(string emailAddress, string subject, string body)
{
    using (var mail = new MailMessage(NoReplyEmailAddress, emailAddress))
    {
        mail.Subject = subject;
        mail.Body = body;
        mail.IsBodyHtml = true;

        using (var client = new SmtpClient())
        {
            client.Host = SmtpHost;
            client.Port = SmtpPort;
            client.DeliveryMethod = SmtpDeliveryMethod.Network;
            client.Send(mail);
        }
    }
}

我的测试方法是:

public void TestSendEmail()
{
    Mock<MailMessage> mailMessageMock = new Mock<MailMessage>();
    MailMessage message = (MailMessage)mailMessageMock.Setup(m => new MailMessage()).Returns(mailMessageMock.Object);
    Mock<SmtpClient> smtpClientMock = new Mock<SmtpClient>();
    smtpClientMock.Setup(s => new SmtpClient()).Returns(smtpClientMock.Object);

    EmailService emailService = new EmailService();
    emailService.SendEmail("tester@gmail.com","Test Mail", "Content");

    mailMessageMock.Verify(m => m.Subject);
    mailMessageMock.Verify(m => m.Body);
    mailMessageMock.Verify(m => m.IsBodyHtml);

    smtpClientMock.Verify(s=>s.Host);
    smtpClientMock.Verify(s=>s.Port);
    smtpClientMock.Verify(s=>s.DeliveryMethod);
    smtpClientMock.Verify(s=>s.Send(message));
}

但不管怎么说它不起作用了吗?

2 个答案:

答案 0 :(得分:6)

您的代码是不可测试的,您可以通过多种方式修复它,例如:

SmtpClientMailMessage类型上创建抽象:

public interface ISmtpClient
{
    void Send(MailMessage msg, string host, int port, SmtpDeliveryMethod deliveryMethod);
}

public interface IMailMessageComposer
{
    MailMessage Create(string noReplyEmailAddress, string emailAddress, string subject, string body, bool isHtml);
}

使用您的逻辑创建非常简单的实现:

public class MySmtpClient : ISmtpClient
{
    public void Send(MailMessage msg, string host, int port, SmtpDeliveryMethod deliveryMethod)
    {
        using (var client = new SmtpClient())
        {
            client.Host = host;
            client.Port = port;
            client.DeliveryMethod = deliveryMethod;
            client.Send(msg);
        }
    }
}

public class MailMessageComposer : IMailMessageComposer
{
    public MailMessage Create(string noReplyEmailAddress, string emailAddress, string subject, string body, bool isHtml)
    {
        return new MailMessage(noReplyEmailAddress, emailAddress)
        {
            Subject = subject,
            Body = body,
            IsBodyHtml = isHtml
        };
    }
}

然后将这些依赖项注入到负责发送电子邮件的类中:

public class EmailService
{
    private readonly ISmtpClient _smtpClient;
    private readonly IMailMessageComposer _mailMessageComposer;

    public EmailService(ISmtpClient smtpClient, IMailMessageComposer mailMessageComposer)
    {
        _smtpClient = smtpClient;
        _mailMessageComposer = mailMessageComposer;
    }

    public void SendEmail(string emailAddress, string subject, string body)
    {
        var message = _mailMessageComposer.Create(NoReplyEmailAddress, emailAddress, subject, body, true);
        _smtpClient.Send(message, SmtpHost, SmtpPort, SmtpDeliveryMethod.Network);
    }
}

现在您可以分别测试 SmtpClient MailMessage

<强>试验:

public void Create_MailMessage()
{
    var mailComposer = new MailMessageComposer();

    var mailMessage = mailComposer.Create("noReply@example.com", "email@example.com", "subject", "body", true);

    Assert.Equal("noReply@example.com", mailMessage.Subject);
    Assert.Equal("body", mailMessage.Body);
    //etc.
}

答案 1 :(得分:0)

对于模拟MailMessage和SMTPClient类,应该在EmailServiceConstructor中注入它们

  public class EmailService {
   //Constructor  
   public EmailService(MailMessage mailmsgObj, SmtpClient smtpClient)
   {

   }

 }

然后,您可以在测试用例中创建EmailServiceObject时为MailMessage和smtpclient注入模拟类

    Mock<MailMessage> mailMessageMock = new Mock<MailMessage>();
    MailMessage message = (MailMessage)mailMessageMock.Setup(m => new     MailMessage()).Returns(mailMessageMock.Object);
    Mock<SmtpClient> smtpClientMock = new Mock<SmtpClient>();
    smtpClientMock.Setup(s => new SmtpClient()).Returns(smtpClientMock.Object);

    EmailService emailService = new EmailService(mailMessageMock.object,smtpClientMock.object);