[英]Mocking a pure virtual function in gtest and testing with expect_call
I am trying to test a function call from another function using gtest.我正在尝试使用 gtest 测试来自另一个函数的函数调用。 A simplified version of the header and cpp files are: Sample.h头文件和 cpp 文件的简化版本是: Sample.h
class Sample {
public:
Sample();
~Sample();
void funcA();
protected:
virtual void funcB()=0;
};
In Sample.cpp the funcA calls funcB.在Sample.cpp 中,funcA 调用 funcB。
funcA() {
funcB();
}
The code works as expected but writing its unit test fails.代码按预期工作,但编写单元测试失败。 The example of unit test I wrote is as following:我写的单元测试示例如下:
testSample.cpp testSample.cpp
#include "gtest/gtest.h"
#include "gmock/gmock.h"
#include "Sample.h"
class SampleMock : public Sample {
public:
SampleMock(): SampleMock(){};
virtual ~SampleMock(){};
MOCK_METHOD0(funcB, void());
};
class SampleTest : public ::testing::Test {
protected:
void SetUp() override
{
sample = new SampleMock();
}
void TearDown() override
{
delete sample;
}
Sample* sample;
};
TEST_F(SampleTest, Test1)
{
SampleMock tempObj;
EXPECT_CALL(tempObj, funcB);
sample->funcA();
}
}
I want to run a test and make sure that on the invocation of funcA, funcB is called.我想运行一个测试并确保在调用 funcA 时调用 funcB。 But running this test throws the error但是运行此测试会引发错误
Undefined symbols for architecture x86_64:
"Sample::funcA()", referenced from:
SampleTest_Test1_Test::TestBody() in testSample.o
"Sample::Sample(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)", referenced from:
SampleMock::SampleMock() in testSample.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
What am I doing wrong here?我在这里做错了什么?
There are a few things wrong with the code example you show:您显示的代码示例有一些问题:
You are inheriting from Sample, so change this:您是从 Sample 继承的,因此请更改以下内容:
~Sample();
to:至:
virtual ~Sample() = default;
SampleMock(): Sample(){};
You probably meant to:你可能想:
SampleMock* sample; // instead of Sample* sample
SampleMock* sample; // instead of Sample* sample
Regarding your test, EXPECT_CALL
expects a mock.关于您的测试, EXPECT_CALL
需要模拟。 I'm not entirely sure what the intent is in the test.我不完全确定测试的意图是什么。 Anyway, this should get you passed compilation:无论如何,这应该让你通过编译:
Make funcA virtual: virtual void funcA()
使 funcA 虚拟: virtual void funcA()
Mocked class becomes like this Mocked 类变成这样
class SampleTest : public ::testing::Test {
public:
// ...
SampleMock* sample;
};
SampleTest
: SampleTest
:
TEST_F(SampleTest, Test1)
{
EXPECT_CALL(*sample, funcA).WillOnce([this]() {
sample->Sample::funcA();
});
EXPECT_CALL(*sample, funcB).Times(1);
sample->funcA();
}
And the test will once called with funcA delegate it to the base, and we expect that funcB also be called.并且测试将使用 funcA 调用一次,将它委托给基类,并且我们期望 funcB 也被调用。
TEST_F(SampleTest, Test1) { EXPECT_CALL(*sample, funcA).WillOnce([this]() { sample->Sample::funcA(); }); EXPECT_CALL(*sample, funcB).Times(1); sample->funcA(); }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.