How do I mock an overloaded function with const/non
Asked Answered
P

1

7

How do I mock the following code?

class ISomeClass
{
public:
   virtual ~ISomeClass() {} = 0;
   virtual const MyType & getType() const = 0;
   virtual MyType & getType() = 0;
};

I have tried the following, but it doesn´t work. Can you please help me?

class MockSomeClass : public ISomeClass
{
public:
    using MyTypeConstRefType = const MyType&;
    using MyTypeRefType = MyType&;

public:
    MOCK_METHOD0(getType, MyTypeConstRefType(void) const);

    MOCK_METHOD0(getType, MyTypeRefType(void));
};
Phile answered 10/12, 2015 at 8:19 Comment(4)
MOCK_CONST_METHODEulogistic
Thank you! I don´t know why I couldn´t find any information regarding this.Phile
Yeah, I got very confused by this the first time. In the end, someone else on my team had already used it in a different file, and I spotted it there. No idea how they found out about it in the first place.Eulogistic
@BobTFish If you add your comment as an answer I will accept it.Phile
E
9

They provide a separate set of macros for const member functions ("methods"): MOCK_CONST_METHOD#. So in your case, it would be:

MOCK_CONST_METHOD0(getType, MyTypeConstRefType());

The usage is otherwise identical to MOCK_METHOD#, taking the function name in the first argument and function type in the second.

Eulogistic answered 10/12, 2015 at 9:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.