为什么犀牛模拟的期望没有得到满足?

发布于 2024-09-18 20:06:04 字数 1423 浏览 2 评论 0原文

这是代码:

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();
    }
}

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();
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

呆萌少年 2024-09-25 20:06:05

如果你使用这个测试有什么改进吗?

[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());
}

顺便说一句,BackhaulMaximumReached() 是一种奇怪的设计。除非消费者检查是否达到回程最大值,否则无论 BackHaulMaximum 的值如何,都不会发出通知。

以这种方式混合命令和查询在语义上是令人困惑的。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文