Can gmock be used for stubbing C functions?

前端 未结 6 1763
时光取名叫无心
时光取名叫无心 2020-12-05 15:25

I am new to gmock, so I want to know how can I stub simple C function called in a function under test for Unit Testing.

Example:

int func(int a)
{
           


        
6条回答
  •  北海茫月
    2020-12-05 15:26

    I had a similar case in a project I was unit-testing. My solution was to create two make files, one for production and one for testing.

    If the function func_1() is definded in the header a.h, and implemented in a.cpp, then for testing you can add a new source file a_testing.cpp, that will implement all the functions in a.h as a stub. For unittesting, just compile and link with a_testing.cpp instead of a.cpp and the tested code will call your stub.

    In a_testing.cpp you can then forward the call to a gmock object that will set expectations and actions as usual based on the state and paramteres.

    I know it's not perfect, but it works ans solve the problem without changing production code or interfaces at all.

提交回复
热议问题