RhinoMocks:清除或重置 AssertWasCalled()
如何验证在测试的“操作”部分中调用了模拟,而忽略测试的“安排”部分中对模拟的任何调用。
[Test]
public void ShouldOpenThrottleWhenDrivingHome()
{
var engineMock = MockRepository.GenerateStub<IEngine>();
var car = new Car(engineMock);
car.DriveToGroceryStore(); // this will call engine.OpenThrottle
car.DriveHome();
engine.AssertWasCalled(e => e.OpenThrottle());
}
我不想尝试注入新的模拟或依赖 .Repeat() 因为测试必须知道在设置中调用该方法的次数。
How can I verify a mock is called in the "act" portion of my test ignoring any calls to the mock in the "arrange" portion of the test.
[Test]
public void ShouldOpenThrottleWhenDrivingHome()
{
var engineMock = MockRepository.GenerateStub<IEngine>();
var car = new Car(engineMock);
car.DriveToGroceryStore(); // this will call engine.OpenThrottle
car.DriveHome();
engine.AssertWasCalled(e => e.OpenThrottle());
}
I'd prefer not to try an inject a fresh mock or rely on .Repeat() because the test then has to know how many times the method is called in the setup.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在这些情况下,我使用模拟而不是存根以及
Expect
和VerifyAllExpectations
的组合:在这种情况下,在安排完成后将期望置于方法上。有时我认为这是它自己的测试风格:Arrange、Expect、Act、Assert
In these situations I use a mock instead of a stub and combination of
Expect
andVerifyAllExpectations
:In this case the expectation is placed on the method after the arranging is complete. Sometimes I think of this as its own testing style: Arrange, Expect, Act, Assert
我重新阅读了您的问题,似乎您需要某种方法来区分“安排”阶段期间对模拟的调用和“行动”阶段期间对模拟的调用。我不知道对此有任何内置支持,但您可以做的是使用
WhenCalled
传递回调。在你的情况下,代码会是这样的:希望它有帮助......
I've reread your question and it seems that you want some method to seperate between the calls to the mock during the Arrange stage, and the calls to the mock during the Act stage. I don't know of any built-in support for it, but what you can do is pass a callback by using
WhenCalled
. In your case the code would be something like:Hope it helps...