GMock and mocking constructors in a class with non virtual methods

巧了我就是萌 提交于 2019-12-24 02:33:15

问题


i need to mock a class that has only non virtual methods. This class has a copy constructor. How to I write a mock method for that. I get a compiler error if I just use the

MOCK_METHOD1(classname, void(classname& source)); 

Thanks in advance.


回答1:


You can't mock non-virtual functions with gmock. So the first alternative to consider is to make the functions virtual. If you are concerned with performance overhead of making the functions virtual make sure that this really is a problem (by measuring), cause generally it ain't.

An alternative solution if there is no possibility to make the functions virtual is to use templates. See this question for details on this technique and this question for pros and cons of using it.



来源:https://stackoverflow.com/questions/6034031/gmock-and-mocking-constructors-in-a-class-with-non-virtual-methods

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!