Googlemock - makes fun of a method that returns a complex data type

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 only 2"

I think the googlemock macro does not understand QMap and interprets the comma as a parameter delimiter.

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

+7
source share
2 answers

Just use typedef as follows:

 class aClass { public: typedef const QMap<QString, QString> MyType; virtual MyType aMethod() const; } class MockaClass : public aClass { public: MOCK_CONST_METHOD0(aMethod, MyType()); } 
+16
source

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

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

Note that this will not necessarily work for nested macro calls. For example, if MOCK_CONSTANT_METHOD0 passes the second parameter to another macro, you will have problems again.

+3
source

All Articles