实际函数调用计数与 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())...
./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
#include<iostream>
using namespace std;
class GTEST_static_class
{
public:
GTEST_static_class()
{}
void display()
{
cout<< "inside the GTEST_static_class:: display " <<endl;
}
};
GTestrongtatic_example.cpp
#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;
}
试试这样写:
#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:: displayn"; }
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_examplen";
}
};
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
的析构函数设置为虚函数,以避免切片。
相关文章:
- 如何使用Google Mock来模拟gettimeofday()
- gmock / google-mock发出警告,并且通过模拟异常未通过测试
- C++ Xcode 9 - 无法从 Cimg 调用函数 display()
- MOCK一个接受unique_ptr的方法
- 如何期望通过使用Google Test(Mock)以特定频率调用函数
- 静态指针指向这个为mock for googletest定义的类内部
- window.display() 单独在显示的最后一个缓冲区和当前缓冲区之间切换
- 需要有关在 C++ 中的函数 display() 上查找未定义引用的帮助
- Google Mock:在目标类的构造函数中实例化的模拟私有变量成员
- 使用 WinApi 获取与面板中相同的显示名称"Advanced display settings"
- Google Mock Expect_call将退休另一个期望
- Google-Mock 一个已经声明的方法
- RenderWindow.display()导致SFML崩溃
- 如何使用shared_ptr从外部提供mock对象
- Google Mock - 使用 CoTaskMemFree 时,测试返回没有结果
- 如何在内联回调函数中使用EXPECT_EQ和其他Google Mock语句
- C Google Mock -Expect_Call() - 期望在不直接调用时无法正常工作
- opencv: namedWindow() 错误"can't open display"!
- C++ 使用Google Mock进行高性能单元测试
- 实际函数调用计数与 EXPECT_CALL(*mock, display()) 不匹配