eliminating mocked file dependencies

408 Views Asked by At

We are mocking our code but we are having difficulties eliminating the dependency of other files the mocked file includes due to the mocked header including the original header which includes several other files. The error we are getting below:

In file included from ../nRF5_SDK_11.0.0_89a8197/components/softdevice/s130/headers/ble_gap.h:48:0,
                from ../nRF5_SDK_11.0.0_89a8197/components/softdevice/s130/headers/ble.h:52,
                from ../infrastructure/microcontroller_abstraction/ble/include/ble_service.h:4,
                from ../infrastructure/system_abstraction/pressure/include/pressure_service.h:15,
                from ./mocks/pressure_service_mock.h:5,
                from ./mocks/pressure_service_mock.c:7:
../nRF5_SDK_11.0.0_89a8197/components/softdevice/s130/headers/ble_gap.h: In function 'sd_ble_gap_address_set':
../nRF5_SDK_11.0.0_89a8197/components/softdevice/s130/headers/nrf_svc.h:66:5: error: unknown register name 'r0' in 'asm'
    __asm(                                              \
    ^

It is due to the following sample scenario: lets take a sample file pressure service mock.c for example the mock.c ---- includes ---> mock.h the thing is, since mock.h is generated, it is including the the pressure_service.h and since we are including the pressure_service.h , it is trying to include the ble_service.h and then up the hierarchy of includes the c files of the original SDK is NOT compiled but the header files have to be included

how would we stop Cmock from including the pressure_service.h?

Please assist i believe this is a generic problem and it is the whole purpose of why one would utilize CMOCK, but we cannot seem to find the solution.

1

There are 1 best solutions below

0
On

Short answer:

AFAIK there is no option for CMOCK to dismiss any of the includes of the mocked C module. It is only possible to specify additional includes.

Depending on your structure of include directories you can try to replace the obstructive header (pressure_service.h in your example) by a strip-down copy of the original header, which reduces any additional dependencies to the minimum.

Long answer:

IMHO you are not facing a generic problem of CMOCK here but a design problem of the module your are trying to mock. It is a tried and tested practice that the header of a C module should only include further header files, which are needed by the public interface of this module. Typical dependencies are type definitions, that are used for arguments and/or return values of interface functions. Since mocking a module means to provide a fake implementations of the specified interface, CMOCK needs to copy all of the original includes over to the mock implementation, in order to make it compile.

This problem can usually be solved reducing the dependencies of original header (the one to be mocked), which usually leads to a better software architecture in general. If the module to be mocked comes from a third party library, this is usually beyond reach, though. In this case, a workaround is making a copy of the original header and stripping it down to the minimum functionality required for the unit test. Of course this implies, that any change to the original interface needs to be manually transfered to strip-down copy later. In case of a stable third party module that should not be a big problem, though.