angela d
angela d

Reputation: 775

google mock : how can I " EXPECT " that no method will be called on a mock

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

Upvotes: 42

Views: 61253

Answers (4)

mfacchinelli
mfacchinelli

Reputation: 111

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.

Upvotes: 0

Gianluca Ghettini
Gianluca Ghettini

Reputation: 11628

Use Exactly(0) for all your class methods.

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

Upvotes: 5

BЈовић
BЈовић

Reputation: 64223

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.

Upvotes: 80

ephemient
ephemient

Reputation: 204738

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

Upvotes: 31

Related Questions