Search code examples
c++tddgoogletestgooglemock

GMock EXPECT_CALL doesn't match AnyMatcher


Code:

class MockDefault : public Verifyingcalls::DefaultSCI_OCB {
    public:
    MOCK_METHOD0(op, void());
    MOCK_METHOD1(op1, void(sc_integer param1));
    MOCK_METHOD2(op2, void(sc_boolean param1, sc_integer param2));
};

TEST_F(VerifyCallCount, after3Cycle) {
    MockDefault defaultMock;

    EXPECT_CALL(defaultMock, op1(_)).Times(AtLeast(4));

    EXPECT_CALL(defaultMock, op2(_,_)).Times(AtLeast(4));

    EXPECT_CALL(defaultMock, op1(5l)).Times(AtLeast(2));

    EXPECT_CALL(defaultMock, op1(7l)).Times(AtLeast(2));

    EXPECT_CALL(defaultMock, op2(true, 7l)).Times(AtLeast(2));

    EXPECT_CALL(defaultMock, op2(true, 5l)).Times(AtLeast(2));
...
}

I have a problem or at least an unexpected behavior when running this test: If I comment out the specific EXPECT_CALL's the test works just fine but if I don't i get this Failure:

Error:

Actual function call count doesn't match EXPECT_CALL(defaultMock, op2(_,_))...
         Expected: to be called at least 4 times
           Actual: never called - unsatisfied and active
..\gtests\VerifyCallCount\VerifyCallCount.cc:123: Failure

Question:

My question is, why does the test fail? I would assume that the function op1 was called at least 4 times, at least two times with the argument 5 and two times with the argument 7. So why did the test fail?


Solution

  • I think that in this case the expectations

    EXPECT_CALL(defaultMock, op2(true, 7l)).Times(AtLeast(2));
    EXPECT_CALL(defaultMock, op2(true, 5l)).Times(AtLeast(2));
    

    are called and

    EXPECT_CALL(defaultMock, op2(_,_)).Times(AtLeast(4));
    

    is not called, because the two others match. With gmock only one match is executed not all matches. An EXPECT_CALL can for example return values. That would not be unique if more than one match is called.