GMock:如何 return 由 EXPECT_CALL() 定义的函数指针

GMock: How to return a fuction pointer defined by an EXPECT_CALL()

我正在使用一个将函数指针作为 void* 传递的框架。我想要一个 return 函数指针的模拟,并且我想就地定义函数(就像 lambda;它不起作用,如下所示)。

下面显示了一个最小的工作示例。

#include <gtest/gtest.h>
#include <gmock/gmock.h>

using namespace std;
using namespace testing;

class Original
{
public:
    typedef int(*fptr)();

    void Func() 
    {
        void* f = Func2();
        fptr func = reinterpret_cast<fptr>(f);
        if (func) {
            int i = func();
            if (i == 1) {
                //do something
            } else if (i == 3) {
                //NOTE my unit test should test this decision branch
            }
        }
    }

    static int Func3() {return 1;}

    virtual void* Func2() {return (void*)&Func3;}
};

class MyMock : public Original
{
public:
    MOCK_METHOD0(Func2, void*());
};

我的主要目标:我想摆脱这个函数,并在EXPECT_CALL()中内联定义它。见下文。

int MockFunc() {cout << "mock func" << endl; return 3;}

测试用例:

TEST(MYTEST, Test)
{
    MyMock m;

    //WORKS: compiles and works as expected, 
    //but I do not want to use **MockFunc**
    EXPECT_CALL(m, Func2()).Times(AtLeast(1))
        .WillRepeatedly(Return(&MockFunc)); 

    //DOES NOT WORK: Does not compile, of course 
    //(compiler message below this code block)
    EXPECT_CALL(m, Func2()).Times(AtLeast(1))
        .WillRepeatedly(Return((void*)&([](){return 3;}))); 
    m.Func();
}

main.cpp:117:90: 错误:获取临时地址 [-fpermissive]

为了完整起见,main():

int main(int argc, char** argv)
{    
    ::testing::InitGoogleTest(&argc, argv);
    return RUN_ALL_TESTS();
}

所以问题又来了:我怎样才能去掉 MockFunc() 函数并在 Return() 中就地定义它的内容?

以下语句应该有效:

EXPECT_CALL(m, Func2()).Times(AtLeast(1)).WillRepeatedly(Return((void*)(+([](){return 3;}))));

它利用了非捕获 lambda 衰减为函数指针这一事实。
换句话说,表达式 (+([](){return 3;}) 的结果类型是 int(*)()。然后,您可以像以前一样将其转换为 void*。错误也应该消失,因为您不再获得临时地址。