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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T00:52:33+00:00 2026-06-10T00:52:33+00:00

I wrote the following piece of code to test Xyz::xyz_func by mocking Abc::abc_func using

  • 0

I wrote the following piece of code to test Xyz::xyz_func by mocking Abc::abc_func using Google Mock.

#include <iostream>
#include "gmock/gmock.h"
#include "gtest/gtest.h"

using namespace std;
using ::testing::_;
using ::testing::Return;

class Abc
{
    public:
        virtual ~Abc() {}

        virtual bool abc_func(int arg)  = 0;
};

class MockAbc : public Abc
{
    public:
        virtual ~MockAbc() { }

        MOCK_METHOD1(abc_func,  bool(int arg));
};

class AbcImpl : public Abc
{
    public:
        virtual bool abc_func(int arg)
        {
            cout << arg << " :: " << __FILE__ << " :: " << __LINE__ << endl;
            return true;
        }
};

class Xyz : public AbcImpl
{
    public:
        virtual ~Xyz() {}
        virtual bool xyz_func()
        {
            AbcImpl obj;
            return obj.abc_func(1);
        }
};

TEST(AbcTest, func_success)
{
    MockAbc *mock = new MockAbc();

    EXPECT_CALL(*mock,  abc_func(_)).WillOnce(Return(true));

    Xyz test;

    EXPECT_TRUE(test.xyz_func());

    delete mock;
}

int main(int argc, char** argv)
{
    ::testing::InitGoogleMock(&argc, argv);
    return RUN_ALL_TESTS();
}

But i get the below error. I want to know how we can tell Xyz class to call mock of abc_func instead of actual implementation. Could you please help me.

1 :: ./gmock_test.cpp :: 30
./gmock_test.cpp:50: Failure
Actual function call count doesn't match EXPECT_CALL(*mock, abc_func(_))...
         Expected: to be called once
           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-06-10T00:52:34+00:00Added an answer on June 10, 2026 at 12:52 am

    You are creating a mock object of type MockAbc but you never use it. From your code it appears that you’re expecting the MockAbc instance you create to automagically replace the AbcImpl object that you explicitly create in Xyz::xyz_func.

    What you need to be doing instead is using dependency inversion to allow the object used by xyz_func to be specified at runtime.

    You don’t need Xyz to inherit from AbcImpl or Abc.


    Two potential solutions:

    class Xyz {
    public:
        explicit Xyz(Abc& obj) : 
            obj(&) {
        }
    
        virtual bool xyz_func() {
            return obj->abc_func(1);
        }
    private:
        Abc* obj;
    };
    

    or

    class Xyz {
    public:
        virtual bool xyz_func(Abc& obj) {
            return obj->abc_func(1);
        }
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's look at the following piece of code which I unintentionally wrote: void test
I wrote the following piece of code to test my understanding of virtual inheritance.
I wrote following piece of code to retrieve value of 'recipientList' in array abc
I'm using the following piece of code to get the content of a webpage
I wrote the following piece of code and I believed it would crash if
I wrote the following piece of code on codepad but I do not understand
I wrote the following piece of code: -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ GameViewController *gameViewController =
I wrote the following piece of code and I think it's lame: def findfile1(wildcard):
Given the following piece of code: using(var data = new SomeDataContext(ConnectionString)) { data.ObjectTrackingEnabled =
Following is the piece of code that i wrote to calculate the nth prime

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.