Using nDumbster with Nunit

The following example shows a simple NUnit test written in C# which creates a server listening on port 2525 at the start of each test, sends a message to it, then checks the message was sent correctly.

[TestFixture]
public class SimpleSmtpServerTest
{
    private SimpleSmtpServer smtpServer;

    [SetUp]
    public void Setup()
    {
        this.smtpServer = SimpleSmtpServer.Start(2525);
    }

    [TearDown]
    public void TearDown()
    {
        this.smtpServer.Stop();
    }

    [Test]
    public void SendMessage_Should_Send_A_Single_Message_To_The_Specified_User
    {
        // Arrange

        // Act
        // Call your own message sending code here.
        sendMessage(2525, "sender@here.com", "Test", "Test Body", "receiver@there.com");

        // Assert
        Assert.AreEqual(1, this.smtpServer.ReceivedEmailCount, "1 mails sent");
        SmtpMessage mail= this.smtpServer.ReceivedEmail[0];
        Assert.AreEqual("<receiver@there.com>", mail.Headers["To"], "Receiver");
        Assert.AreEqual("<sender@here.com>", mail.Headers["From"], "Sender");
        Assert.AreEqual("Test", mail.Headers["Subject"], "Subject");
        Assert.AreEqual("Test Body", mailUser.Body, "Body");
     }
}

Last edited Apr 15, 2011 at 6:52 PM by blowdart, version 6

Comments

No comments yet.