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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T11:41:03+00:00 2026-06-02T11:41:03+00:00

I’m developing code that uses boost::asio . To test it, I need to mock

  • 0

I’m developing code that uses boost::asio. To test it, I need to mock a set of classes from this library. I’m using Google Mock, which allows for mocking virtual methods. The usual (and tedious) process would be to write an interface for each of the classes I need to use.

On the other hand, the Google Mock Cookbook describes an alternative when it comes to mocking non-virtual methods: using templates. The problem in my case is that I might need to mock several classes at the same time (so using templates directly wouldn’t work). So I thought: why not use two-levels of templates? I came up with the following solution:

// Classes to be mocked.

class RealA
{
public:
    void a() { cout << "RealA::a()" << endl; };
};

class RealB
{
public:
    void b() { cout << "RealB::b()" << endl; };
};

// Mock classes.

class MockA
{
public:
    void a() { cout << "MockA::a()" << endl; };
};

class MockB
{
public:
    void b() { cout << "MockB::b()" << endl; };
};

template<class ABFactory>
class Program
{
public:
    void setFactory(ABFactory* factory) { factory = factory; }
    void useA() { typename ABFactory::A* a = factory->createA(); a->a(); delete a; }
    void useB() { typename ABFactory::B b; b.b(); }

private:
    ABFactory* factory;
};


template<class ParamA, class ParamB>
class TABFactory
{
public:
    typedef ParamA A;
    typedef ParamB B;
    A* createA() { return new A; };
    B* createB() { return new B; };
};

typedef TABFactory<RealA, RealB> RealABFactory;
typedef TABFactory<MockA, MockB> MockABFactory;

Then, the normal usage would be:

Program<RealABFactory> p;
p.useA();
p.useB();

While the test would be:

Program<MockABFactory> t;
t.useA();
t.useB();

This starts to get complicated when the mocked classes have methods with complex parameters (such as other classes from the same library which might not be mocked). In summary, it doesn’t seem to scale. Any thoughts on this solution, or suggestions on other approaches to the problem?

  • 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-02T11:41:04+00:00Added an answer on June 2, 2026 at 11:41 am

    this is a perfect use case for python cog

    see also this answer.

    I’ve used cog to generate handlers for a list of events, the handler code is very generic and i don’t need to do special cases but i still have to write all the functions, so what i did is keep the events in a list in a .py file and the code to generate the boilerplate of the handlers in a python function. so i’m able to be faithful to the DRY principle

    obviously you’ll had to add cog to the pre-build of your makefile in order to work with your toolchain

    Edit as an example of the code generation design required for adding boilerplate to your classes, i would do something like:

    myCodeGeneration.py

    import cog
    ClassesToMock = [ [ 'IfaceA' , 'classA' , 'mockA' , ['void doSomething(int foo)' , 'int getSomething()'] 
                     , [ 'IfaceB', 'classB' , 'mockB' , ['static classA& getInstance()'] ]
    
    def addInterfaces( myStructure ):
       for classItem in myStructure:
          cog.outl('class %s { ' % classItem[0] )
          for methodDecl in classItem[3]:
             cog.outl(' virtual %s = 0;' %methodDecl )
          cog.outl(' } ')
    
    #implement your real classes normally
    
    def addMocks( myStructure ):
       for classItem in myStructure:
          cog.outl('class %s : public %s { ' % classItem[2] % classItem[0] )
          for methodDecl in classItem[3]:
             cog.outl(' %s {' %methodDecl )
             cog.outl(' MOCK_STUFF_MACRO ')
             cog.outl(' } ')
          cog.outl(' } ')
    

    then in your header:

    IfaceA.h

    /*[[[cog
    import cog
    import myCodeGeneration
    
    myCodeGeneration.addInterfaces( [ [ 'IfaceA' , 'classA' , 'mockA' , ['void doSomething(int foo)' , 'int getSomething()'] ] )
    ]]]*/
    
    //your code will be generated here
    
    //[[[end]]]
    

    mockA.h

    /*[[[cog
    import cog
    import myCodeGeneration
    
    myCodeGeneration.addMocks( [ [ 'IfaceA' , 'classA' , 'mockA' , ['void doSomething(int foo)' , 'int getSomething()'] ] )
    ]]]*/
    
    //your code will be generated here
    
    //[[[end]]]
    

    Also, the issue if considering adding python to your c++ source is ‘polluting’ it or ‘beautifying’ it is largely a matter of taste and style. I consider that cog provides a complement to template style metaprogramming that is lacking in c++, giving the tools to the programmer to provide code tidiness and readability. But i don’t expect everyone to agree

    For me the whole architectural design principle behind this approach is Don’t Repeat Yourself. errors happen when we have to encode a method in several places manually. Let the computer automate what its automatable and encode things that cannot just once. As a side-effect, it will make your coding more enjoyable, both for writing it and reading it later.

    hope this helps

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
Does anyone know how can I replace this 2 symbol below from the string
I need a function that will clean a strings' special characters. I do NOT
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
this is what i have right now Drawing an RSS feed into the php,

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.