问题
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