GoogleMock: how to expect precisely one call with a certain argument, and see diagnostic on failure?

纵饮孤独 提交于 2019-12-06 20:45:29
Rob Kennedy

Looks like you should use the StrictMock template modifier. Let's use this simple class and mock:

struct Foo
{
    virtual void function(int) {
    }
};

struct MockFoo: public Foo
{
    MOCK_METHOD1(function, void(int x));
};

Let's start with a basic test that exercises that method:

TEST(MockTest, basic_one_expectation)
{
    MockFoo foo;
    EXPECT_CALL(foo, function(4));
    Foo& foo1(foo);
    foo1.function(3);
}

Output:

[ RUN      ] MockTest.basic_one_expectation
unknown file: Failure

Unexpected mock function call - returning directly.
    Function call: function(3)
Google Mock tried the following 1 expectation, but it didn't match:

mock-test.cpp:298: EXPECT_CALL(foo, function(4))...
  Expected arg #0: is equal to 4
           Actual: 3
         Expected: to be called once
           Actual: never called - unsatisfied and active
mock-test.cpp:298: Failure
Actual function call count doesn't match EXPECT_CALL(foo, function(4))...
         Expected: to be called once
           Actual: never called - unsatisfied and active
[  FAILED  ] MockTest.basic_one_expectation (1 ms)

That's one of the alternatives you've already considered, but you don't want it because you have other tests that don't have any particular expectations about whether the function is called, and you want those tests to fail if the function gets called anyway. As a reminder, let's see what happened when we try such a test:

TEST(MockTest, basic_no_expectation)
{
    MockFoo foo;
    Foo& foo1(foo);
    foo1.function(3);
}

Output:

[ RUN      ] MockTest.basic_no_expectation

GMOCK WARNING:
Uninteresting mock function call - returning directly.
    Function call: function(3)
Stack trace:
[       OK ] MockTest.basic_no_expectation (1 ms)

We get a warning, but the test still passes. That's no good for you. Let's see what effect StrictMock has:

TEST(MockTest, strict_no_expectation)
{
    ::testing::StrictMock<MockFoo> foo;
    Foo& foo1(foo);
    foo1.function(3);
}

Output:

[ RUN      ] MockTest.strict_no_expectation
unknown file: Failure
Uninteresting mock function call - returning directly.
    Function call: function(3)
[  FAILED  ] MockTest.strict_no_expectation (0 ms)

We didn't have to explicitly say that we don't want the function to be called, but when the function gets called anyway, the test correctly fails. Exactly what you wanted.

Finally, let's look at what happens with StrictMock in the case where there are explicit expectations for the function's argument:

TEST(MockTest, strict_one_expectation)
{
    ::testing::StrictMock<MockFoo> foo;
    EXPECT_CALL(foo, function(4));
    Foo& foo1(foo);
    foo1.function(3);
}

Output:

[ RUN      ] MockTest.strict_one_expectation
unknown file: Failure

Unexpected mock function call - returning directly.
    Function call: function(3)
Google Mock tried the following 1 expectation, but it didn't match:

mock-test.cpp:307: EXPECT_CALL(foo, function(4))...
  Expected arg #0: is equal to 4
           Actual: 3
         Expected: to be called once
           Actual: never called - unsatisfied and active
mock-test.cpp:307: Failure
Actual function call count doesn't match EXPECT_CALL(foo, function(4))...
         Expected: to be called once
           Actual: never called - unsatisfied and active
[  FAILED  ] MockTest.strict_one_expectation (0 ms)

The diagnostics show the reason the argument didn't match, just like the original basic_one_expectation test shown above.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!