Stubbing a property twice with rhino mocks

会有一股神秘感。 提交于 2019-12-23 08:07:42

问题


For some objects I want to create default stubs so that common properties contains values. But in some cases I want to override my default behaviour. My question is, can I somehow overwrite an already stubbed value?

//First I create the default stub with a default value
var foo = MockRepository.GenerateStub<IFoo>();
foo.Stub(x => x.TheValue).Return(1);

//Somewhere else in the code I override the stubbed value
foo.Stub(x => x.TheValue).Return(2);

Assert.AreEqual(2, foo.TheValue); //Fails, since TheValue is 1

回答1:


Using Expect instead of Stub and GenerateMock instead of GenerateStub will solve this:

//First I create the default stub with a default value
var foo = MockRepository.GenerateMock<IFoo>();
foo.Expect(x => x.TheValue).Return(1);

//Somewhere else in the code I override the stubbed value
foo.Expect(x => x.TheValue).Return(2);

Assert.AreEqual(1, foo.TheValue);
Assert.AreEqual(2, foo.TheValue);


来源:https://stackoverflow.com/questions/7171500/stubbing-a-property-twice-with-rhino-mocks

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!