0

I'm using Google Mock to Unit Test. I want the ability to switch between fake and mock. This is the Fake.

class Fake {
public:
    void test(char val1, int val2) {
    }
};

And here's the Mock.

class Mock {
public:
    MOCK_METHOD2(func, void(int, char*));
    MOCK_METHOD2(func, void(char, int));

    void delegate() {
      ON_CALL(*this, func).WillByDefault([this](char val1, int val2) {
        fake_medusa_->test(val1, val2);
      })
    }

private:
    Fake *fake_;
};

The error message is: error: call to member function 'gmock_func' is ambiguous

Does someone know what's the method to achieve overload here?

Tinyden
  • 524
  • 4
  • 13
  • Not sure why delegating the call to the mock to the fake/stub? With GMock you can set expectations on the calls to the mock, set return values, side effects and custom actions like calling other user-defined methods etc. What's your use case? – Quarra Jul 31 '20 at 07:04

1 Answers1

0

I think that one thing is missing: the Mock class should most probably derive from a pure virtual class, but that's a different story.

The ambiguous call error is raised because the compiler doesn't know which func method you're referring to in ON_CALL. Try this trick of dispatching the call to difderent method::

class Mock {
public:
    void func(int i, char* cp) {
        func_int_charp(i, cp);
    }

    void func(char c, int i) {
        func_char_int(c, i);
    }

    MOCK_METHOD2(func_int_charp, void(int, char*));
    MOCK_METHOD2(func_char_int, void(char, int));

    void delegate() {
      ON_CALL(*this, func_char_int).WillByDefault([this](char val1, int val2) {
        fake_medusa_->test(val1, val2);
      })
    }

private:
    Fake *fake_;
};

This should do the trick. You can also use typed any matchers :A<type>, An<type> or typed Eq matcher: TypedEq<type> or Matcher<type>. to disambiguate which func method you on the caller site:

EXPECT_CALL(mock_obj, func(A<char>(), TypedEq<int>(10));
Quarra
  • 2,527
  • 1
  • 19
  • 27