You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Mocking the code for a module should work as follows:
The code for the mock should be in <Module>Mock.[chi]pp.
Whether to use the mock or the real code is decided with CMake by adding <Module>.cpp or <Module>Mock.cpp to the target sources.
Similar to the non-virtual interface (NVI) design pattern, the public functions that need to be mocked and are declared in <Module>.hpp, forward their calls to functions of the same name but with prefix Do, _, Mock or something like that. I'll call them "do functions" from now on.
These do functions are actually function pointers. For each one, a set function must be declared in <Module>Mock.hpp and defined in <Module>Mock.cpp. As the name suggests, the set functions are used to chose where the function pointers point to. This means that you can choose and change the implementation of the mocked function at runtime.
All function pointers must be initialized with a default function that does "nothing". This way, tests using <Module>Mock.cpp instead of <Module>.cpp will at least compile without any further changes.
Description
Mocking the code for a module should work as follows:
<Module>Mock.[chi]pp
.<Module>.cpp
or<Module>Mock.cpp
to the target sources.<Module>.hpp
, forward their calls to functions of the same name but with prefixDo
,_
,Mock
or something like that. I'll call them "do functions" from now on.<Module>Mock.hpp
and defined in<Module>Mock.cpp
. As the name suggests, the set functions are used to chose where the function pointers point to. This means that you can choose and change the implementation of the mocked function at runtime.<Module>Mock.cpp
instead of<Module>.cpp
will at least compile without any further changes.To do
The text was updated successfully, but these errors were encountered: