Skip to content

Instantly share code, notes, and snippets.

@isc30
Last active October 15, 2017 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save isc30/f45d4dabd67d86b0c4a097e9aa44fb1e to your computer and use it in GitHub Desktop.
Save isc30/f45d4dabd67d86b0c4a097e9aa44fb1e to your computer and use it in GitHub Desktop.
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