16

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 happened on the object (e.g. verifying) while Stubs just facilitate your test. I guess for mocking, I can use googlemock but I don't see anything in it for creating Stubs (ala RhinoMocks' GenerateStub).

Is there a way to get automatically generated stubs? Does googlemock have any support for stubs? Or do I pretty much have to manually create stubs for testing?

User
  • 62,498
  • 72
  • 186
  • 247
  • Not sure but I hear boost has a unit test library. Unfortunately I have to concede with aloneguid I have never written code for unit testing. – AJG85 Jun 14 '11 at 23:42
  • Isn't there some sort of generator python script which can generate some mock classes? – Nim Jun 14 '11 at 23:52
  • 1
    @aloneguid: Seriously? I don't believe that. Real C++ people use whatever tests are appropriate for their application. – Billy ONeal Jun 14 '11 at 23:52
  • @Nim: There's no reason to do that when everything is possible in plain C++. – Billy ONeal Jun 14 '11 at 23:53
  • @Billy, of course, I guess it's for the really lazy! ;) – Nim Jun 14 '11 at 23:54
  • @Nim: I believe you are correct googlemock has a generator script, gmock_gen.py, which will take a c++ class file and attempt to print out the mock class definition for you. – User Jun 21 '11 at 05:51

4 Answers4

20

I think the missing piece of the puzzle is that you don't have to set an expectation on a method and instead you can just set a default return value.

Mocks

All the discussion and examples in the "Google Mock for Dummies" is focused around setting expectations. Everything talks about using some code similar to the following:

EXPECT_CALL(turtle, PenDown())
      .Times(AtLeast(1));

Which is what you want for mocking, but for stubbing you don't have any expectations. After reading that intro I had no clue how to use googlemock for stubbing.

Stubs

ratkok's comment led me to find out how to set a default return value. Here's how to specify a return value for a mocked object but no expectation:

ON_CALL(foo, Sign(_))
      .WillByDefault(Return(-1));

https://github.com/google/googletest/blob/master/docs/gmock_cook_book.md#setting-the-default-actions-for-a-mock-method

It appears googlemock will issue a warning if you call a method that has no EXPECT_CALL. Apparently you can prevent this warning by using their NiceMock construct or you can just ignore it. Additionally it appears you can avoid the warning by using an expect instead (which I'm not sure if it's a good idea for stubs). From the Google Mock FAQ:

EXPECT_CALL(foo, Bar(_))
    .WillRepeatedly(...);

I believe that is exactly what I was trying to figure out.

Update

I can confirm this works. I wrote a unit test using google test along with googlemock and was able to stub out a method for a class using ON_CALL.

DerKasper
  • 167
  • 2
  • 11
User
  • 62,498
  • 72
  • 186
  • 247
  • Yes, using ON_CALL() and NiceMock is exactly what I was referring too. Check those wiki pages (google mock) - this mocking framework is really quite powerful and flexible. – ratkok Jun 16 '11 at 04:15
4

The only difference between a Mock and a Stub is that a Mock enforces behavior, while a Stub does not.

As far as I am aware, Google Mock's mocks are actually stubs by default. They only enforce behavior if you place assertions on the various methods.

Billy ONeal
  • 104,103
  • 58
  • 317
  • 552
  • I guess what I'm confused about is I want to define a stub behavior. For example a method IsValidUser() that will return true. I don't want to create an expecation on IsValidUser because I want to test something else and IsValidUser is merely a dependency to test another method. But everything in googlemocks seems to be oriented around creating an expectation – User Jun 15 '11 at 05:40
  • 1
    You can avoid setting expectations in google mock by providing default values/behavior. Also, google mock framework let you ignore unexpected calls so your tests will not fail if the expectation does not exist. – ratkok Jun 15 '11 at 12:49
0

Take a look at this: stubgen and the similar discussion here.

This question maybe useful/related as well.

Regarding google mocks - we use it on my current project to fully automate stub implementation. Virtually the entire stub code base is implemented using google mocks.

Community
  • 1
  • 1
ratkok
  • 739
  • 9
  • 15
0

https://github.com/coolxv/cpp-stub

//for linux and windows
#include<iostream>
#include "stub.h"
using namespace std;
int foo(int a)
{   
    cout<<"I am foo"<<endl;
    return 0;
}
int foo_stub(int a)
{   
    cout<<"I am foo_stub"<<endl;
    return 0;
}


int main()
{
    Stub stub;
    stub.set(foo, foo_stub);
    foo(1);
    return 0;
}
coolxv
  • 9
  • 1