我编写了以下代码以使用 Google Mock 进行模拟Xyz::xyz_func
测试。Abc::abc_func
#include <iostream>
#include "gmock/gmock.h"
#include "gtest/gtest.h"
using namespace std;
using ::testing::_;
using ::testing::Return;
class Abc
{
public:
virtual ~Abc() {}
virtual bool abc_func(int arg) = 0;
};
class MockAbc : public Abc
{
public:
virtual ~MockAbc() { }
MOCK_METHOD1(abc_func, bool(int arg));
};
class AbcImpl : public Abc
{
public:
virtual bool abc_func(int arg)
{
cout << arg << " :: " << __FILE__ << " :: " << __LINE__ << endl;
return true;
}
};
class Xyz : public AbcImpl
{
public:
virtual ~Xyz() {}
virtual bool xyz_func()
{
AbcImpl obj;
return obj.abc_func(1);
}
};
TEST(AbcTest, func_success)
{
MockAbc *mock = new MockAbc();
EXPECT_CALL(*mock, abc_func(_)).WillOnce(Return(true));
Xyz test;
EXPECT_TRUE(test.xyz_func());
delete mock;
}
int main(int argc, char** argv)
{
::testing::InitGoogleMock(&argc, argv);
return RUN_ALL_TESTS();
}
但我收到以下错误。我想知道我们如何告诉 Xyz 类调用 abc_func 的模拟而不是实际的实现。请你帮助我好吗。
1 :: ./gmock_test.cpp :: 30
./gmock_test.cpp:50: Failure
Actual function call count doesn't match EXPECT_CALL(*mock, abc_func(_))...
Expected: to be called once
Actual: never called - unsatisfied and active