Created
June 27, 2018 04:35
-
-
Save danielplawgo/e63b8dedb8e0c7f8ba79ebe8ca9e79eb to your computer and use it in GitHub Desktop.
Testowanie wysyłki email w ASP.NET MVC
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<system.net> | |
<mailSettings> | |
<smtp from="[email protected]"> | |
<network host="localhost" port="25" userName="" password="" /> | |
</smtp> | |
</mailSettings> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class BaseIntegrationTests | |
{ | |
protected Lazy<IEmailServiceFactory> CreateEmailServiceFactory() | |
{ | |
return new Lazy<IEmailServiceFactory>(() => | |
new EmailServiceFactory(new Lazy<IHostingEnviromentService>(() => new TestHostingEnviromentService()))); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class BaseMailer | |
{ | |
private Lazy<IEmailServiceFactory> _emailServiceFactory; | |
protected IEmailServiceFactory EmailServiceFactory | |
{ | |
get { return _emailServiceFactory.Value; } | |
} | |
public BaseMailer(Lazy<IEmailServiceFactory> emailServiceFactory) | |
{ | |
_emailServiceFactory = emailServiceFactory; | |
} | |
protected void Send(Email email) | |
{ | |
var emailService = EmailServiceFactory.Create(GetType()); | |
emailService.Send(email); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class BaseUnitTests | |
{ | |
protected Mock<IEmailServiceFactory> EmailServiceFactory { get; set; } | |
protected Mock<IEmailService> EmailService { get; set; } | |
protected void CreateMocks() | |
{ | |
EmailServiceFactory = new Mock<IEmailServiceFactory>(); | |
EmailService = new Mock<IEmailService>(); | |
EmailServiceFactory.Setup(s => s.Create(It.IsAny<Type>())) | |
.Returns(EmailService.Object); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class EmailServiceFactory : IEmailServiceFactory | |
{ | |
private Lazy<IHostingEnviromentService> _hostingEnviromentService; | |
protected IHostingEnviromentService HostingEnviromentService | |
{ | |
get { return _hostingEnviromentService.Value; } | |
} | |
public EmailServiceFactory(Lazy<IHostingEnviromentService> hostingEnviromentService) | |
{ | |
_hostingEnviromentService = hostingEnviromentService; | |
} | |
public IEmailService Create(Type mailerType) | |
{ | |
var mailerName = mailerType.Name.Replace("Mailer", string.Empty); | |
var viewsPath = Path.GetFullPath(string.Format(HostingEnviromentService.MapPath(@"~/Views/Emails/{0}"), mailerName)); | |
var engines = new ViewEngineCollectionWithoutResolver(); | |
engines.Add(new FileSystemRazorViewEngine(viewsPath)); | |
return new EmailService(engines); | |
} | |
private class ViewEngineCollectionWithoutResolver : ViewEngineCollection | |
{ | |
public ViewEngineCollectionWithoutResolver() | |
{ | |
var resolverField = typeof(ViewEngineCollection).GetField("_dependencyResolver", | |
BindingFlags.NonPublic | BindingFlags.Instance); | |
var resolver = new EmptyResolver(); | |
resolverField.SetValue(this, resolver); | |
} | |
private class EmptyResolver : IDependencyResolver | |
{ | |
public object GetService(Type serviceType) | |
{ | |
return null; | |
} | |
public IEnumerable<object> GetServices(Type serviceType) | |
{ | |
return Enumerable.Empty<object>(); | |
} | |
} | |
} | |
} | |
public interface IEmailServiceFactory | |
{ | |
IEmailService Create(Type mailerType); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class HostingEnviromentService : IHostingEnviromentService | |
{ | |
public string MapPath(string path) | |
{ | |
return HostingEnvironment.MapPath(path); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface IHostingEnviromentService | |
{ | |
string MapPath(string path); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class TestHostingEnviromentService : IHostingEnviromentService | |
{ | |
public string MapPath(string path) | |
{ | |
var basePath = Directory.GetParent(AppDomain.CurrentDomain.BaseDirectory).Parent.Parent.FullName; | |
return path.Replace("~", Path.Combine(basePath, "PostalAndHangfire")).Replace("/", "\\"); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class UsersMailerIntegrationTests : BaseIntegrationTests | |
{ | |
protected User User = new User() | |
{ | |
FirstName = "Daniel", | |
Email = "[email protected]" | |
}; | |
protected UsersMailer Create() | |
{ | |
return new UsersMailer(CreateEmailServiceFactory()); | |
} | |
[Fact] | |
public void SendRegisterEmail() | |
{ | |
using (var server = SimpleSmtpServer.Start(25)) | |
{ | |
var usersMailer = Create(); | |
usersMailer.SendRegisterEmail(User); | |
Assert.Equal(1, server.ReceivedEmailCount); | |
var email = server.ReceivedEmail.FirstOrDefault(); | |
Assert.NotNull(email); | |
Assert.Equal("[email protected]", email.ToAddresses[0].Address); | |
Assert.Equal("[email protected]", email.FromAddress.Address); | |
Assert.Equal("Nowe konto", email.Headers["Subject"]); | |
var body = email.MessageParts[0].BodyData.FromBase64(); | |
Assert.Contains("Witaj Daniel!", body); | |
Assert.Contains("Dziękujemy za założenie konta.", body); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class UsersMailerUnitTests : BaseUnitTests | |
{ | |
protected User User = new User() | |
{ | |
FirstName = "Daniel", | |
Email = "[email protected]" | |
}; | |
protected UsersMailer Create() | |
{ | |
CreateMocks(); | |
return new UsersMailer(new Lazy<IEmailServiceFactory>(() => EmailServiceFactory.Object)); | |
} | |
[Fact] | |
public void Invoke_EmailService() | |
{ | |
var usersMailer = Create(); | |
usersMailer.SendRegisterEmail(User); | |
EmailService.Verify(s => s.Send(It.Is<RegisterEmail>(e => e.Email == "[email protected]" || e.FirstName == "Daniel"))); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment