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 7765733
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T15:14:17+00:00 2026-06-01T15:14:17+00:00

In our project we are using Google Mock, but in multiple places we are

  • 0

In our project we are using Google Mock, but in multiple places we are making contructions in our production code, only to make sure classes are “mockable”. We do this because we want to have the benefits of Google Mock, but on the other side we would rather have more optimal production code. The following case is something we do often, and would like to get rid off.

class A{
  public:
    void doSomething(); //Does something with _someB
    void setSomeB(B* mockedB); //This is only here for GMock
  private:
    B* _someB; //This should not be a pointer, it is a pointer only for GMock
}

This is only a simplified example as you can see, I left out the details. Basically we want to get rid of B being a pointer. The reason we have it as a pointer, is that we can subclass B in our test code (mock it) and set it with that setter.

Is there any way to avoid this? Can’t we let B be scoped in the class?

Thanks

  • 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-06-01T15:14:18+00:00Added an answer on June 1, 2026 at 3:14 pm

    If you really don’t want _someB held as a pointer, an option would be to wrap its declaration in preprocessor commands:

    #ifdef MOCK_TEST
        B _someB;
    #else
        MockB _someB;
    #endif
    

    If you don’t, there’s no easy way to place gmock expectations and/or assertions on _someB, since it will not be an instance of a mock object. Apart from the ugliness of this, it also has the disadvantage of requiring the lib in which Class A resides to be compiled twice, once with MOCK_TEST defined and once without. It does however do away with the need for a setter for _someB.

    Even if you choose to keep _someB as a pointer, perhaps a better option than creating a public setter is to declare the test class a friend class of A‘s to allow the test to directly set and access _someB.

    As an aside, it’s often considered poor form to name variables with a leading underscore.

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

Sidebar

Related Questions

In my SVN folder someone deleted our project source code. Using SVN history we
I'm using google's cpplint.py to verify source code in my project meets the standards
We want to be able to deploy our project using Hudson, but the build
Last year I developed a data access service for our project using Entity Framework
in our current project we are using ADO.NET Entity Framework as data layer for
Our issue is that our project has files being downloaded using wget to the
We are using jQuery in our project. We have numerous custom javascript files in
How to improve our productivity when starting new project using Delphi? For me, I
I'm using this jquery plugin ajaxFileupload in our project. My design is I have
We are currently using unit tests to test our project. We have the majority

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.