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
24
votes
5 answers

Mocking free function

I am stuck in a problem and can't seem to find the solution. I am using VS2005 SP1 for compiling the code. I have a global function: A* foo(); I have a mock class class MockA : public A { public: MOCK_METHOD0 (bar, bool()); ... }; In the…
Muhammad Hassan
  • 501
  • 1
  • 4
  • 14
23
votes
4 answers

How to mock method with optional parameter in Google Mock?

How to mock a method with optional parameter in Google Mock? For example: class A { public: void set_enable( bool enabled = true ); }; class MockA : public A { MOCK_METHOD1( set_enable, void( bool ) ); // this is not working };
nyarlathotep108
  • 5,275
  • 2
  • 26
  • 64
23
votes
2 answers

Unit testing C++ SetUp() and TearDown()

I'm currently learning unit testing with google mock What is the usual use of virtual void SetUp() and virtual void TearDown() in the google mock? An example scenario with codes would be good.
user2785929
  • 985
  • 7
  • 13
  • 30
23
votes
2 answers

Why does Google Mocks find this function call ambiguous?

I've run into an issue while attempting to start using Google Mocks - for some reason it can't tell the call I'm specifying in the EXPECT_CALL macro, even though the types are consistent. I want to know why it doesn't just match the first function,…
dlanod
  • 8,664
  • 8
  • 54
  • 96
22
votes
1 answer

How to use GoogleMock in Visual Studio?

This is going to be a self-answered, FAQ-style question. See answer below. With Visual Studio 2017/2019 it is really easy to set up a new Google Test project and start writing tests (as long as you don't mind using older versions of GoogleTest…
Chris Olsen
  • 3,243
  • 1
  • 27
  • 41
21
votes
4 answers

Teach Google-Test how to print Eigen Matrix

Introduction I am writing tests on Eigen matrices using Google's testing framework Google-Mock, as already discussed in another question. With the following code I was able to add a custom Matcher to match Eigen matrices to a given…
Lemming
  • 4,085
  • 3
  • 23
  • 36
21
votes
5 answers

Gmock - matching structures

How can I match value of an element in a union for an input argument e.g - if I mock a method with the following signatiure - struct SomeStruct { int data1; int data2; }; void SomeMethod(SomeStruct data); How…
NiladriBose
  • 1,849
  • 2
  • 16
  • 31
21
votes
3 answers

Mocking non-virtual methods in C++ without editing production code?

I am a fairly new software developer currently working adding unit tests to an existing C++ project that started years ago. Due to a non-technical reason, I'm not allowed to modify any existing code. The base class of all my modules has a bunch of…
wk1989
  • 611
  • 1
  • 8
  • 19
20
votes
3 answers

Mock non-virtual method C++ (gmock)

I have class class CSumWnd : public CBaseWnd { private: bool MethodA() } Please can you help how to mock MethodA() without making virtual, I didn't understand the concept of hi-perf dependency injection
Sasi
  • 203
  • 1
  • 2
  • 4
20
votes
2 answers

Is there a way to make mock functions "interesting" with ON_CALL?

Given: #include "gmock/gmock.h" #include using namespace testing; // tsk, tsk // -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- struct Mockable { virtual std::string ify(int x) const; }; std::string Mockable::ify(int…
Bulletmagnet
  • 5,665
  • 2
  • 26
  • 56
19
votes
1 answer

How to use gmock MOCK_METHOD for overloaded operators?

I am new to googlemock (and StackOverflow). I got a problem when using MOCK_METHODn in googlemock and I believe this function is widely used. Here is what I did. I have an abstract class Foo with virtual overloaded operator[]: class Foo{ public: …
Ruoxi
  • 213
  • 1
  • 2
  • 6
19
votes
1 answer

Google Test - Test that a string does not contain a string

Using Google Test, I need a way to verify that a string that was returned by my class under test does not contain a particular string. I can currently test that a string does contain another string by using EXPECT_THAT and MatchesRegex; however, I…
Dustin Wilhelmi
  • 1,769
  • 2
  • 13
  • 27
19
votes
2 answers

Is there a way to mock QSqlQuery?

I just recently discovered gmock and am now in progress of "rethinking the whole programming process as it is", adding unit tests wherever I can. One thing that struck me as weird in the process is that QSql module clearly being an external…
Zeks
  • 2,265
  • 20
  • 32
18
votes
1 answer

Mocking Parametrized Constructor using Gmock

I have class to be mocked but it does not have a default constructor. I cannot change the source code, so is there any way to mock a parametrized constructor using Gmock
Daemon
  • 1,575
  • 1
  • 17
  • 37
17
votes
1 answer

How to create a mock class with operator[]?

I am having a class with operator[], like this : class Base { public: virtual ~Base(){} virtual const int & operator[]( const unsigned int index ) const = 0; }; How can I create a mock class using google mock framework for this method? I…
BЈовић
  • 62,405
  • 41
  • 173
  • 273
1
2
3
74 75