Using google mock for C code

后端 未结 3 1945
无人及你
无人及你 2020-12-05 10:41

I\'m maintaining a legacy project written in C and it\'s unfeasible to get it running with a C++ compiler. Since the code is cross compiled it is however possible to run uni

3条回答
  •  鱼传尺愫
    2020-12-05 11:26

    As from the Google Mock FAQ:

    My code calls a static/global function. Can I mock it?
    You can, but you need to make some changes.

    In general, if you find yourself needing to mock a static function, it's a sign that your modules are too tightly coupled (and less flexible, less reusable, less testable, etc). You are probably better off defining a small interface and call the function through that interface, which then can be easily mocked. It's a bit of work initially, but usually pays for itself quickly.

    This Google Testing Blog post says it excellently. Check it out.

提交回复
热议问题