开发者

Why isn't the Rhino Mock expectation met?

Here is code:

开发者_JAVA百科
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();
    }
}


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜