4.2. External Systems

While the WroxPizza sample doesn't interact with external systems, it is very common for web applications to need external systems such as email servers.

4.2.1. Email Servers

Although your WroxPizza application doesn't have any email implementation, you could easily imagine that when an order is placed an email confirmation is sent. This is a very common feature and has certain issues when it comes to testing the implementation.

Imagine your implementation has a controller which contains some logic and is required to send an alert via email. You then have an EmailService object which implements the INotificationService interface. The EmailService will communicate with your SMTP Server and send the email.

As described in Chapter 3, testing your higher level controller logic is very easy as you can simply use a mock object to verify the two are implemented as expected:

[TestFixture]
public class ControllerTests
{
   [Test]
   public void CanSendEmail()
   {
      var notification = new Notification {Text = "Test"};

      var mock = MockRepository.GenerateMock<INotificationService>();
      mock.Expect(i => i.SendNotification(notification)).Return(true);

      Controller controller = new Controller(mock);
      controller.SendAlert(notification);

      mock.VerifyAllExpectations();
}
}

However, the problem is testing your EmailService to verify that it works as expected; you need to actually connect to an SMTP Server. There are two major problems with this. First is the fact you need to set up and figure ...

Get Testing ASP.NET Web Applications now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.