template <typename F>

class MockFunction

Defined at line 2031 of file ../../third_party/googletest/src/googlemock/include/gmock/gmock-spec-builders.h

A MockFunction

<F

> type has one mock method whose type is

internal::SignatureOfT

<F

>. It is useful when you just want your

test code to emit some messages and have Google Mock verify the

right messages are sent (and perhaps at the right times). For

example, if you are exercising code:

Foo(1);

Foo(2);

Foo(3);

and want to verify that Foo(1) and Foo(3) both invoke

mock.Bar("a"), but Foo(2) doesn't invoke anything, you can write:

TEST(FooTest, InvokesBarCorrectly) {

MyMock mock;

MockFunction

<void

(string check_point_name)> check;

{

InSequence s;

EXPECT_CALL(mock, Bar("a"));

EXPECT_CALL(check, Call("1"));

EXPECT_CALL(check, Call("2"));

EXPECT_CALL(mock, Bar("a"));

}

Foo(1);

check.Call("1");

Foo(2);

check.Call("2");

Foo(3);

}

The expectation spec says that the first Bar("a") must happen

before check point "1", the second Bar("a") must happen after check

point "2", and nothing should happen between the two check

points. The explicit check points make it easy to tell which

Bar("a") is called by which call to Foo().

MockFunction

<F

> can also be used to exercise code that accepts

std::function

<internal

::SignatureOfT

<F

>> callbacks. To do so, use

AsStdFunction() method to create std::function proxy forwarding to

original object's Call. Example:

TEST(FooTest, RunsCallbackWithBarArgument) {

MockFunction

<int

(string)> callback;

EXPECT_CALL(callback, Call("bar")).WillOnce(Return(1));

Foo(callback.AsStdFunction());

}

The internal::SignatureOfT

<F

> indirection allows to use other types

than just function signature type. This is typically useful when

providing a mock for a predefined std::function type. Example:

using FilterPredicate = std::function

<bool

(string)>;

void MyFilterAlgorithm(FilterPredicate predicate);

TEST(FooTest, FilterPredicateAlwaysAccepts) {

MockFunction

<FilterPredicate

> predicateMock;

EXPECT_CALL(predicateMock, Call(_)).WillRepeatedly(Return(true));

MyFilterAlgorithm(predicateMock.AsStdFunction());

}