Asserting that a method is called exactly one time

后端 未结 7 1343
无人共我
无人共我 2021-02-05 03:16

I want to assert that a method is called exactly one time. I\'m using RhinoMocks 3.5.

Here\'s what I thought would work:



        
7条回答
  •  执念已碎
    2021-02-05 03:59

    Here's how I'd verify a method is called once.

    [Test]
    public void just_once()
    {
        // Arrange (Important to GenerateMock not GenerateStub)
        var a = MockRepository.GenerateMock();
        a.Expect(x => x.GetSomethingThatTakesALotOfResources()).Return(new Something()).Repeat.Once();
    
        // Act
        // First invocation should call GetSomethingThatTakesALotOfResources
        a.GetMeMyThing();
    
        // Second invocation should return cached result
        a.GetMeMyThing();
    
        // Assert
        a.VerifyAllExpectations();
    }
    

提交回复
热议问题