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

How can I tell GoogleMock to stop checking an expectation after the test finished?

I have two unit tests that share some state (unfortunately I can't change this since the point is to test the handling of this very state). TEST(MySuite, test1) { shared_ptr first(make_shared()); …
Tamás Szelei
  • 23,169
  • 18
  • 105
  • 180
13
votes
1 answer

C++ Mock/Test boost::asio::io_stream - based Asynch Handler

I've recently returned to C/C++ after years of C#. During those years I've found the value of Mocking and Unit testing. Finding resources for Mocks and Units tests in C# is trivial. WRT Mocking, not so much with C++. I would like some guidance on…
rbellamy
  • 5,683
  • 6
  • 38
  • 48
13
votes
1 answer

Mock function with class template parameter in signature

Is it possible with gmock to mock a function which contains a class template parameter in it's signature? For example: template struct Mockable { virtual void do_work(const int num, const T& value) = 0; }; template
Graeme
  • 4,514
  • 5
  • 43
  • 71
13
votes
5 answers

Expecting googlemock calls from another thread

What will be the best way to write (google) test cases using a google mock object and expect the EXPECT_CALL() definitions being called from another thread controlled by the class in test? Simply calling sleep() or alike after triggering the call…
πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
12
votes
2 answers

how to set custom ref-variable in gmock

I am using gmock in my project and I meet a problem to set a custom reference variable for a mock function. Suppose I have a class as following: class XXXClient { public: void QueryXXX(const Request&, Response&); }; class XXXRunner { public: …
bourneli
  • 2,172
  • 4
  • 24
  • 40
12
votes
2 answers

Google mock compile error (error: ‘’ is not a type)

My actual code (class name changed, some cut, as it is company confidential, but there is only one compiler error, so what I cut should not be affecting things) class Xyz { public: virtual void vPrintStatus() const; }; and its mock class…
Mawg says reinstate Monica
  • 38,334
  • 103
  • 306
  • 551
12
votes
1 answer

Google Mock: why NiceMock does not ignore unexpected calls?

I am using Google Mock 1.7.0 with Google Test 1.7.0. The problem is when I use NiceMock I get test failures because of unexpected mock function call (which should be ignored by NiceMock as per Google Mock documentation). The code looks like this: //…
Uniqus
  • 564
  • 1
  • 5
  • 21
11
votes
1 answer

How to stop GTest test-case execution, when first test failed

I'm looking for some #define which can stop test-case execution if first test failed TEST_F(TestInitializer, 1st-test) { Initiator.call(); EXPECT_CALL(mock_obj, onAction(false)).Times(AtLeast(0)); // some define I want …
Yuriy Gyerts
  • 1,464
  • 18
  • 30
11
votes
1 answer

How to mock variadic functions using googlemock

Not so much a question as a piece of knowledge sharing. According to the GoogleMock FAQ it is not possible to mock variadic functions since it is unknown how many arguments will be given to the function. This is true, but in most cases one knows…
Nemelis
  • 4,858
  • 2
  • 17
  • 37
11
votes
1 answer

Why can't I use SetArgPointee() with googlemock?

I am trying to set an "out" parameter on a mock with SetArgPointee. Here's the code I'm testing: DWORD bodysize = 1024; char body[1024]; HRESULT hr = req->ReadEntityBody(body, bodysize, false, &bodysize, NULL); req is an IHttpRequest*, and I want…
ladenedge
  • 13,197
  • 11
  • 60
  • 117
11
votes
1 answer

Is it possible to capture parameters with Google Mock (gmock)?

I am planning on using Google Mock. I need to capture an object reference so that I can subsequently call some methods from that object. Does Google Mock have any capturing abilities? If not, what are the other choices for C++ unit testing? One…
Victor Lyuboslavsky
  • 9,882
  • 25
  • 87
  • 134
10
votes
1 answer

Why does Google Test/Mock show leaked mock object error by std::unique_ptr?

Let's assume that there is Bar object which uses a Foo object. The ownership is exclusive, so Bar gets Foo as a std::unique_ptr in its constructor. I would like to test Bar with Google Test framework, so I made a following code: using namespace…
Tibor Takács
  • 3,535
  • 1
  • 20
  • 23
10
votes
2 answers

Interleaving EXPECT_CALL()s and calls to the mock functions

The Google Mock documentation says: Important note: Google Mock requires expectations to be set before the mock functions are called, otherwise the behavior is undefined. In particular, you mustn't interleave EXPECT_CALL()s and calls to the mock…
Jordan
  • 1,375
  • 14
  • 17
10
votes
2 answers

set EXPECT_CALL to redirect the call to the original method

I have a class with several methods that depend on each other. Lets say foo(), bar() and baz(). When I test bar() I need to mock the behavior of foo(), when I test baz() I need to mock the behavior of bar(). If I mock bar for baz I cannot use the…
gsf
  • 6,612
  • 7
  • 35
  • 64
10
votes
1 answer

Google Mock Destructor

I'm trying to become familiar with Google's mocking framework so I can more easily apply some TDD to my C++ development. I have the following interface: #include class Symbol { public: Symbol (std::string name, unsigned long address)…
Bobby Vandiver
  • 306
  • 3
  • 11