[英]Rhino Mocks, how can I perform an action after setting a Stub property
I have a simple requirement, but I seem to be struggling. 我有一个简单的要求,但是我似乎很挣扎。
I have created a stub
that mocks an interface
that includes a Property
: 我创建了一个
stub
来模拟包含Property
的interface
:
public interface IMockIRuleRuningViewModel : IRuleRunningViewModel
{
int Id { get; set; }
}
And the mock
is : mock
是:
var mock = MockRepository.GenerateStub<IMockIRuleRuningViewModel>();
Now I want to mock
an action that I would have put in a setter for this Property
, and here is my attempt : 现在,我想
mock
一个我会为此Property
设置在设置器中的操作,这是我的尝试:
mock.Stub(x => x.Id).WhenCalled(
o =>
{
var engine = new RulesEngine(mock);
mock.ProcessRuleEngineResults(engine.RunRule("Id"));
});
But I keep getting this Exception
: 但是我一直收到这个
Exception
:
You are trying to set an expectation on a property that was defined to use PropertyBehavior.
您试图对定义为使用PropertyBehavior的属性设置期望。 Instead of writing code such as this: mockObject.Stub(x => x.SomeProperty).Return(42);
而不是编写这样的代码:mockObject.Stub(x => x.SomeProperty).Return(42); You can use the property directly to achieve the same result: mockObject.SomeProperty = 42;
您可以直接使用该属性来实现相同的结果:mockObject.SomeProperty = 42;
The following works for me: 以下对我有用:
HttpResponseBase response = MockRepository.GenerateMock<HttpResponseBase>();
// stub the getter
response.Stub(r => r.StatusCode).Return((int)HttpStatusCode.OK);
// Stub the setter
response.Stub(r => r.StatusCode = Arg<int>.Is.Anything).WhenCalled( o =>
{
Console.WriteLine("called");
});
Since what I'm actually trying to do is model the case where you can get but not set the status code (because headers have already been sent), I don't do WhenCalled()
, I do this: 由于我实际上想做的是对可以获取但不设置状态代码的情况进行建模(因为已经发送了标头),所以我不执行
WhenCalled()
,我这样做:
response.Stub(r => r.StatusCode = Arg<int>.Is.Anything)
.Throw(new HttpException("Server cannot set status after HTTP headers have been sent"));
You have to use MockRepository.GenerateMock
not MockRepository.GenerateStub
. 您必须使用
MockRepository.GenerateMock
而不是MockRepository.GenerateStub
。 I don't know why. 我不知道为什么
If you want to verify the behavior of the SUT (system under test), you should use a mock with the appropriate expectation, and verify that. 如果要验证SUT(被测系统)的行为,则应使用具有适当期望值的模拟并进行验证。 If you want just to pass a value that may need to act in a certain way, but isn't the focus of this test, you will use a stub (stub will not cause a test to fail).
如果您只想传递可能需要以某种方式起作用但不是此测试重点的值,则可以使用存根(存根不会导致测试失败)。
I assume that you are testing RulesEngine
in this test (because it is only real object I see). 我假设您正在此测试中测试
RulesEngine
(因为它只是我看到的真实对象)。 Here is a sample test, which verifies behavior of engine, when "Id" rule was executed: 这是一个示例测试,它在执行“ Id”规则时验证引擎的行为:
// Arrange
var model = MockRepository.GenerateMock<IMockIRuleRuningViewModel>();
model.Expect(m => m.ProcessEngineResults(42));
RulesEngine engine = new RulesEngine(model);
// Act
engine.RunRule("Id");
// Assert
model.VerifyAllExpectations();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.