forked from smsohan/MvcMailer
/
MailMessageExtensionsTest.cs
84 lines (71 loc) · 2.47 KB
/
MailMessageExtensionsTest.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using Mvc.Mailer;
using System.Net.Mail;
using System.IO;
using System.Threading;
using Moq;
namespace Mvc.Mailer.Test
{
[TestFixture]
public class MailMessageExtensionsTest
{
private SmtpClientWrapper _smtpClient;
private MailMessage _mailMessage;
private DirectoryInfo _mailDirectory;
[SetUp]
public void SetUp()
{
var smtpClient = new SmtpClient();
smtpClient.DeliveryMethod = SmtpDeliveryMethod.SpecifiedPickupDirectory;
_mailDirectory = Directory.CreateDirectory(Path.Combine(Environment.CurrentDirectory, "Mails"));
smtpClient.PickupDirectoryLocation = _mailDirectory.FullName;
smtpClient.Host = "smtp.gmail.com";
smtpClient.Port = 597;
_smtpClient = new SmtpClientWrapper{InnerSmtpClient = smtpClient};
_mailMessage = new MailMessage { From = new MailAddress("gaga@gaga.com") };
_mailMessage.To.Add("gigi@gigi.com");
_mailMessage.Subject = "Hello!";
_mailMessage.Body = "Mail Body";
}
[Test]
public void TestSend()
{
_mailMessage.Send(_smtpClient);
Assert.Pass("Mail Send working since no exception wast thrown");
}
[Test]
public void TestSendAsync()
{
_mailMessage.SendAsync(smtpClient: _smtpClient);
Assert.Pass("Mail Send Async working since no exception wast thrown");
}
[Test]
public void SendAsync_with_userState_should_pass_that()
{
var client = new Mock<ISmtpClient>();
client.Setup(c => c.SendAsync(_mailMessage, "something"));
_mailMessage.SendAsync(userState: "something", smtpClient: client.Object);
client.VerifyAll();
}
[Test]
public void In_Test_Mode_should_use_TestSmtpClient()
{
TestSmtpClient.SentMails.Clear();
MailerBase.IsTestModeEnabled = true;
_mailMessage.Send();
Assert.AreEqual(1, TestSmtpClient.SentMails.Count);
Assert.AreSame(_mailMessage, TestSmtpClient.SentMails[0]);
}
[TearDown]
public void TearDown()
{
MailerBase.IsTestModeEnabled = false;
TestSmtpClient.SentMails.Clear();
_mailDirectory.Delete(true);
}
}
}