Skip to content

Instantly share code, notes, and snippets.

@lukant
Last active March 30, 2020 07:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lukant/376df8c49a3aba8179879fadcea77dea to your computer and use it in GitHub Desktop.
Save lukant/376df8c49a3aba8179879fadcea77dea to your computer and use it in GitHub Desktop.
#include <gmock/gmock.h>
using namespace ::testing;
struct SomeMock
{
MOCK_CONST_METHOD1(foo, void(int));
};
TEST(Examples, TheSameArgumentsGoingToFail)
{
SomeMock mock;
EXPECT_CALL(mock, foo(4)).Times(1);
EXPECT_CALL(mock, foo(4)).Times(1);
mock.foo(4);
mock.foo(4);
}
TEST(Examples, theTestWillFailWhenOnlyLastExpectancyWasSatisfied)
{
SomeMock mock;
EXPECT_CALL(mock, foo(4)).Times(1);
EXPECT_CALL(mock, foo(4)).Times(2);
mock.foo(4);
mock.foo(4);
}
TEST(Examples, theTestWillFailWhenOnlyLastExpectancyWasSatisfied_theSameCardinality)
{
SomeMock mock;
EXPECT_CALL(mock, foo(4)).Times(2);
EXPECT_CALL(mock, foo(4)).Times(2);
mock.foo(4);
mock.foo(4);
}
Running main() from gmock_main.cc
[==========] Running 3 tests from 1 test suite.
[----------] Global test environment set-up.
[----------] 3 tests from Examples
[ RUN ] Examples.TheSameArgumentsGoingToFail
/tmp/SO/main.cpp:15: Failure
Mock function called more times than expected - returning directly.
Function call: foo(4)
Expected: to be called once
Actual: called twice - over-saturated and active
/tmp/SO/main.cpp:14: Failure
Actual function call count doesn't match EXPECT_CALL(mock, foo(4))...
Expected: to be called once
Actual: never called - unsatisfied and active
[ FAILED ] Examples.TheSameArgumentsGoingToFail (1 ms)
[ RUN ] Examples.theTestWillFailWhenOnlyLastExpectancyWasSatisfied
/tmp/SO/main.cpp:25: Failure
Actual function call count doesn't match EXPECT_CALL(mock, foo(4))...
Expected: to be called once
Actual: never called - unsatisfied and active
[ FAILED ] Examples.theTestWillFailWhenOnlyLastExpectancyWasSatisfied (0 ms)
[ RUN ] Examples.theTestWillFailWhenOnlyLastExpectancyWasSatisfied_theSameCardinality
/tmp/SO/main.cpp:36: Failure
Actual function call count doesn't match EXPECT_CALL(mock, foo(4))...
Expected: to be called twice
Actual: never called - unsatisfied and active
[ FAILED ] Examples.theTestWillFailWhenOnlyLastExpectancyWasSatisfied_theSameCardinality (0 ms)
[----------] 3 tests from Examples (1 ms total)
[----------] Global test environment tear-down
[==========] 3 tests from 1 test suite ran. (1 ms total)
[ PASSED ] 0 tests.
[ FAILED ] 3 tests, listed below:
[ FAILED ] Examples.TheSameArgumentsGoingToFail
[ FAILED ] Examples.theTestWillFailWhenOnlyLastExpectancyWasSatisfied
[ FAILED ] Examples.theTestWillFailWhenOnlyLastExpectancyWasSatisfied_theSameCardinality
3 FAILED TESTS
@ElectricRCAircraftGuy
Copy link

ElectricRCAircraftGuy commented Mar 29, 2020

Delete line 15 of main.cpp and you'll get the same failure. It appears from your demonstration that you believe that the failure only occurs if you have both line 14 and 15 in main.cpp. That's not true.

Let me check some things. There may be something I'm missing too. I want to get to the bottom of this.

@lukant
Copy link
Author

lukant commented Mar 29, 2020

From what do you imply that I believe it only will fail when both expectations are present?

In the following file the test TheSameArgumentsGoingToFail will fail as the second expectation will be matched twice and the first one will not be matched at all.

All I wrote that the test, in presented form will fail. Additionally, I wrote why it will fail:

In the following file the test TheSameArgumentGoingToFail will fail [...]

[ RUN      ] Examples.TheSameArgumentsGoingToFail

[...] as the second expectation will be matched twice [...]

/tmp/SO/main.cpp:15: Failure
Mock function called more times than expected - returning directly.
    Function call: foo(4)
         Expected: to be called once
           Actual: called twice - over-saturated and active

[...]and the first one will not be matched at all.

/tmp/SO/main.cpp:14: Failure
Actual function call count doesn't match EXPECT_CALL(mock, foo(4))...
         Expected: to be called once
           Actual: never called - unsatisfied and active

If you change the test and remove the 15th line. The expectation from 14th line will be over-saturated instead of unsatisfied and 15th expectation won't exist. That's different tests and, due to the fact that the test was aimed to be tested, different failure.

My intent was to show that multiple expect calls is not the same as one expect call with .Times(2) and the example shows that.

@ElectricRCAircraftGuy
Copy link

ElectricRCAircraftGuy commented Mar 30, 2020

After playing around a lot in a sandbox project I created here (https://github.com/ElectricRCAircraftGuy/eRCaGuy_gtest_practice), I totally see your point. I also learned how to set up Bazel from scratch, which was nice. I think we were accidentally talking past each other, as your points were totally correct, but I still had a valid point. I've edited your answer and upvoted it. Please see my comments under your answer to address what I did. I think it's a very solid answer now and crystal clear in what is happening. I really hope you like and leave my edits.

This process has also made me realize my super long answer has some important errors in it I need to go back and fix, so I'm going back to try to fix them too. I've learned a ton in this process and it has been very enlightening all the way, albeit frustrating too. I think my answer adds a lot of insight in addition to what your answer has.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment