37

I want to test that in case of some fail no method will be called on a mock object, using google mock. So the code would be something like:

auto mockObj = new MockObj;
EXPECT_NO_METHOD_CALL(mockObj); // this is what I'm looking for

auto mainObj = new MainObj(mocObj , ......and other mocks); // here I simulate a fail using the other mock objects, and I want to be sure the no methods are called on the mockObj
Konrad
  • 355
  • 6
  • 18
angela d
  • 725
  • 1
  • 8
  • 12

4 Answers4

72

There are no needs to explicitly tell that no methods will be called. If you set the logging level high enough, you should get a message if a method is called (if no expectation is set).

Other then that, you can set expectations like this :

EXPECT_CALL( mockObj, Foo(_) ).Times(0);

on all methods.

BЈовић
  • 62,405
  • 41
  • 173
  • 273
  • 1
    This is what I need in my test. But note, right name is `Times`, starting with capital. – demi Jul 29 '13 at 20:29
27

Create a StrictMock; any unexpected method call will be a failure.

ephemient
  • 198,619
  • 38
  • 280
  • 391
  • Although the [cookbook](https://github.com/google/googletest/blob/master/googlemock/docs/cook_book.md) states that: `Our general recommendation is to use nice mocks (not yet the default) most of the time, use naggy mocks (the current default) when developing or debugging tests, and use strict mocks only as the last resort.` Better be careful – Paiusco Jul 29 '20 at 20:08
  • 1
    Don't stop here, look next answer for an specific call. – Paiusco Jul 29 '20 at 20:10
5

Use Exactly(0) for all your class methods.

the cardinality will be set to zero so you are expecting no calls

Gianluca Ghettini
  • 11,129
  • 19
  • 93
  • 159
0

You can also use StrictMock instead of NiceMock. This will fail on any "uninteresting" call, i.e., whenever a method of the mock is called, but no EXPECT_CALL was defined.

See Google Mock documentation here.

mfacchinelli
  • 101
  • 2
  • 6