ThrowTheSwitch / Ceedling

Ruby-based unit testing and build system for C projects
http://throwtheswitch.org
Other
588 stars 247 forks source link

Mocking Nested Functions #631

Closed rushmakes closed 1 day ago

rushmakes commented 3 years ago

Sorry I couldn't find the solution to this problem. So I am writing a unit test for a sensor API. The API has a sensor.c, sensor.h and sensor_types.h file. sensor.c has various functions needed to access the sensor, these functions are declared in the sensor.h and sensor_types.h has some constants. Also, there are HAL files for i2c communication. Now, the real problem here is, consider the function given below, sensor_init, funcA and funcB all are in same file sensor.c. eg: int8_t sensor_init(some struct){ funcA{} i2c_read() funcb{} i2c_write() }

I was able to write unit tests and test the funcA and funcB as they don't depend on anything else. Also, for the i2c_read and write I was able to write unit tests. My problem is writing unit test for sensor_init, I cannot directly mock funcA and funcB as they belong to the same file. I tried creating a dummy header file containing the declaration of dummy_funcA and dummy_funcB, and mocking this header into the test file. I changed the name to avoid conflict. Is this a right approach.? Also I don't understand how to handle this nested function situation? Any clue would be very helpful

JuPrgn commented 3 years ago

This can be usefull https://github.com/ThrowTheSwitch/Ceedling/issues/604 https://github.com/ThrowTheSwitch/CMock/issues/365

mkarlesky commented 1 day ago

Closing this issue to cull open issues. Issue #936 collects issues related to this one.