Chapter 4. API Guides
• Installed ESP-IDF with all its requirements
• ruby
• On the Linux target, which is the only target where mocking currently works, libbsd is required, too
Mock a Component
To create a mock version of a component, called a component mock, the component needs to be overwritten in
a particular way. Overriding a component entails creating a component with the exact same name as the original
component, then let the build system discover it later than the original component (see Multiple components with the
same name <cmake-components-same-name> for more details).
In the component mock, the following parts are specified:
• The headers providing the functions to generate mocks for
• Include paths of the aforementioned headers
• Dependencies of the mock component (this is necessary e.g. if the headers include files from other components)
All these parts have to be specified using the IDF build system function idf_component_mock. You can use the
IDF build system function idf_component_get_property with the tag COMPONENT_OVERRIDEN_DIR
to access the component directory of the original component and then register the mock component parts using
idf_component_mock:
idf_component_get_property(original_component_dir <original-component-name>␣
,→COMPONENT_OVERRIDEN_DIR)
...
idf_component_mock(INCLUDE_DIRS "${original_component_dir}/include"
REQUIRES freertos
MOCK_HEADER_FILES ${original_component_dir}/include/header_containing_
,→functions_to_mock.h)
The component mock also requires a separate mock directory containing a mock_config.yaml file that config-
ures CMock. A simple mock_config.yaml could look like this:
:cmock:
:plugins:
- expect
- expect_any_args
For more details about the CMock configuration yaml file, have a look at cmock/CMock/docs/CMock_Summary.md.
Note that the component mock does not have to mock the original component in its entirety. As long as the test
project’s dependencies and dependencies of other code to the original components are satisfied by the component
mock, partial mocking is adequate. In fact, most of the component mocks in IDF in tools/mocks are only partially
mocking the original component.
Examples of component mocks can be found under tools/mocks in the IDF directory. General information on how
to override an IDF component can be found under the section “Multiple components with the same name”in the
IDF build system documentation`.
Adjustments in Unit Test
The unit test needs to inform the cmake build system to mock dependent components (i.e., it needs to override the
original component with the mock component). This is done by either placing the component mock into the project’
s components directory or adding the mock component’s directory using the following line in the project’s root
CMakeLists.txt:
list(APPEND EXTRA_COMPONENT_DIRS "<mock_component_dir>")
Both methods will override existing components in ESP-IDF with the component mock. The latter is particularly
convenient if you use component mocks that are already supplied by IDF.
Espressif Systems 1509
Submit Document Feedback
Release v4.4