Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6026721
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T04:29:12+00:00 2026-05-23T04:29:12+00:00

Just getting into Unit Testing with C++. It looks like I will need to

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T04:29:13+00:00Added an answer on May 23, 2026 at 4:29 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm just getting into unit testing, and have written some short tests to check
I'm just getting into the practice of version control (I'd like to use Eclipse
I am just getting into PHP classes (OK it took me years ...... )
I'm just getting into creating some WCF services, but I have a requirement to
I am just getting into ibatis with SqlMap for the first time and I
My little brother is just getting into programming, and for his Science Fair project,
I'm just getting into ASP.NET Been avoiding it for years as I'm a desktop
I just started getting into BizTalk at work and would love to keep using
I have just been getting into low level programming (reading/writing to memory that sort
I'm just getting back into C++ after a couple of years of doing a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.