2011-08-24 74 views
7

对于某些对象,我想创建默认存根,以便公共属性包含值。但在某些情况下,我想覆盖我的默认行为。我的问题是,我能否以某种方式覆盖已经存在的值?犀牛嘲讽属性两次

//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 
+0

见http://stackoverflow.com/questions/770013/rhino-mocks-how-to-clear-以前对某个对象的期望 – Ted

回答

0

使用Expect代替StubGenerateMock代替GenerateStub将解决这个问题:

//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); 
+3

不适用于方法(至少在我使用'Arg '作为参数的情况下)。 –

+0

也许你可以创建一个新的问题,说明你的具体问题? – Jeroen

+4

我认为提问者想设置一些值为默认值,然后为每个测试更改其中一个值。即不排队新的价值,但改变它。 –