fake/mock nonvirtual C++ methods

后端 未结 8 1654
谎友^
谎友^ 2020-12-13 19:32

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

8条回答
  •  [愿得一人]
    2020-12-13 20:16

    You very specifically say "if you can't modify original code", which the techniques you mention in your question (and all the other current "answers") do.

    Without changing that source, you can still generally (for common OSes/tools) preload an object that defines its own version of the function(s) you wish to intercept. They can even call the original functions afterwards. I provided an example of doing this in (my) question Good Linux TCP/IP monitoring tools that don't need root access?.

提交回复
热议问题