【问题标题】:Get exception when stubbing a property存根属性时获取异常
【发布时间】:2013-09-23 18:44:00
【问题描述】:

当我运行以下测试时:

    [TestMethod]
    public void MyTest()
    {
        var wizardCatalog = MockRepository.GenerateStub<IWizardCatalog>();

        var firstQuestion = MockRepository.GenerateStub<IWizardQuestion>();
        wizardCatalog.Stub(i => i.GetFirstQuestion()).Return(firstQuestion);

        var choices = new List<IWizardChoice>();
        firstQuestion.Stub(i => i.Choices).Return(choices);
    }

我得到了这个例外:

您正在尝试对定义为的属性设置期望 使用属性行为。而不是编写这样的代码: mockObject.Stub(x => x.SomeProperty).Return(42);您可以使用 属性直接达到相同的结果:mockObject.SomeProperty = 42;

我读到的所有内容都告诉我这个存根操作是有效的:

        var choices = new List<IWizardChoice>();
        firstQuestion.Stub(i => i.Choices).Return(choices);

发生了什么事?

【问题讨论】:

    标签: testing rhino-mocks


    【解决方案1】:

    PropertyBehaviour 默认在存根上打开,但在模拟上不打开。因此,您可以继续使用存根并更改为异常中建议的语法,或者使用GenerateMock&lt;IWizardQuestion&gt;() 创建一个模拟并使用您现有的.Stub(...).Return(...) 语法。

    【讨论】:

    • 这对我有用。我希望在 Rhino Mocks 上有一个好的屏幕演员或其他东西。文档相当稀疏。
    猜你喜欢
    • 2021-09-09
    • 2017-03-07
    • 1970-01-01
    • 2018-12-12
    • 1970-01-01
    • 2012-12-04
    • 1970-01-01
    • 2014-08-08
    • 2022-07-07
    相关资源
    最近更新 更多