开发者

GMock and mocking constructors in a class with non virtual methods

开发者 https://www.devze.com 2023-03-06 18:42 出处:网络
i need to mock a class that has only non virtual methods. This 开发者_StackOverflow中文版class

i need to mock a class that has only non virtual methods. This 开发者_StackOverflow中文版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.


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.

0

精彩评论

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