代码之家  ›  专栏  ›  技术社区  ›  George Mauer

Rhino Mocks:repeat.once()不工作?

  •  14
  • George Mauer  · 技术社区  · 16 年前

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

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

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

    2 回复  |  直到 8 年前
        1
  •  25
  •   Stefan Steinegger    14 年前

    已经有一个 thread on the RhinoMocks group .

    generateMock创建动态模拟。动态模拟允许未指定的调用(=expected)。如果发生这种情况,它只返回空值(或返回类型的默认值)。

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

    如果您想避免有超过一定数量的电话,您可以写:

    [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());
    }
    
        2
  •  9
  •   Sam Shiles    15 年前

    当使用generateMock(或通常与动态模拟一起使用)时,我总是在心里插入以下内容:

    a.expect(x=>x.notify())。重复。 * [至少] *();