使用Rhino模拟对属性进行两次存根 [英] Stubbing a property twice with rhino mocks
本文介绍了使用Rhino模拟对属性进行两次存根的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
对于某些对象,我想创建默认存根,以便公共属性包含值.但是在某些情况下,我想覆盖默认行为.我的问题是,我可以以某种方式覆盖已经存根的值吗?
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
推荐答案
使用Expect
代替Stub
,使用GenerateMock
代替GenerateStub
将解决此问题:
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);
这篇关于使用Rhino模拟对属性进行两次存根的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文