1

当我的存根的属性(双精度)被设置(为任何值)时,我的存根应该抛出一个异常。如何使用 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;

但在这种情况下,我不是在谈论设置和获取一个简单的值,它应该抛出。

4

1 回答 1

3

您必须替换为MockRepository.GenerateMock,

这个版本有问题GenerateStub

var myMock = MockRepository.GenerateMock<MyInterface>();
myMock.Stub(x => x.MyProperty).Throw(new Exception());
于 2012-09-07T12:22:00.843 回答