用犀牛嘲笑两次捣毁财产

时间:2011-08-24 06:47:46

标签: c# mocking rhino-mocks

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

//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 个答案:

答案 0 :(得分: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);