关于c ++:实际函数调用计数与EXPECT_CALL(* mock,display())不匹配

Actual function call count doesn't match EXPECT_CALL(*mock, display())

我在模拟函数display()上调用EXPECT_CALL,但是它返回运行时错误

Actual function call count doesn't match EXPECT_CALL(*mock, display())...

输出

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
./GTest_static_example.tst

[==========] Running 1 test from 1 test case.
[----------] Global test environment set-up.
[----------] 1 test from GTest_static_example
[ RUN      ] GTest_static_example.case1
inside the GTEST_static_class:: display
display called from GTest_static_example
package/web/webscr/GTest_static_example.cpp:70: Failure
Actual function call count doesn't match EXPECT_CALL(*mock, display())...
         Expected: to be called once
           Actual: never called - unsatisfied and active
[  FAILED  ] GTest_static_example.case1 (0 ms)
[----------] 1 test from GTest_static_example (0 ms total)

[----------] Global test environment tear-down
[==========] 1 test from 1 test case ran. (0 ms total)
[  PASSED  ] 0 tests.
[  FAILED  ] 1 test, listed below:
[  FAILED  ] GTest_static_example.case1

 1 FAILED TEST

GTEST_static_class.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include<iostream>

using namespace std;
class GTEST_static_class
{  
    public:
    GTEST_static_class()
    {}

    void display()
    {
        cout<<"inside the GTEST_static_class:: display" <<endl;
    }
};

GTest_static_example.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <gtest/gtest.h>
#include <gmock/gmock.h>
#include <iostream>
#include"GTEST_static_class.h"

using namespace std;
using ::testing::Return;


class GTest_static_example : public::testing::Test
{
    public:
    virtual void SetUp();
    virtual void TearDown();
    void call_display()
    {
        GTEST_static_class *_class = new GTEST_static_class();
        _class->display();
        cout<<"display called from GTest_static_example"<<endl;
    }

};


class MockGTEST_static_class : public GTEST_static_class
{
    public:
    MockGTEST_static_class():GTEST_static_class()
    {}

    MOCK_METHOD0(display, void());
};


int main(int argc, char* argv[])
{
    ::testing::InitGoogleTest(&argc, argv);
    return RUN_ALL_TESTS();
}

void GTest_static_example::SetUp()
{}

void GTest_static_example::TearDown()
{}

TEST_F(GTest_static_example, case1)
{
    MockGTEST_static_class *mock = new MockGTEST_static_class();
    EXPECT_CALL(*mock,display()).WillOnce(Return());
    call_display();
    delete mock;
}


尝试更多类似的方法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>
#include <gtest/gtest.h>
#include <gmock/gmock.h>

class GTEST_static_class {
  public:
    virtual void display() { std::cout <<"inside the GTEST_static_class:: display\
"
; }
    virtual ~GTEST_static_class() {}
};

class MockGTEST_static_class : public GTEST_static_class {
  public:
    MOCK_METHOD0(display, void());
};

class GTest_static_example : public ::testing::Test {
  public:
    void call_display(GTEST_static_class *instance) {
      instance->display();
      std::cout <<"display called from GTest_static_example\
"
;
    }
};

int main(int argc, char * argv[]) {
  ::testing::InitGoogleTest(&argc,argv);
  return RUN_ALL_TESTS();
}

TEST_F(GTest_static_example, case1) {
  MockGTEST_static_class mock;
  EXPECT_CALL(mock, display()).WillOnce(::testing::Return());
  call_display(&mock);
}

您需要传递期望的类的实际实例。 此外,如果希望模拟能够覆盖void display();,则需要将其设为虚拟。 不幸的是,忘记添加virtual不会生成任何编译器警告或错误-这只是您必须注意的事情。 您还应该将GTEST_static_class的析构函数设为虚拟,以避免切片。