Questions tagged [googlemock]

Designed with C++'s specifics in mind, Google C++ Mocking Framework (or Google Mock for short) is a library for writing and using C++ mock classes.

Inspired by jMock, EasyMock, and Hamcrest, and designed with C++'s specifics in mind, Google C++ Mocking Framework (or Google Mock for short) is a library for writing and using C++ mock classes.

Google Mock:

  • lets you create mock classes trivially using simple macros,
  • supports a rich set of matchers and actions,
  • handles unordered, partially ordered, or completely ordered expectations,
  • is extensible by users, and
  • works on Linux, Mac, OS X, Windows, Windows Mobile, minGW, and Symbian.
1120 questions
80
votes
6 answers

gmock setting default actions / ON_CALL vs. EXPECT_CALL

I don't understand the difference between ON_CALL and EXPECT_CALL when using it to specify the default action. So far I noticed/learned there are two ways to adjust the default action of a mock: ON_CALL(mock,…
Nicoretti
  • 1,067
  • 1
  • 8
  • 9
63
votes
4 answers

Is Google Mock a good mocking framework?

I am pioneering unit testing efforts at my company, and need need to choose a mocking framework to use. I have never used a mocking framework before. We have already chosen Google Test, so using Google Mock would be nice. However, my initial…
des4maisons
  • 1,791
  • 4
  • 20
  • 23
60
votes
2 answers

What is the difference between TEST, TEST_F and TEST_P?

I have researched a lot about gtest/gmock but none of them gave me the right answer. I'm new to C++ so any help would be really appreciated.
Nguyễn Đức Tâm
  • 1,017
  • 2
  • 10
  • 24
53
votes
5 answers

Can Google Mock a method with a smart pointer return type?

I have a factory that returns a smart pointer. Regardless of what smart pointer I use, I can't get Google Mock to mock the factory method. The mock object is the implementation of a pure abstract interface where all methods are virtual. I have a…
Matthew Reddington
  • 1,409
  • 3
  • 13
  • 24
41
votes
4 answers

What is the difference between gtest and gmock?

I'm trying to understand the purpose of google-mock, Google's C++ mocking framework. I have already worked with gtest earlier, but still I can't understand what gmock is. Why do we need it? gtest is used for unit testing. What do we need gmock for…
Rasmi Ranjan Nayak
  • 11,510
  • 29
  • 82
  • 122
37
votes
4 answers

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 =…
angela d
  • 725
  • 1
  • 8
  • 12
37
votes
1 answer

Why is GoogleMock leaking my shared_ptr?

I use GoogleMock/GoogleTest for testing, and I'm seeing some strange behavior when a matcher has a shared_ptr to a mock as a parameter, and EXPECT is called on the same shared_ptr. The offending piece of code: #include #include…
bruno nery
  • 2,022
  • 2
  • 20
  • 31
34
votes
3 answers

google mock - can I call EXPECT_CALL multiple times on same mock object?

If I call EXPECT_CALL twice on the same mock object in the same TEST_F . . . what happens? Are the expectations appended to the mock object or does the second call erase the effects of the first call? I found The After Clause which appears to imply…
Bob
  • 4,576
  • 7
  • 39
  • 107
30
votes
3 answers

How to mock templated methods using Google Mock?

I am trying to mock a templated method. Here is the class containing the method to mock : class myClass { public: virtual ~myClass() {} template void myMethod(T param); } How can I mock the method myMethod using Google…
Xavier V.
  • 6,068
  • 6
  • 30
  • 35
28
votes
3 answers

Avoid matching .WillOnce multiple times in Google Mock

I have a mock object setup that looks like this: MyObject obj; EXPECT_CALL(obj, myFunction(_)) .WillOnce(Return(1)) .WillOnce(Return(1)) .WillOnce(Return(1)) .WillRepeatedly(Return(-1)); Is there a way to not have to repeat .WillOnce(Return(1))…
UXkQEZ7
  • 1,114
  • 1
  • 14
  • 24
27
votes
2 answers

How to make a mock object throw an exception in Google Mock?

With Google Mock 1.7.0, I have a mock object with a method, and I want to expect it to be called, and in this case the mocked method should throw an exception. ObjectMock object_mock_; EXPECT_CALL(object_mock_, method()) .Times(1) …
user1735594
  • 437
  • 1
  • 6
  • 9
26
votes
3 answers

Google Mock unit testing static methods c++

I just started working on unit testing (using BOOST framework for testing, but for mocks I have to use Google Mock) and I have this situation : class A { static int Method1(int a, int b){return a+b;} }; class B { static int Method2(int a, int b){…
Jonhtra
  • 897
  • 2
  • 12
  • 18
26
votes
7 answers

Can gmock be used for stubbing C functions?

I am new to gmock, so I want to know how can I stub simple C function called in a function under test for Unit Testing. Example: int func(int a) { boolean find; // Some code find = func_1(); return find; } I have searched about gmock and in…
SRehman
  • 389
  • 1
  • 3
  • 7
26
votes
1 answer

gmock multiple in-out parameters SetArgReferee

I have an interface Itest: class Itest { bool testfunction(vector& v, int& id); } I can mock it with: MOCK_METHOD2(testfunction, bool(vector&, int&)) but how can I set the return values? I tried: vector v; int…
user3549244
  • 273
  • 1
  • 3
  • 5
25
votes
2 answers

How to specify consecutive returns in gmock?

In Mockito we can specify multiple returns like (taken from here): //you can set different behavior for consecutive method calls. //Last stubbing (e.g: thenReturn("foo")) determines the behavior of further consecutive calls. …
Paymahn Moghadasian
  • 9,301
  • 13
  • 56
  • 94
1
2 3
74 75