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

Is it possible to mock static functions? #106

Open
KasperZK opened this issue Nov 22, 2021 · 2 comments
Open

Is it possible to mock static functions? #106

KasperZK opened this issue Nov 22, 2021 · 2 comments

Comments

@KasperZK
Copy link

I presented the framework to my team and it raised some questions:

Can you mock static functions with this framework? For example would it be possible to mock and test a static function that is both defined and declared in the .c file that is to be tested? Do we even want to do this ever or should private functions only be accessed through public functions?

Thanks

@nradulovic
Copy link

nradulovic commented Nov 22, 2021

Hi, you can only mock functions which are defined outside of tested implementation unit (.c).

In my projects I treat static functions as part of tested function so I don't need to mock them. When I get into the situation that I need to mock a static function, then that is a sign to me that I need to re-think about architecture of the code and ask some questions to myself.

@codeofandrin
Copy link

I usually use static functions, if it's only used "internally" i.e. in the .c file itself and doesn't have to be included somewhere else. In that case I don't think it's a code architecture problem, but how would you mock that static function.

For example, I have a function called I2cTransmit which is "public" and can be called externally. This function calls the static function I2cStartCondition which returns a value. In this example, how would I mock I2cStartCondition to "fake" the return value to then test the behaviour of I2cTransmit?

In my projects I treat static functions as part of tested function so I don't need to mock them.

That makes sense, especially if you want to test what the static function itself does, but you can't test what the behaviour of the function is that calls the static function, or am I missing something?

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

No branches or pull requests

3 participants