Skip to content

Instantly share code, notes, and snippets.

@danielplawgo
Created June 27, 2018 04:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danielplawgo/e63b8dedb8e0c7f8ba79ebe8ca9e79eb to your computer and use it in GitHub Desktop.
Save danielplawgo/e63b8dedb8e0c7f8ba79ebe8ca9e79eb to your computer and use it in GitHub Desktop.
Testowanie wysyłki email w ASP.NET MVC
<system.net>
<mailSettings>
<smtp from="test@email.com">
<network host="localhost" port="25" userName="" password="" />
</smtp>
</mailSettings>
public class BaseIntegrationTests
{
protected Lazy<IEmailServiceFactory> CreateEmailServiceFactory()
{
return new Lazy<IEmailServiceFactory>(() =>
new EmailServiceFactory(new Lazy<IHostingEnviromentService>(() => new TestHostingEnviromentService())));
}
}
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);
}
}
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);
}
}
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);
}
public class HostingEnviromentService : IHostingEnviromentService
{
public string MapPath(string path)
{
return HostingEnvironment.MapPath(path);
}
}
public interface IHostingEnviromentService
{
string MapPath(string path);
}
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("/", "\\");
}
}
public class UsersMailerIntegrationTests : BaseIntegrationTests
{
protected User User = new User()
{
FirstName = "Daniel",
Email = "daniel@plawgo.pl"
};
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("daniel@plawgo.pl", email.ToAddresses[0].Address);
Assert.Equal("test@email.com", 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);
}
}
}
public class UsersMailerUnitTests : BaseUnitTests
{
protected User User = new User()
{
FirstName = "Daniel",
Email = "daniel@plawgo.pl"
};
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 == "daniel@plawgo.pl" || e.FirstName == "Daniel")));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment