Search code examples
c++googletest

Access Google Test fixture member


I am trying to mock hardware abstraction layer function, which is uint32_t hw_epoch() by writing a fake, and from a fake calling mock method, which will let me verify that the hw_epoch() function is called. So considering the simplified code below:

#include <stdint.h>
#include "gmock/gmock.h"

using ::testing::Return;

class FooInterface {
 public:
    virtual ~FooInterface() {}
    virtual uint32_t m_hw_epoch() = 0;
};

class MockFoo : public FooInterface {
 public:
    MOCK_METHOD0(m_hw_epoch, uint32_t());
 private:
};

// ----- Test Fixture:
class FooTest : public ::testing::Test {
 public:
    FooTest() : FooInterfacePtr(&MockFooObj) {}
    ~FooTest() {}

    MockFoo MockFooObj;
    FooInterface* FooInterfacePtr;
};

// ----- Fakes
uint32_t hw_epoch() {
    FooInterfacePtr->m_hw_epoch();  // *** How Can I access FooInterfacePtr?
    return 5;
}

TEST_F(FooTest, constructor) {
}

The FooTest Fixture has member FooInterfacePtr, how can I access this member from free function uint32_t hw_epoch()?

Thank you...


Solution

  • Per @πάντα ῥεῖ, it is not possible to to do it with a free function. A custom action is a possible solution.