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

  • Home
  • SEARCH
  • 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 647029
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T21:40:40+00:00 2026-05-13T21:40:40+00:00

Suppose I have a class A which depends on 3 other classes X, Y

  • 0

Suppose I have a class A which depends on 3 other classes X, Y and Z, either A uses these through a reference or a pointer or say A is templated to be instantiated with X, Y and Z doesn’t matter, the key is that in order to test A, I need to have X, Y and Z.

So I need to have fakes for A, B and C. Suppose I write them. Now, how do I swap real and fake objects easily? I can see that this works very easily in the case of templates. In order to make it work when A depends on X, Y and Z through a reference or a pointer, I would need to have a base class say X_Interface from which I can inherit X_Real and X_Fake.

So basically, I would end up in having 3 times the number of classes for every class that would need to have a fake.

I am most likely missing something. There has to be a simpler way to do this. Having a base class X_Interface is also quite expensive as I will be using more space and making virtual calls. I guess I could use CRTP as I know whether its a X_Real or X_Fake at compile time but still there must be a better way.

  • 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-13T21:40:41+00:00Added an answer on May 13, 2026 at 9:40 pm

    First make sure that the objects you depend on (X, Y & Z) are passed in at the constructor, this way you can easily pass in ‘fakes’ when you are testing. (I really hope you are using a unit test framework, like CUnit)

    Now when you are writing the test, all you need to do is make up ‘fakes’ for the objects the class under test depends on. Often you can just pass in ‘null’, but if you really need to do something with that ‘fake’ you can simply instantiate it (you made sure all its dependencies are passed in at the constructor too, right? right!).

    Or in an extreme case you might extend the class that is depended upon and reimplement some key methods to suit your test.

    So now in most cases you don’t need a ‘fake’, sometimes you simply instantiate a real ‘fake’, and in rare cases you have an extra class that resides only in your test code.

    Look at this video for a much better explanation.

    There are more on this topic from GoogleTechTalks, but you’ll manage from here.

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

Sidebar

Related Questions

Suppose I have a LimitedValue class which holds a value, and is parameterized on
Good night, Let's suppose I have a class which implements a NFA/DFA whose transitions
Let's suppose we have a base class which has a virtual method: class BaseClass
Suppose I have a class module clsMyClass with an object as a member variable.
Suppose I have a class with some attributes. How is it best (in the
Suppose I have a class 'Application'. In order to be initialised it takes certain
Suppose I have a class public class MyClass { private Set<String> set = new
For example, suppose I have a class: class Foo { public: std::string& Name() {
Suppose I have a base class B, and a derived class D. I wish
Suppose I have some per-class data: (AandB.h) class A { public: static Persister* getPersister();

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.