我的存根应该在设置其属性(double)时抛出异常(对任何值)。 如何使用Rhino Mocks 3.5实现这一目标?
我试过了:
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;
但在这种情况下,我不是在谈论设置和获取一个简单的值,它应该抛出。
答案 0 :(得分:3)
您必须替换为MockRepository.GenerateMock,
此版本存在GenerateStub
,
var myMock = MockRepository.GenerateMock<MyInterface>();
myMock.Stub(x => x.MyProperty).Throw(new Exception());