Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mock code for external modules #200

Open
1 of 4 tasks
PatrickKa opened this issue Dec 16, 2023 · 0 comments
Open
1 of 4 tasks

Mock code for external modules #200

PatrickKa opened this issue Dec 16, 2023 · 0 comments

Comments

@PatrickKa
Copy link
Contributor

PatrickKa commented Dec 16, 2023

Description

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.

To do

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant