Can gmock be used for stubbing C functions?

前端 未结 6 1779
时光取名叫无心
时光取名叫无心 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条回答
  •  旧时难觅i
    2020-12-05 15:39

    In each UT we are trying to verify a specific behavior.

    You should fake something when it's very hard/impossible(we need to isolate our unit)/spend a lot of time(running time..) to simulate a specific behavior.

    Using a 'C' function in the explicit way means that the function is apart of your unit(therefore you shouldn't mock it..). In this answer I explain the initiative to test the method as is(in the edit..). In my opinion you should call func with the parameters which cause func_1 to simulate the behavior you want to verify.

    GMock is based on compilation fake(macros), therefore you cannot do such a thing. To fake 'C' methods you have to use a different tools such as Typemock Isolator++.

    If you don't want to use Isolator++, then you should refactor your method; Change func to func(int a, ) and then use the pointer instead of func_1.

    My chart in this answer might help to decide the way to handle your case.

提交回复
热议问题