Gilgamesz
Gilgamesz

Reputation: 5073

verify and verifyNoMoreInteractions to gtest

I am trying to rewrite test written from JUnit for gtest: I faced the following lines:

when(obj1.peek(300)).thenReturn(true);
verify(obj1, times(1)).peek(333);
verify(obj2, times(1)).log(400);
verifyNoMoreInteractions(obj1);
verifyNoMoreInteractions(obj2);

And I don't know how to deal with that. Please help me.

P.S. How to get a percentage of coverage from gtest/gmock?

Upvotes: 1

Views: 255

Answers (1)

Marko Popovic
Marko Popovic

Reputation: 4153

Assuming that your mock classes for objects obj1 and obj2 are named MyMockClass1 and MyMockClass2, here is how you do this using gmock:

testing::StrictMock<MyMockClass1> obj1;
testing::StrictMock<MyMockClass2> obj2;

EXPECT_CALL(obj1, peek(300)).WillOnce(Return(true));
EXPECT_CALL(obj1, peek(333)).Times(1);
EXPECT_CALL(obj2, loog(400)).Times(1);

Usage of testing::StrictMock treats every uninteresting method call on that mock object as an error, which should be what you want to achieve with verifyNoMoreInteractions.

As far as code coverage goes, I personally use the Visual Studio add-on for gtest, which then enables you to see code coverage of tests. Since you cannot use the same, then the best bet is probably Gcov suggested by @Stefano.

Upvotes: 1

Related Questions