2011-08-24 87 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

我認爲提問者想設置一些值爲默認值,然後爲每個測試更改其中一個值。即不排隊新的價值,但改變它。 –