gpt4 book ai didi

c++ - 谷歌模拟中的 TEST_F 给出错误

转载 作者:行者123 更新时间:2023-11-28 04:47:39 25 4
gpt4 key购买 nike

这是一个将 google mocking 与 fixtures 一起使用的简单示例。我正在尝试在 Xcode 上设置和学习 google mock 并编写了以下代码

using ::testing::Return;

class Shape {
public:
virtual int calculateArea() = 0;
virtual std::string getShapeColor() = 0; // this interface must have been used by some other class under test
};

// Mock class for Shape
class MockShape : public Shape{
public:
MOCK_METHOD0(calculateArea, int());
MOCK_METHOD0(getShapeColor, std::string());
};

// class under test
class Show{
public:
Show() : printFlag(false), isColorValid(false) {}

void printArea(Shape *shape) {
if (shape->calculateArea() <= 0)
printFlag = false;
else
printFlag = true;
}

void printColor(Shape *shape) {
if (shape->getShapeColor().compare("black"))
isColorValid = true;
else
isColorValid = false;
}
bool printFlag;
bool isColorValid;
};

// Test fixture for class under test
class FixtureShow : public ::testing::Test{
public:
void SetUp(){}
void TearDown(){}
void SetUpTestCase(){}
void TearDownTestCase(){}

Show show; // common resources to be used in all the test cases
MockShape mockedShape;
};

TEST_F(FixtureShow, areaValid) {
EXPECT_CALL(mockedShape, calculateArea()).WillOnce(Return(5));
show.printArea(&mockedShape);
EXPECT_EQ(show.printFlag, true);
}

“TEST_F(FixtureShow, areaValid) ”给出错误“调用没有对象参数的非静态成员函数”。任何人都可以帮助我为什么会收到此错误?

最佳答案

SetUpTestCase()TearDownTestCase() 旨在声明为静态成员函数。您也可以删除它们,除非您打算放入一些代码。

关于c++ - 谷歌模拟中的 TEST_F 给出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48950499/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com