首页 > 解决方案 > gmock:使用 unique_ptr 时 EXPECT_CALL 崩溃

问题描述

我在以下代码中遇到了崩溃:

class ITest
{
    virtual void test(){};
};


void doNothing(){}



class TestMock: ITest
{
public:
    MOCK_METHOD0(test, void());
};


class Server_Fixture: public ::testing::Test
{
public:
    std::unique_ptr<TestMock> mock;

    void SetUp() override
    {        
        mock = std::unique_ptr<TestMock>();
    }

    void TearDown() override
    {

    }

    TestMock& getMock(){ return *mock; }
};


TEST_F(Server_Fixture, execute)
{
    EXPECT_CALL(getMock(), test()).WillRepeatedly(Invoke(doNothing)); // <<<< Segmentation fault (core dumped)
}

有人可以解释为什么它会崩溃吗?如果我在 TEST_F 函数中创建本地模拟对象(不使用 unique_ptr),则不会发生崩溃。

标签: c++c++17unique-ptrgooglemock

解决方案


推荐阅读