TEST_F在谷歌模拟给出错误

问题描述 投票:0回答:1

这是一个使用谷歌模拟和灯具的简单示例。我正在尝试设置并在Xcode上学习谷歌模拟并编写以下代码

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)”给出错误“调用非静态成员函数而没有对象参数”。任何人都可以帮助我为什么我会收到此错误?

c++ googletest googlemock gmock
1个回答
1
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.