简体   繁体   English

如何模拟在单元测试功能中使用的SmtpClient对象

[英]how to mock the SmtpClient object which is used inside a function for Unit Testing

I want to write the Nunit or unit test for the SendMail method which is present in the BatchProcess without sending mails. 我想为BatchProcess中存在的SendMail方法编写Nunit或单元测试,而不发送邮件。

How to can I mock the SmtpClient which is present inside another method. 如何模拟另一个方法中存在的SmtpClient。 Please help. 请帮忙。

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            //Assuming we are populating the emails from the data from database
            List<EmailEntity> emails = new List<EmailEntity>();
            BatchProcess.SendMail(emails);
        }
    }

    public class EmailEntity
    {
        public string ToAddress { get; set; }
        public string Subject { get; set; }
        public string Body { get; set; }
    }

    public class BatchProcess
    {
        public static void SendMail(List<EmailEntity> emails)
        {
            foreach (EmailEntity email in emails)
            {
                MailMessage mail = new MailMessage();
                SmtpClient SmtpServer = new SmtpClient("sampleSmtp.sampleTest.com");
                mail.From = new MailAddress("your_email_address@gmail.com");
                mail.To.Add(email.ToAddress);
                mail.Subject = email.Subject;
                mail.Body = email.Body;
                SmtpServer.Port = 587;
                SmtpServer.Credentials = new System.Net.NetworkCredential("username", "password");
                SmtpServer.EnableSsl = true;
                SmtpServer.Send(mail);
            }
        }
    }
}

That's one of the reason why you should use Dependency Injection . 这就是您应该使用依赖注入的原因之一。

The point is that you shouldn't create an instance of SmtpClient in SendMail() . 关键是您不应该在SendMail()创建SmtpClient的实例。 It's better to define your wrapper over SmtpClient that implements ISmtpClient interface and pass that interface to constructor of BatchProcess so that you could mock it in the test: 最好在实现ISmtpClient接口的SmtpClient上定义包装,并将该接口传递给BatchProcess构造函数,以便可以在测试中对其进行模拟:

public interface ISmtpClient
{
    int Port { get; set; }

    ICredentialsByHost Credentials { get; set; }

    bool EnableSsl { get; set; }

    void Send(MailMessage mail);
}

public class SmtpClientWrapper : SmtpClient, ISmtpClient
{
}

public class BatchProcess
{
    private readonly ISmtpClient smtpClient;

    BatchProcess(ISmtpClient smtpClient)
    {
        this.smtpClient = smtpClient;
    }

    public void SendMail(List<EmailEntity> emails)
    {
        foreach (EmailEntity email in emails)
        {
            MailMessage mail = new MailMessage();
            mail.From = new MailAddress("your_email_address@gmail.com");
            mail.To.Add(email.ToAddress);
            mail.Subject = email.Subject;
            mail.Body = email.Body;

            //  You could leave this configuration here but it's far better to have it configured in SmtpClientWrapper constructor
            //  or at least outside the loop
            smtpClient.Port = 587;
            smtpClient.Credentials = new System.Net.NetworkCredential("username", "password");
            smtpClient.EnableSsl = true;

            smtpClient.Send(mail);
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 单元测试 Smtpclient 模拟发送方法 - Unit testing Smtpclient mock Send method 模拟数据库 function 进行单元测试 - Mock a database function for unit testing 如何在单元测试中模拟没有设置器的属性? - How can I mock properties which have no setters in unit testing? 单元测试 Azure 函数:无法创建 TraceWriter 的实例,如何模拟? - Unit testing Azure Function: Cannot create an instance of TraceWriter, how to mock? 如何模拟WebOperationContext进行单元测试? - How to mock WebOperationContext for unit testing? 如何在 UnitTest 中模拟/伪造 SmtpClient? - How to mock/fake SmtpClient in a UnitTest? 如果一个功能依赖于另一个功能,您如何将该内部功能放在模拟数据中以进行单元测试? - If a function depends on another function, how do you emplace that inner-function with mock data for unit testing? 单元测试:我的ViewModel中使用的模拟静态类 - Unit testing: mock static class used in my ViewModel Mock JsonReader单元如何测试自定义JsonConverter - How Mock JsonReader unit testing a custom JsonConverter 如何模拟 DynamodbContext BatchWrite 以在网络中进行单元测试 - How to mock DynamodbContext BatchWrite for unit testing in net
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM