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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T14:12:32+00:00 2026-05-28T14:12:32+00:00

Is it possible to mock method calls from within other method calls of the

  • 0

Is it possible to mock method calls from within other method calls of the same class? I am new to C++ (primarily a C developer) and very new to googlemock and Google Test so forgive me if this is answered elsewhere and I didn’t understand the answer! Below is a simple example that should explain what I want to do. Using the example below, I want to mock ReturnInput, while testing ReturnInputPlus1.

using ::testing::Invoke;
using ::testing::_;
using ::testing::Return;

class MyClass
{
public:
    MyClass() : x(1) {}
    virtual ~MyClass() {}

    int ReturnInput(int x) { return x; }
    int ReturnInputPlus1(int x) { return ReturnInput(x) + 1; }
};

class MockMyClass : public MyClass
{
public:
    MockMyClass()
    {
        ON_CALL(*this, ReturnInput(_)).WillByDefault(Invoke(&real, &MyClass::ReturnInput));
        ON_CALL(*this, ReturnInputPlus1(_)).WillByDefault(Invoke(&real, &MyClass::ReturnInputPlus1));
    }
    virtual ~MockMyClass() {}

    MOCK_METHOD1(ReturnInput, int(int y));
    MOCK_METHOD1(ReturnInputPlus1, int(int));

private:
    MyClass real;
};

TEST(MyClassTest, mockUseClassMethod)
{
    MockMyClass mock;

    EXPECT_CALL(mock, ReturnInput(_))
        .WillOnce(Return(2));
    EXPECT_CALL(mock, ReturnInputPlus1(_))
        .Times(1);

    EXPECT_EQ(3, mock.ReturnInputPlus1(1));
}

When I run the above test, I would expect all the tests to succeed. Instead I get the following output:

1>[ RUN      ] MyClassTest.mockUseClassMethod
1>.\MyClassTest.cpp(15): error: Value of: mock.ReturnInputPlus1(1)
1>  Actual: 2
1>Expected: 3
1>.\DllInfoTest.cpp(10): error: Actual function call count doesn't match EXPECT_CALL(mock, ReturnInput(_))...
1>         Expected: to be called once
1>           Actual: never called - unsatisfied and active
  • 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-28T14:12:33+00:00Added an answer on May 28, 2026 at 2:12 pm

    Your call to MockMyClass::ReturnInputPlus1(1) calls MyClass::ReturnInputPlus1(1) on real by default, which calls MyClass::ReturnInput(2). The object that ReturnInputPlus1() is running on is not mock, it’s mock.real, and when it calls ReturnInput() it calls the original version of the function. What you want to do is create a MockMyClass that has a mock override of ReturnInput() but doesn’t mock out ReturnInputPlus1() at all — you don’t want to have both a mock version of your class and a real version that it sometimes delegates to.

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

Sidebar

Related Questions

Is it possible to mock the Assembly class? If so, using what framework, and
Is it possible to Mock a Linq Expression via Moq using a Generic class
I want to mock an instance method for all instances of a class using
Is it possible to mock an object in a way that it fakes multiple
I was wandering if it's possible to mock a Game object in order to
Possible Duplicates: What is the best approach for a Java developer to learn C++
As some of you will know, it is generally not possible to mock a
Mockito api provides method: Mockito.verifyNoMoreInteractions(someMock); but is it possible in Mockito to declare that
I have a method I'd like to mock: public interface IServiceBus { void Subscribe<T>(ISubscribeTo<T>
I have a one method interface and a class mocking that interface. The method

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.