#define mockable virtual in C ++

I found this code:

#define mockable virtual

Do you have an idea why someone will define a virtual like this? I'm just curious what to do about it

+3
source share
1 answer

So you can do:

class ClassIdLikeToTest{
    mockable void mymethod(){
         //Behavior I would like to be different in my tests
    }
}

And then define the layout as virtual for the test build so that you can override this method inheriting from the class.

You write your test and use a class that inherits from ClassIdLikeToTest and override mymethod and it will work as long as the layout is virtual, but then you can remove it for production assemblies and these functions will not be virtual and you pay for a virtual call.

+10
source

All Articles