开发者

#define mockable virtual in C++

开发者 https://www.devze.com 2023-02-16 12:10 出处:网络
I found this code: #define mockable virtual Do you have any idea why 开发者_高级运维somebody will define virtual like this? I\'m just curios what\'s the point of doing thisSo that you could do:

I found this code:

#define mockable virtual

Do you have any idea why 开发者_高级运维somebody will define virtual like this? I'm just curios what's the point of doing this


So that you could do:

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

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

You write your test and use a class that inherits from ClassIdLikeToTest and overrides mymethod, and it will work as long as mockable is virtual, but then you can remove it for production builds and those functions will not be virtual, and you don't pay for the virtual call.

0

精彩评论

暂无评论...
验证码 换一张
取 消