2012-09-07 76 views
1

當它的屬性(double)設置爲任意值時,我的存根應該拋出異常。 如何使用Rhino Mocks 3.5來完成此操作?如何在Rhino Mocks中設置屬性時拋出異常

我嘗試這樣做:

 var myMock = MockRepository.GenerateStub<MyInterface>(); 
     myMock.Stub(x => x.MyProperty).Throw(new Exception()); 

但是,這給了我:

System.InvalidOperationException : You are trying to set an expectation on a property that was defined to use PropertyBehavior. 
Instead of writing code such as this: mockObject.Stub(x => x.SomeProperty).Return(42); 
You can use the property directly to achieve the same result: mockObject.SomeProperty = 42; 

但在這種情況下,我說的不是獲取和設置一個簡單的值,它應該拋出。

回答

3

您必須MockRepository.GenerateMock,

取代這個版本有GenerateStub問題,

var myMock = MockRepository.GenerateMock<MyInterface>(); 
myMock.Stub(x => x.MyProperty).Throw(new Exception()); 
+1

謝謝!有什麼我應該知道的有關GenerateMock或可以簡單地將我所有的GenerateStub替換爲GenerateMock? – Emile

+1

我很樂意幫助您Emile,您根據自己的需要進行調整,在這種情況下,它必須 –

相關問題