How to create meaningful unit tests for fakes

I understand the basics like unit test, however I often try to find out what meaningful things to experience. I believe that I need to create a fake implementation and introduce a consumer into it. I have a subscription service class (using Exchange Web Services (EWS)). Exchange 2010 requests updates by new mail. To separate my subscription from the service itself, I decided to implement its implementation in the service. The following is what I currently have. I skipped code specifically related to Exchange.

// Not a big fan of having two identical interfaces...
public interface IStreamingNotificationService
{
    void Subscribe();
}

public interface IExchangeService
{
    void Subscribe();
}

public class StreamingNotificationService : IStreamingNotificationService
{
    private readonly IExchangeService _exchangeService;

    public StreamingNotificationService(IExchangeService exchangeService)
    {
        if (exchangeService == null)
        {
            throw new ArgumentNullException("exchangeService");
        }

        _exchangeService = exchangeService;
    }

    public void Subscribe()
    {
        _exchangeService.Subscribe();
    }
}

public class ExchangeServiceImpl : IExchangeService
{
    private readonly INetworkConfiguration _networkConfiguration;
    private ExchangeService ExchangeService { get; set; }

    public ExchangeServiceImpl(INetworkConfiguration networkConfiguration)
    {
        if (networkConfiguration == null)
        {
            throw new ArgumentNullException("networkConfiguration");
        }

        _networkConfiguration = networkConfiguration;
        // Set up EWS 
    }

    public void Subscribe()
    {
        // Subscribe for new mail notifications.
    }
}

In particular, how to create a meaningful unit test to ensure that the subscription works as it should?

+3
source share
2 answers

, Subscribe. Rhino Mocks, , . ( ):

[Test]
public void SubscribesToExchange()
{
  var exchange = MockRepository.GenerateMock<IExchangeService>(); //this is the stub
  var service = StreamingNotificationService(exchange); //this is the object we are testing

  service.Subscribe();
  service.AssertWasCalled(x => x.Subscribe(););
}
+3

.

StreamingNotificationService. , , , , ( ) IExchangeService.

+1

All Articles