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

Uninteresting mock function call bla() && Expected: to be called at least once bla()?

I've written a small test with a mocked class. When I run it, first I get the warning that an uninteresting mock function was called and then the test fails because the expectation is not met, which is that the mocked function is called at least…
emmerich
  • 512
  • 1
  • 5
  • 14
16
votes
4 answers

C++ Unit Testing: Stubs (not mocks)?

Just getting into Unit Testing with C++. It looks like I will need to write several stub classes as I go along. My understanding is there is a difference between Mocks and Stubs. Basically it seems Mocks are for when you are testing something…
User
  • 62,498
  • 72
  • 186
  • 247
16
votes
1 answer

Google mock ByRef method

I have a class that takes a boolean as a reference parameter and returns an integer: class Foo { public: Bar my_bar; virtual int myMethod(bool &my_boolean) = 0; } /*...*/ int Foo::myMethod(bool &my_boolean){ if (my_bar == NULL){ …
Rodrigo Vasconcelos
  • 1,270
  • 2
  • 14
  • 26
15
votes
3 answers

Unit Testing: coding to interfaces?

Currently my project is composed of various concrete classes. Now as I'm getting into unit testing it looks like I'm supposed to create an interface for each and every class (effectively doubling the number of classes in my project)? I happen to be…
User
  • 62,498
  • 72
  • 186
  • 247
15
votes
1 answer

How to inspect argument to a gmock EXPECT_CALL()?

I'm using Google Mock (gMock) for the first time. Given the following code snippet: class LinkSignals { public: virtual ~LinkSignals() { } virtual void onLink(std::string) = 0; virtual void onUnLink() = 0; }; class…
Chimera
  • 5,884
  • 7
  • 49
  • 81
15
votes
1 answer

Google Mock - how to name mock functions?

I am just getting started with Google Mock. The For Dummies is reasonably easy to follow. However, I don't understand why the example has class MockTurtle : public Turtle { public: ... MOCK_METHOD0(PenUp, void()); MOCK_METHOD0(PenDown,…
Mawg says reinstate Monica
  • 38,334
  • 103
  • 306
  • 551
15
votes
4 answers

Google Mock and Catch.hpp Integration

I really like catch.hpp for testing (https://github.com/philsquared/Catch). I like its BDD style and its REQUIRE statements, its version of asserts. However, catch does not come with a mocking framework. The project I'm working on has GMock and…
Erock 634
  • 591
  • 3
  • 18
15
votes
1 answer

Why the compiler does not recognize Google Mock wildcard?

When I try to use: ON_CALL(mock, foo(_)) Compilation error is thrown: Error 1 error C2065: '_' : undeclared identifier I am using visual studio 2012.
User234
  • 237
  • 1
  • 2
  • 9
15
votes
2 answers

Passing arbitrary arguments to invocked methods with Google C++ Mocking Framework (Google Mock) (V1.5)

I have a mock method. When it is called, I'd like it to call another function before calling its normal behavior. Something like : EXPECT_CALL(*my_obj, MockedMethod(_,_,_,_,_,_)) .WillOnce(DoAll( Invoke(my_obj,…
SylvainD
  • 1,743
  • 1
  • 11
  • 27
15
votes
2 answers

Compare containers with GoogleTest

I'm trying to get a working googletest test that compares two vectors. For this I'm using google mock with its matchers but I get a C3861 error saying "ContainerEq identifier not found" and also C2512 saying "testing::AssertionResult has not a…
Stefano
  • 3,213
  • 9
  • 60
  • 101
14
votes
3 answers

Compare Eigen matrices in Google Test or Google Mock

I was wondering if there is a good way to test two Eigen matrices for approximate equality using Google Test, or Google Mock. Take the following test-case as a simplified example: I am multiplying two complex valued matrices A, and B, and expect a…
Lemming
  • 4,085
  • 3
  • 23
  • 36
13
votes
2 answers

Automatic generation of mock classes for gmock

I am using gmock for unit testing C++ code. I am not using the gtest framework. I am using visual studio 2008's built-in testing framework. Now my problem is that I have to manually write mock classes for a real class to unit test. For example if I…
fhnaseer
  • 7,159
  • 16
  • 60
  • 112
13
votes
2 answers

Using GMock to verify a Destructor Call

Using GMock, how can I verify that a class's destructor is called? Is there a way, other than to wrap it in another class? The obvious method, EXPECT_CALL(object, ~classtype()) yields a compiler error (gmock cannot produce a mock method called…
Andres Jaan Tack
  • 22,566
  • 11
  • 59
  • 78
13
votes
2 answers

How to mock methods return object with deleted copy-ctor?

If an interface has a function to create an object with deleted copy-ctor, how to mock this function? Gmock seems to use the object's copy constructor internally. E.g. // The object with deleted copy-ctor and copy-assignment class TTest { public: …
Mine
  • 4,123
  • 1
  • 25
  • 46
13
votes
3 answers

Dependency injection with unique_ptr to mock

I have a class Foo that uses class Bar. Bar is used only in Foo and Foo is managing Bar, therefore I use unique_ptr (not a reference, because I don't need Bar outside of Foo): using namespace std; struct IBar { virtual ~IBar() = default; …
Quarra
  • 2,527
  • 1
  • 19
  • 27
1 2
3
74 75