googlemock - mock a method that returns a complex datatyp

岁酱吖の 提交于 2019-12-08 19:12:11

问题


I want to mock a method that returns a complex datatyp

class aClass
{
public:
   virtual const QMap<QString, QString> aMethod() const;
}

class MockaClass : public aClass
{
public:
   MOCK_CONST_METHOD0(aMethod, const QMap<QString, QString>());
}

This code does not compile: "macro "MOCK_CONST_METHOD0" passed 3 arguments, but takes just 2"

I think that the googlemock macro does not understand QMap and interpret the comma as parameter separator.

Is there a way to tell googlemock that QMap is the return value?


回答1:


Just use a typedef like this:

class aClass
{
public:
   typedef const QMap<QString, QString> MyType;
   virtual MyType aMethod() const;
}

class MockaClass : public aClass
{
public:
   MOCK_CONST_METHOD0(aMethod, MyType());
}



回答2:


You are right, the comma is interpreted as a parameter separator. You can define a preprocessor macro to protect the comma from being interpreted that way.

#define COMMA ,
MOCK_CONSTANT_METHOD0(aMethod, const QMap<QString COMMA QString>());

Please note, that this will not necessarily work for nested macro calls. For example, if MOCK_CONSTANT_METHOD0 would pass the second parameter to another macro, you would be in trouble again.



来源:https://stackoverflow.com/questions/10207490/googlemock-mock-a-method-that-returns-a-complex-datatyp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!