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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:11:17+00:00 2026-05-28T05:11:17+00:00

I know the first part of this question has been asked before , but

  • 0

I know the first part of this question has been asked before, but that was a long time ago :). I was wondering if the mean time any of the open source mocking frameworks caught up with Typemock Isolator++ when it comes to mocking non-virtual methods and C functions. I’m mostly interested in gcc under Linux. So far, I’m interested in mocking accessors (so that I can simulate states in the mocked object – see below) and replacing C functions from other libraries (select, pcap_*, etc.).

class Foo {
  public:
    ...
    bool IsCondition() { return condition; };
    ...
  private:
    bool condition;
}

// I want a framework that allows me to do something like this:
TEST(TestFoo) {
    MOCK_INTERFACE(Foo) mock_foo;
    EXPECT_CALL(mock_foo, IsCondition).returns(true);
    EXPECT(mock_foo.IsCondition()); 
}
  • 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-28T05:11:18+00:00Added an answer on May 28, 2026 at 5:11 am

    GMock supports what they call hi-perf dependency injection for mocking non-virtual methods.

    The gist, from the link above, is to use templates:

    template <class PacketStream>
    void CreateConnection(PacketStream* stream) { ... }
    
    template <class PacketStream>
    class PacketReader {
     public:
      void ReadPackets(PacketStream* stream, size_t packet_num);
    };
    

    Then you can use CreateConnection() and PacketReader in production code, and use CreateConnection() and PacketReader in tests.

    For C functions they recommend interfaces, so probably not what you want. However, if you have separate libraries you could always link against a testing library, which contains functions with the same signatures as the deployment library. You could even do it dynamically with LD_PRELOAD if you were feeling particularly daring. That sounds like a lot of linking to me.

    Cxxtest, if you look under section 8.1 in advanced features supports some macros to make using/creating interfaces easier:

    From that link:

    CXXTEST_MOCK_GLOBAL( time_t,        /* Return type          */  
                         time,          /* Name of the function */  
                         ( time_t *t ), /* Prototype            */
                         ( t )          /* Argument list        */ );
    
    8.1.2. Mock Functions in Tested Code
    

    The tested code uses mock global functions, rather than using the global functions directly. You access mock functions in the T (for Test) namespace, so the tested code calls T::time() instead of time(). This is the equivalent of using abstract interfaces instead of concrete classes.

    // rand_example.cpp
    #include <time_mock.h>
    
    int generateRandomNumber()
    {
        return T::time( NULL ) * 3;
    }
    

    I’ve had good luck with the Cxxtest approach in the past.

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

Sidebar

Related Questions

I know this type of question has been asked before, but I could not
First of all, I know I asked a similar question before but this one
I know this question has been asked multiple number of times and i have
This is a two-part question: First, I am interested to know what the best
Let's get this out of the way first: I know that SessionFactory is immutable
Edit: I think this has been answered successfully, but I can't check 'til later.
I have this homework question: Assume a Database table has 0 records at time
First off, let me preface this question by stating that I'm really a pretty
I understand this is a basic question... but I have been stuck on it
First of all, I want to say sorry for this very big question, but

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.