0

I need to make a mock for the function foo, which is called in dosomething, is it possible with gmock?

class Base
{
public:
    int foo()
    {

    }
};


class A {

   A()  {
     pointer = new Base();
    }

    double dosomething()
    {
        //more code
        pointer->foo();
    }

private:
    Base* pointer;
};
Guy Avraham
  • 3,482
  • 3
  • 38
  • 50
Gogogo
  • 167
  • 1
  • 8
  • 1
    Which class do you want to test? If you are testing `A`, then you should inject dependency of `Base` and mock `foo` as usual. If you are testing some class that uses `A`, you should inject `A` and mock `dosomething()`. You shouldn't ever need nested mock structures in CoogleMock. – Yksisarvinen May 19 '20 at 08:10
  • I want to test class A, specifically the dosomething function, I tried to make a mock for foo, but this did not work. – Gogogo May 19 '20 at 08:13

2 Answers2

2

You need to use Dependency Injection. There are many ways to do that, I'll present the easiest one:

class Base
{
public:
    virtual int foo()
    {

    }
};

class BaseMock: public Base {
    MOCK_METHOD(int, foo, (), (override));
};

class A {

   A(Base* providedBase = new Base)  {
     pointer = providedBase;
    }

    double dosomething()
    {
        //more code
        pointer->foo();
    }

private:
    Base* pointer;
};

Now, in your unit test you can provide BaseMock in place of Base:

TEST(A_Test, dosomethingWorksCorrectly)
{
    BaseMock* mock = new BaseMock;
    EXPECT_CALL(*mock, foo()).WillOnce(Return(13));
    A uut(mock);
    uut.dosomething();
}

Note: avoid using new in modern C++. It's a headache to use it correctly and you almost never need it in C++14 and later.

Yksisarvinen
  • 18,008
  • 2
  • 24
  • 52
1

If you want to mock foo from Base, make it virtual:

class Base
{
public:
    virtual int foo()
    {

    }
};

Define your mock:

class MyMock : public Base{
    MOCK_METHOD(int, foo, (), (override));
}

You need to add Base as dependency:

class A {

   A(Base* ptr):pointer{ptr}  {}

    double dosomething()
    {
        //more code
        pointer->foo();
    }

private:
    Base* pointer;
};

Instantiate MyMock and use it in your test expectations.

Elvis Oric
  • 1,289
  • 8
  • 22