Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is Google Mock a good mocking framework? [closed]

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 impressions after looking at Google Mock's tutorial are:

  • The need for re-declaring each method in the mocking class with a MOCK_METHODn macro seems unnecessary and seems to go against the DRY principle.
  • Their matchers (eg, the '_' in EXPECT_CALL(turtle, Forward(_));) and the order of matching seem almost too powerful. Like, it would be easy to say something you don't mean, and miss bugs that way.

I have high confidence in google's developers, and low confidence in my own ability to judge mocking frameworks, never having used them before. So my question is: Are these valid concerns?

Or is there no better way to define a mock object, and are the matchers intuitive to use in practice? I would appreciate answers from anyone who has used Google Mock before, and comparisons to other C++ frameworks would be helpful.

like image 846
des4maisons Avatar asked May 13 '10 13:05

des4maisons


People also ask

How does Google mock work?

Using Multiple Expectations By default, when a mock method is invoked, Google Mock will search the expectations in the reverse order they are defined, and stop when an active expectation that matches the arguments is found (you can think of it as “newer rules override older ones.”).

What is gMock used for?

gMock is a library (sometimes we also call it a “framework” to make it sound cool) for creating mock classes and using them. It does to C++ what jMock/EasyMock does to Java (well, more or less).

Does Gtest include gMock?

gMock is bundled with googletest.

Why do we need mocking framework?

The benefits of a mocking framework are: Easier (subjective, but after a while you will not write hand written implementations) Less Code (frameworks allow you to create a mock in a matter of lines, rather than full class declarations) Follows DRY (you won't end up repeating mock implementations)


2 Answers

I use it frequently.

It's trivial to do relatively easy things, and possible to do very difficult things - that's pretty much what I want from a framework.

The hardest part about writing custom Matchers (and other stuff) with Google's mocks isn't Google's mocks, it's C++'s template errors... they're close to impossible to parse. I often write complex expressions by incrementally building a working expression from a few less complicated expressions. That way, the template errors are easier to pinpoint.

I haven't seen a better option for c++ mocking, and Google's covers a lot of ground, so I'd suggest you give it a shot.

WRT the DRY principle, I agree the declaring the mocked methods is unfortunate, but without reflection, I'm not sure c++ would have much luck otherwise. I'm near certain if there were a way, googlemock would be using it ;)

BTW: The googlemock cookbook is a good reference.

like image 182
Stephen Avatar answered Sep 21 '22 17:09

Stephen


Fake-It is a simple mocking framework for C++. FakeIt uses the latest C++11 features to create an expressive (yet very simple) API. With FakeIt there is no need for re-declaring methods nor creating a derived class for each mock. Here is how you Fake-It:

struct SomeInterface {   virtual int foo(int) = 0; };  // That's all you have to do to create a mock. Mock<SomeInterface> mock;   // Stub method mock.foo(any argument) to return 1. When(Method(mock,foo)).Return(1);  // Fetch the SomeInterface instance from the mock. SomeInterface &i = mock.get();  // Will print "1" cout << i.foo(10); 

There are many more features to explore. Go ahead and give it a try.

like image 45
Eran Pe'er Avatar answered Sep 18 '22 17:09

Eran Pe'er