views:

36

answers:

1

Here is code:

public interface IAccessPoint
{
    int BackHaulMaximum { get; set; }

    bool BackHaulMaximumReached();
    void EmailNetworkProvider();
}

public class AccessPoint : IAccessPoint
{
    private IMailProvider Mailer { get; set; }

    public AccessPoint(IMailProvider provider)
    {
        this.Mailer = provider ?? new DefaultMailProvider();
    }

    public int BackHaulMaximum { get; set; }

    public bool BackHaulMaximumReached()
    {
        if (BackHaulMaximum > 80)
        {
            EmailNetworkProvider();
            return true;
        }
        return false;
    }


    public void EmailNetworkProvider()
    {
        this.Mailer.SendMail();
    }
}

public interface IMailProvider
{
    void SendMail();
}

public class DefaultMailProvider : IMailProvider
{
    public void SendMail()
    {

    }
}

// Here is the Test, It is not calling EmailNetworkProvider which calls SendMail()

[TestFixture]
public class Tests
{
    [Test]
    public void NetworkProviderShouldBeEmailedWhenBackHaulMaximumIsReached()
    {
        var mailerMock = MockRepository.GenerateMock<IMailProvider>();

        mailerMock.Expect(x => x.SendMail());

        var accessPoint = new AccessPoint(mailerMock);

        accessPoint.BackHaulMaximum = 81;

        Assert.IsTrue(accessPoint.BackHaulMaximumReached());

        mailerMock.VerifyAllExpectations();
    }
}
+1  A: 

Any improvement if you use this test?

[Test]
public void NetworkProviderShouldBeEmailedWhenBackHaulMaximumIsReached()
{
    var mailerMock = MockRepository.GenerateStub<IMailProvider>();
    var accessPoint = new AccessPoint(mailerMock);

    accessPoint.BackHaulMaximum = 81;

    var actual = accessPoint.BackHaulMaximumReached();

    Assert.AreEqual(true, actual);
    mailerMock.AssertWasCalled(x => x.SendMail());
}

As a side-note, BackhaulMaximumReached() is kind of a bizarre design. No notification will be made unless a consumer checks whether the back haul maximum was reached, regardless of the value of BackHaulMaximum.

It is semantically confusing to comingle commands and queries in this way.

Jay