gpt4 book ai didi

rhino-mocks - Rhino Mocks : Repeat. Once() 不起作用?

转载 作者:行者123 更新时间:2023-12-03 15:17:11 26 4
gpt4 key购买 nike

谁能告诉我为什么下面的测试没有失败?

[Test]
public void uhh_what() {
var a = MockRepository.GenerateMock<IPrebuiltNotifier>();
a.Expect(x => x.Notify()).Repeat.Once();
a.Notify();
a.Notify();
a.VerifyAllExpectations();
}

真的需要第二双眼睛来确认我没有疯……现在我担心我所有的测试都不可靠。

最佳答案

已经有 thread on the RhinoMocks group .

GenerateMock 创建一个动态模拟。动态模拟允许未指定(=预期)的调用。如果发生这种情况,它只会返回 null(或返回类型的默认值)。

注:重复是行为的规范(如 Stub),而不是期望 即使在期望中指定。

如果你想避免超过一定数量的调用,你可以写:

[Test]
public void uhh_what()
{
var a = MockRepository.GenerateMock<IPrebuiltNotifier>();
a.Expect(x => x.Notify()).Repeat.Once();
a.Stub(x => x.Notify()).Throw(new InvalidOperationException("gotcha"));
a.Notify();

// this fails
a.Notify();

a.VerifyAllExpectations();
}

或者
[Test]
public void uhh_what()
{
var a = MockRepository.GenerateMock<IPrebuiltNotifier>();
a.Notify();
a.Notify();

// this fails
a.AssertWasCalled(
x => x.Notify(),
o => o.Repeat.Once());
}

关于rhino-mocks - Rhino Mocks : Repeat. Once() 不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/887245/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com