- How to conduct integration testing when the process includes sending emails
- Developers needs to be able to verify that the emails are correctly sent but the end-users should never receive emails triggered during development
- Many non-profit projects do not have the budget to pay for SMTP Server (or the required subscription goes above the budget)
What is SmtpMoq.NET
SmtpMoq.NET is a lightweight SMTP Server that runs within the process of your .NET Core application. All the emails are not delivered but stored in the memory. Using HTML UI or JSON API you can verify if your application correctly handles the outgoing emails during development/integration testing.
Installation
Step1 - NuGet
Enter the following command in Visual Studio's Package Manager Console:
Install-Package SmtpMoq.NET
Step 2 - Source Code
Alter the ConfigureServices method from Startup.cs to run the SmtpMoq server as part of your application:
services.AddSmtpMoq();To run the HTML UI and the JSON API add the following line to the Configure method (Startup.cs):
app.UseSmtpMoq();That's it. Now your application is setup with the SmtpMoq server. Bellow in this post you can read about the two of the most common usage scenarios.
Another slightly improved scenario where the server is started only in the development environment can be found here.
Usage scenarios
Creating automated integration tests
The SmtpMoq server can be used to fully automate the integration tests where the application sends emails. The idea is that when running in the development/integration environments your application will use SmtpMoq as SMTP server to send emails. These emails won't be delivered to the end recipients but later they can be inspected by JSON API. An sample xUnit test that does exactly this a shown bellow:
Developer can visually inspect the outgoing emails
Another way to inspect the emails processed by the SmtpMoq server is to visually browse them using the embedded HTML UI. Just run your application and append /smtpmoq/ui to your root url. For example:
https://localhost:44311/smtpmoq/ui
Feedback
Please, send your feedback using the project's github page.
No comments:
Post a Comment