Ralph Zhang Ralph Zhang - 1 year ago 127
C++ Question

Use google mock's EXPECT_THAT in EXPECT_CALL?

I'm using google mock. The document says that we can use EXPECT_THAT in EXPECT_CALL or ON_CALL, but code like this doesn't seem to compile:

EXPECT_CALL(obj, method(_, _)).Times(1).WillOnce(EXPECT_THAT(true, Eq(1)));

I know EXPECT_THAT is a macro so it expands to some statements that shouldn't appear there. So what does "use EXPECT_THAT in EXPECT_CALL" mean? How to do it?


Answer Source

You've misunderstood the documentation for matchers:

A matcher matches a single argument. You can use it inside ON_CALL() or EXPECT_CALL(), or use it to validate a value directly

The docs then go on to give an example of how you could use a matcher to validate a value:

EXPECT_THAT(value, matcher)Asserts that value matches matcher.

This is not saying that EXPECT_THAT is itself a matcher. So you can't do what you're attempting, but only something more like:

EXPECT_THAT(true, testing::Eq(1));


EXPECT_CALL(obj, method(testing::_, testing::Eq(1))).Times(1);