Using Mock Tools

Mock tools such as Google Mock, CppUMock, or Hippo Mocks simplify your effort to define mocks and set up expectations. You’ll learn how to use Google Mock in this section.

Defining a Derivative

Let’s test-drive summaryDescription again. We’ll need to mock the HTTP methods get and initialize.

c5/7/Http.h
 
virtual​ ~Http() {}
 
virtual​ ​void​ initialize() = 0;
 
virtual​ std::​string​ get(​const​ std::​string​& url) ​const​ = 0;

The first step in using the mock support built into Google Mock is to create a derived class that declares mock methods. Google Mock allows us to define the HttpStub derivative succinctly.

c5/7/PlaceDescriptionServiceTest.cpp
 
class​ HttpStub: ​public​ Http {
 
public​:
*
MOCK_METHOD0(initialize, ​void

Get Modern C++ Programming with Test-Driven Development now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.