Rhino Mocks 3.5 测试属性设置器未被调用

发布于 2024-09-02 10:30:30 字数 42 浏览 1 评论 0原文

是否可以使用Rhino Mocks 3.5测试属性设置器是否未被调用?

Is it possible to test that a property setter has not been called using Rhino Mocks 3.5?

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

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

发布评论

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

评论(2

﹎☆浅夏丿初晴 2024-09-09 10:30:30

这是完全有可能的:

public class OneProperty
{
    virtual public int MyInt
    {
        get;
        set;
    }
}

[Test]
public void IntWasSet()
{
    var prop = Rhino.Mocks.MockRepository.GenerateMock<OneProperty>();

    prop.MyInt = 5;

    prop.AssertWasNotCalled(x => x.MyInt = Arg<int>.Is.Anything);

    prop.VerifyAllExpectations();
}

在 Rhino Mocks 3.5 上运行此测试会导致以下错误:

错误和失败:1) 测试错误:
InterfacerTests.TestMatchesInterface.IntWasSet
Rhino.Mocks.Exceptions.ExpectationViolationException
: 预计
OneProperty.set_MyInt(任何东西);会
不被称为,但它被发现在
对模拟对象进行的实际调用
对象。

我从中发现了 Arg 语法 Rhino 文档的一部分

This is entirely possible:

public class OneProperty
{
    virtual public int MyInt
    {
        get;
        set;
    }
}

[Test]
public void IntWasSet()
{
    var prop = Rhino.Mocks.MockRepository.GenerateMock<OneProperty>();

    prop.MyInt = 5;

    prop.AssertWasNotCalled(x => x.MyInt = Arg<int>.Is.Anything);

    prop.VerifyAllExpectations();
}

Running this test on Rhino Mocks 3.5 results in the following error:

Errors and Failures: 1) Test Error :
InterfacerTests.TestMatchesInterface.IntWasSet
Rhino.Mocks.Exceptions.ExpectationViolationException
: Expected that
OneProperty.set_MyInt(anything); would
not be called, but it was found on the
actual calls made on the mocked
object.

I discovered the Arg<T> syntax from this part of the Rhino documentation.

夏日浅笑〃 2024-09-09 10:30:30

在测试中将该属性设置为某个已知值。调用不会更改属性的代码,然后断言该属性与原来相同。不应该需要为此使用 Rhino Mocks。

Set the property to some known value in the test. Call the code that won't change the property, then assert that the property is the same as it was. Shouldn't need to use Rhino Mocks for that.

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