Alex Rivera | Logout

fake/mock nonvirtual C++ methods

Asked 2010-11-25T16:19:55.040
28

It known that in C++ mocking/faking nonvirtual methods for testing is hard. For example, cookbook of googlemock has two suggestion - both mean to modify original source code in some way (templating and rewriting as interface).

It appear this is very bad problem for C++ code. How can be done best if you can't modify original code that needs to be faked/mocked? Duplicating whole code/class (with it whole base class hierarchy??)

Edit
Report

1 Answer

-2

I used to create an interface for the parts I needed to mock. Then I simply created a stub class that derived from this interface and passed this instance to my classes under test. Yes, it is a lot of hard work, but I found it worth it for some circumstances.

Oh, by interface I mean a struct with only pure virtual methods. Nothing else!

answered 2010-11-25T16:26:49.447

Your Answer