RhinoMocks:清除或重置 AssertWasCalled()

发布于 2024-12-09 16:01:54 字数 438 浏览 0 评论 0原文

如何验证在测试的“操作”部分中调用了模拟,而忽略测试的“安排”部分中对模拟的任何调用。

[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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

╄→承喏 2024-12-16 16:01:54

在这些情况下,我使用模拟而不是存根以及 ExpectVerifyAllExpectations 的组合:

//Arrange
var engineMock = MockRepository.GenerateMock<IEngine>();
var car = new Car(engineMock);
car.DriveToGroceryStore(); // this will call engine.OpenThrottle

engineMock.Expect(e => e.OpenThrottle());

//Act
car.DriveHome();

//Assert
engineMock.VerifyAllExpectations();

在这种情况下,在安排完成后将期望置于方法上。有时我认为这是它自己的测试风格:Arrange、Expect、Act、Assert

In these situations I use a mock instead of a stub and combination of Expect and VerifyAllExpectations:

//Arrange
var engineMock = MockRepository.GenerateMock<IEngine>();
var car = new Car(engineMock);
car.DriveToGroceryStore(); // this will call engine.OpenThrottle

engineMock.Expect(e => e.OpenThrottle());

//Act
car.DriveHome();

//Assert
engineMock.VerifyAllExpectations();

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

芸娘子的小脾气 2024-12-16 16:01:54

我重新阅读了您的问题,似乎您需要某种方法来区分“安排”阶段期间对模拟的调用和“行动”阶段期间对模拟的调用。我不知道对此有任何内置支持,但您可以做的是使用 WhenCalled 传递回调。在你的情况下,代码会是这样的:

// Arrange
var engineMock = MockRepository.GenerateStub<IEngine>();
var car = new Car(engineMock);
int openThrotlleCount = 0;
engineMock.Expect(x => x.OpenThrottle(arg)).WhenCalled(invocation => openThrotlleCount++);
car.DriveToGroceryStore(); // this will call engine.OpenThrottle
var openThrottleCountBeforeAct = openThrotlleCount;

// Act
car.DriveHome();

// Assert
Assert.Greater(openThrotlleCount, openThrottleCountBeforeAct);

希望它有帮助......

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:

// Arrange
var engineMock = MockRepository.GenerateStub<IEngine>();
var car = new Car(engineMock);
int openThrotlleCount = 0;
engineMock.Expect(x => x.OpenThrottle(arg)).WhenCalled(invocation => openThrotlleCount++);
car.DriveToGroceryStore(); // this will call engine.OpenThrottle
var openThrottleCountBeforeAct = openThrotlleCount;

// Act
car.DriveHome();

// Assert
Assert.Greater(openThrotlleCount, openThrottleCountBeforeAct);

Hope it helps...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文