Introducing NFakeMail

Ever had to resort to custom code to control emails sent by an application during integration and/or system testing? If you answered yes then you should definitely continue reading.

NFakeMail makes it easier for developers to do integration/system testing on software that sends emails by providing a fake SMTP server. You’ll no longer have to manually validate the email sending process. It’s developed in C# and IronPython and targets the .NET 4.0 framework.

With NFakeMail you can easily automate the testing of components that rely on sending mails while doing its job. Let’s take a look at some sample code, we start with a simple class containing a method that sends emails.

class Notifier
{
    public void Notify()
    {
        using (var smtpClient = new SmtpClient("localhost", 10025))
        {
            smtpClient.Send("me@me.com", "you@you.com", "S1", ".");

            smtpClient.Send("me@me.com", "you@you.com", "S2", "..");
        }
    }
}

Then to automate the tests for this method we only need to the following:

[Test]
public void Notify_T001()
{
    using (var server = new FakeSmtpServer(10025))
    {
        new Notifier().Notify();

        // Verifies two messages are received in the next five seconds
        var messages = server.WaitForMessages(count: 2, timeout: 5000);

        // Verifies the message sender
        Debug.Assert(messages.All(m => m.From.Address == "me@me.com"));
    }
}

The created FakeSmtpServer instance will act as a simple SMTP server and intercept the messages sent by the Notifier class. It’s even possible to verify some fields of each intercepted message and by default all intercepted messages are saved to the file system in MIME format.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s