0

i am trying to mock a class function that is a

template<class T> QSharedPointer<T>

I checked a bit on StackOverflow but others examples are really complicated. Here is my class function:

template<class T> QSharedPointer<T> getObjectWithId ( int id )
{
    QSharedPointer<SqlObject> obj = getObjectWithId ( T::staticMetaObject.className(), id );
    return qSharedPointerCast<T> ( obj );
}

I would like to mock this function like this

class MockingSqlConnector : public SqlObjectFactory
{
public:
    MOCK_METHOD ( auto, getObjectWithId, ( int id ), ( override ) );
};

I get the error: overloaded function "SqlObjectFactory::getObjectWithId" is not a type name

xPyth
  • 33
  • 1
  • 4
  • 1
    You won't be able to mock template method. In order to have `getObjectWithId` mocked with `MOCK_METHOD`, you'd have to make it a virtual method in the base class or use this approach: https://stackoverflow.com/questions/3426067/how-to-mock-templated-methods-using-google-mock. – Quarra Nov 29 '22 at 13:05
  • 1
    You can make whole MockingSqlConnector class templated by T, in that case MOCK_METHOD would return QSharedPointer. But that will not work in case you have multiple templated methods with different types. – jdfa Dec 03 '22 at 07:47

0 Answers0