Googletest: Test Internal Class Calls

Created on 30 Apr 2019  路  2Comments  路  Source: google/googletest

Hi,

I have a class, i.e A

class A: public AInterface
{
    ...
    void Foo();
    void Bar();
};

In method Foo, it calls method Bar if a condition is met.

How can I test if the method Foo calls Bar?

How I have tried so far.. :

class Amock: public AInterface
{
    MOCK_METHOD0(Foo, void());
    MOCK_METHOD0(Bar, void());
    void realFoo()
    {
        AInterface::Foo();
    }
};

But I end up having a LNK2019 error.. (Use visual studio 2017)

In advance, thank you for your help!

Best regards,

Most helpful comment

I have 2 solutions, but I'm not sure they are good

class MockAAA : public AAAInterface
{
public:
    MOCK_METHOD0(Foo, void());
    MOCK_METHOD0(Bar, void());
};

MockAAA aaa;

void FakeFoo()
{
    aaa.Bar();
}

TEST(TestA, test1)
{
    // Expect
    EXPECT_CALL(aaa, Foo())
        .WillOnce(Invoke(FakeFoo));

    EXPECT_CALL(aaa, Bar());

    // Act
    aaa.Foo();
}
class MockAAA : public AAAInterface
{
public:
    MOCK_METHOD0(Foo, void());
    MOCK_METHOD0(Bar, void());
};

class TestAAA : public ::testing::Test {
protected:
    static MockAAA* aaa;
    static void SetUpTestCase() { aaa = new MockAAA(); }
    static void TearDownTestCase() { delete(aaa); }
    static void FakeFoo()
    {
        aaa->Bar();
    }
};
MockAAA* TestAAA::aaa = nullptr;

TEST_F(TestAAA, test1)
{
    // Expect
    EXPECT_CALL(*aaa, Foo())
        .WillOnce(Invoke(FakeFoo));

    EXPECT_CALL(*aaa, Bar());

    // Act
    aaa->Foo();
}

All 2 comments

I have 2 solutions, but I'm not sure they are good

class MockAAA : public AAAInterface
{
public:
    MOCK_METHOD0(Foo, void());
    MOCK_METHOD0(Bar, void());
};

MockAAA aaa;

void FakeFoo()
{
    aaa.Bar();
}

TEST(TestA, test1)
{
    // Expect
    EXPECT_CALL(aaa, Foo())
        .WillOnce(Invoke(FakeFoo));

    EXPECT_CALL(aaa, Bar());

    // Act
    aaa.Foo();
}
class MockAAA : public AAAInterface
{
public:
    MOCK_METHOD0(Foo, void());
    MOCK_METHOD0(Bar, void());
};

class TestAAA : public ::testing::Test {
protected:
    static MockAAA* aaa;
    static void SetUpTestCase() { aaa = new MockAAA(); }
    static void TearDownTestCase() { delete(aaa); }
    static void FakeFoo()
    {
        aaa->Bar();
    }
};
MockAAA* TestAAA::aaa = nullptr;

TEST_F(TestAAA, test1)
{
    // Expect
    EXPECT_CALL(*aaa, Foo())
        .WillOnce(Invoke(FakeFoo));

    EXPECT_CALL(*aaa, Bar());

    // Act
    aaa->Foo();
}

Hopefully this has been solved already. @Berntyy the link error is probably from calling AInterface::Foo() which is a pure virtual function.

If you want to check that calling A::Foo() calls A::Bar() (which I don't recommend, since that's an implementation detail of A), you could override Bar in a test-only subclass like this:

class AForTest: public A {
 public:
  MOCK_METHOD0(Bar, void());
};

TEST(TestA, FooCallsBar) {
  AForTest a;
  EXPECT_CALL(a, Bar());
  a.Foo();
}
Was this page helpful?
0 / 5 - 0 ratings