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
8
votes
1 answer

How to override the default ON_CALL action for just one EXPECT_CALL and go back to the default action later

I would like to test the method of my system, whose return value partially depends on the return value of the call to some kind of connection interface. In most cases I would like the IConnection to return true upon any kind of call to it's open(_,…
pptaszni
  • 5,591
  • 5
  • 27
  • 43
8
votes
3 answers

How to Configure GoogleMock in Visual Studio 2017 After Already Installing GoogleTest?

I installed the Microsoft.googletest.v140.windesktop.msvcstl.static.rt-dyn package into my VS 2017 application solution. This was accomplished by adding a new GoogleTest project to my solution via "Add New Project/Other Languages/C++/Test/Google…
8
votes
3 answers

Mocking a method with throw() specifier

I am trying to Google mock a virtual method which has a throw() specifier. The original function looks like this: virtual ReturnValue FunctionName() const throw(); I am getting the compiler error: looser throw specifier for 'virtual…
J R
  • 130
  • 1
  • 5
8
votes
2 answers

How to use gmock to mock a template method from a class?

How to use gmock to mock a template method (not a template class) for a class? Example a class like this, I want to mock this class, and this template method.. class A{ public: template int getType(int val); }; I know how…
Bryan Fok
  • 3,277
  • 2
  • 31
  • 59
8
votes
1 answer

Why does googlemock report a leaked mock when I can see it being deleted?

I'm trying to add unit tests and mocks to a legacy project. As part of this I partially mocked one of the classes, mocking just the methods that I need in the class I'm currently testing. I then pass this mock in to the class for dependency…
JSoet
  • 721
  • 7
  • 20
8
votes
1 answer

Google Mock testing::internal::ReturnAction array with negative size

I have testing code which does something like EXPECT_CALL(mock, getSomeString()).WillOnce(Return(&testString)); where getSomeString() is returning by reference: std:string& getSomeString(); and get ../../../../src/test/unit/gmock/gmock-actions.h:…
Alexander Torstling
  • 18,552
  • 7
  • 62
  • 74
8
votes
3 answers

How to mock malloc to return null in GMOCK?

I want to mock malloc in C++ using Gmock framework. Is it possible? I have tried all possible ways. But the class which is hosting this malloc implementation has got a private constructor and destructor? Is there any way in which we can directly…
DeveloperLove
  • 567
  • 2
  • 8
  • 16
8
votes
2 answers

GMock - returning default value with ON_CALL for overloaded methods

I'm trying to write mock for a class which contains three overloaded methods, ie.: #include #include using ::testing::_; using ::testing::Return; using ::testing::A; using ::testing::ByRef; using…
lgromanowski
  • 89
  • 1
  • 2
  • 4
8
votes
3 answers

mock method with 11 parameters with gmock

I'm using gmock to mock my dependencies in legacy code. One of the class have a method with 11 parameters. When I tried to use MOCK_METHOD11_WITH_CALLTYPE to mock it, I found this macro doesn't exist. gmock only supoort up to 10 parameters. What do…
Archer
  • 507
  • 1
  • 8
  • 21
8
votes
3 answers

Leaked Mock Objects when using GoogleMock together with Boost::Shared Pointers

For this special scenario, I am not able to get rid of the leaks. I get the message of Leaked Mock Objects when executing the test. The concrete Message: ClassElementFixture.h:102: ERROR: this mock object (used in test…
Alex
  • 1,602
  • 20
  • 33
8
votes
2 answers

Using googlemock EXPECT_CALL with shared_ptr?

I have a test that works fine with a raw pointer, but I'm having trouble getting it work with a std::shared_ptr. The class is like this: class MyClass { MyClass(SomeService *service); void DoIt(); } My test code is like: class…
User
  • 62,498
  • 72
  • 186
  • 247
7
votes
2 answers

How to ignore the main method from a dynamic linked library in C++

In our application we use several dynamically linked libraries. We recently updated the version of one of these libraries. The developers of that library are providing a main method since this new version (Doing nothing more than printing Hello…
W. Goeman
  • 1,674
  • 2
  • 15
  • 31
7
votes
2 answers

Google Mock and override keyword

Is there a macro in Google Mock to ensure compile time check of the signature of f() by appending the override keyword to the macro substitution: struct I { virtual void f() = 0; }; struct MockI { MOCK_METHOD0(f, void()); // this will…
nyarlathotep108
  • 5,275
  • 2
  • 26
  • 64
7
votes
2 answers

Why is the MOCK_METHOD syntax not working in GMock?

I have not even begun testing and I am encountering some syntax issues with GMock's MOCK_METHOD macro even though I am following GMock's documentation properly. Could it be a compiler issue? I have: MingGW (GCC 4.9.2) Googletest 1.10.x class…
softwarelover
  • 1,009
  • 1
  • 10
  • 22
7
votes
1 answer

Google Mock - using ::testing::An()

I receive a compilation error while using the following Google Mock declaration: EXPECT_CALL(some_object, someFunction(1,An())) .Times(2); The error is: 1>ClCompile: 1> TestMyClass.cpp 1>TestMyClass.cpp(189): error C2664:…
Jonathan Livni
  • 101,334
  • 104
  • 266
  • 359