我有一个带有私有set属性的类,我想用rhino mocks来存根。但是,当我尝试这样做时,它会给我一个编译时错误,说我无法设置只读属性。我是新手使用Rhino Mocks所以我必须在这里遗漏一些东西......
public Interface IFoo
{
int Quantity { get; }
}
[TestMethod]
public void SomeTest()
{
IFoo foo = MockRepository.GenerateStub<IFoo>();
foo.Quantity = 5;
//Asserts and such
}
答案 0 :(得分:34)
使用:
foo.Stub (f => f.Quantity).Return (5);
请参阅http://ayende.com/Wiki/Rhino+Mocks+3.5.ashx#UsingExpecttosetupproperties
您也可以使用:
foo.Expect(f => f.Quantity).Return (5);
答案 1 :(得分:4)
你可以这样做:
foo.Stub(f => f.Quantity).Return(5);
//asserts