2013-09-23 103 views
0

当我运行下面的测试:获取异常

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

我得到这个异常:

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

一切我读告诉我,这个短线操作是有效的:

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

这是怎么回事?

回答

0

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

+0

我不能使用该语法,因为'Choices'是只读的。 – Jordan

+0

这对我有效。我希望在Rhino Mocks上有一个很好的播放或者什么的。文档相当稀少。 – Jordan