I want to write the Nunit or unit test for the SendMail method which is present in the BatchProcess without sending mails.
How to can I mock the SmtpClient which is present inside another method. 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);
}
}
}
}