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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T20:31:07+00:00 2026-05-29T20:31:07+00:00

I have 2 classes. class SomeClass { public: int SomeFunction() { return 5; }

  • 0

I have 2 classes.

class SomeClass
{
public:
    int SomeFunction()
    {
        return 5;
    }
};


class AnotherClass
{
public:
    int AnotherFunction(SomeClass obj)
    {
        return obj.SomeFunction();
    }
};

I have made a mock class for SomeClass.

class MockSomeClass : public SomeClass
{
public:
    MOCK_METHOD0(SomeFunction, int());
};

Now I want in unit test that when i call AnotherClass.AnotherFunction i get result of my own choice. AnotherFunction uses function of SomeClass.SomeFunction(). I have mocked SomeClass. And I have set that when function of mocked object calls it returs 10. But when i run unit test it returns 5 (origional function). What should i do. Below is the unit test which i have written.

[TestMethod]
    void TestMethod1()
    {
        MockSomeClass mock;
        int expected = 10;
        ON_CALL(mock, SomeFunction()).WillByDefault(Return(expected));
        AnotherClass realClass;
        int actual = realClass.AnotherFunction(mock);           
        Assert::AreEqual(expected, actual);
    };

I am using visual studio 2008 and gmock 1.6.0. What is wrong I am doing. on realClass.AnotherFunction i want mocked output from mock.SomeFunction().

  • 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-29T20:31:09+00:00Added an answer on May 29, 2026 at 8:31 pm

    The problem is that SomeClass::SomeFunction(…) isn’t virtual, make it virtual and it will work.

    Update:

    There is one more fundamental error that causes it to fail, that is the method signature of

    int AnotherFunction(SomeClass obj)
    

    which creates a new SomeClass object instance which will in it’s turn cause the normal SomeFunction to be called, you should instead pass a reference to the mocked class as argument.

    int AnotherFunction(SomeClass* obj)
    

    and invoke it using

    int actual = realClass.AnotherFunction(&mock);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have seen some C++ classes with a destructor defined as follows: class someClass
I have two classes class A { public string something { get; set; }
Say I have three classes: class X{}; class Y{}; class Both : public X,
Let's say i have 2 classes: class Class1 { public: std::vector<CustomClass3*> mVec; public: Class1();
I have two classes: public class Singleton{ private Singleton(){...} private static class InstanceHolder{ private
I have 2 Hibernate classes in a Spring-driven Application like these: @Entity public class
I have two classes : class A { public function load() { $var =
I have these classes: class Base { ... private: std::vector<X> v; }; class Derived
If I have three classes class A class B extends A class C extends
Hi i have these classes: class Core < ActiveRecord::Base belongs_to :resource, :polymorphic => true

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.