Gmock: is it allowed to interleave expectations and calls to mock function for Different mock objects?

49 views Asked by At

By the link https://github.com/google/googletest/blob/main/docs/gmock_for_dummies.md#using-mocks-in-tests it's written:

Important note: gMock requires expectations to be set before the mock functions are called, otherwise the behavior is undefined. Do not alternate between calls to EXPECT_CALL() and calls to the mock functions, and do not set any expectations on a mock after passing the mock to an API.

I am not sure is it about one particular mock object or is it about all mock objects used in a unit test together? I mean is it safer to place all calls that make calls to mock object at the end of the test or can I logically divide the test by having epectations+calls first for one mock object and then e.g. for another one?

UPD:

I.e., can it be:

TEST_F(MySuite, MyTest)
{
    Mock mock1, mock2;
    ObjToTest obj{mock1, mock2};

    EXPECT_CALL(mock1, foo);
    obj.funcCallingFooOnMock1();

    EXPECT_CALL(mock2, bar);
    obj.funcCallingBarOnMock2();
}

or is it also UB and it should be:

TEST_F(MySuite, MyTest)
{
    Mock mock1, mock2;
    ObjToTest obj{mock1, mock2};

    EXPECT_CALL(mock1, foo);
    EXPECT_CALL(mock2, bar);

 
    obj.funcCallingFooOnMock1();
 
    obj.funcCallingBarOnMock2();
}

instead?

0

There are 0 answers