Skip to content

Instantly share code, notes, and snippets.

@isc30
Last active Oct 15, 2017
Embed
What would you like to do?
Notes: GMock

Mock const methods

Use MOCK_CONST_*

Mock constructor

Simply call base constructor with custom params

Mock private or protected methods

C++ allows redefining access level on subclasses, so mocks should have everything as PUBLIC

class Loler
{
private:
    virtual void lol();
}

class LolerMock
{
public:
    MOCK_METHOD0(lol, void());
}

Mock overloads

Simply define overload params in mock type

class Loler
{
private:
    virtual void lol();
    virtual void lol(int kek);
}

class LolerMock
{
public:
    MOCK_METHOD0(lol, void());
    MOCK_METHOD0(lol, void(int kek));
}

Mock CLASS templates

Append _T to the MOCK_ macro

template<typename T>
class X
{
public:
    virtual void hey();
}

template<typename T>
class XMock
{
public:
    MOCK_METHOD0_T(hey, void());
}

Types of Mocks

Default mock (simple inheritance)

Displays warning when any method not specified as EXPECT_CALL is called

NiceMock

Displays no warnings when some method not specified as EXPECT_CALL is called
Doesn't support nesting

StrictMock

Throws error when some method not specified as EXPECT_CALL is called
Doesn't support nesting

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment