Relative Content

Tag Archive for c++unit-testingmockingtddgoogletest

How to set an EXPECT_CALL to a mock object that is just created?

Here is my situation with testing. There is a note in googletest that EXPECT_CALL’s must be set first before the calls on mock methods (https://google.github.io/googletest/gmock_for_dummies.html#expectation-ordering). My problem is that I have a mock object that will only be initialized later. Right after it will be initialized a mock method from the mock object will be called right away.
With that, since I can’t set an EXPECT_CALL on a not yet existing mock object. I am forced to set an EXPECT_CALL just in time it the mock object is created. What happens in my tests now is that the mock method call happens first before the EXPECT_CALL. Because of that sequence my test fails because it did not catch the mock method that was expected in the EXPECT_CALL.
Here it is in pseudo code:

How to set an EXPECT_CALL to a mock object that is just created?

Here is my situation with testing. There is a note in googletest that EXPECT_CALL’s must be set first before the calls on mock methods (https://google.github.io/googletest/gmock_for_dummies.html#expectation-ordering). My problem is that I have a mock object that will only be initialized later. Right after it will be initialized a mock method from the mock object will be called right away.
With that, since I can’t set an EXPECT_CALL on a not yet existing mock object. I am forced to set an EXPECT_CALL just in time it the mock object is created. What happens in my tests now is that the mock method call happens first before the EXPECT_CALL. Because of that sequence my test fails because it did not catch the mock method that was expected in the EXPECT_CALL.
Here it is in pseudo code: