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

[BUG] An error occurred when the constructor of a global instance called the mock method #25

Open
cagdasyesildag opened this issue Dec 19, 2022 · 1 comment

Comments

@cagdasyesildag
Copy link

cagdasyesildag commented Dec 19, 2022

Hello,

I am testing C-Mock extension. Everything seems good. I can mock C type functions.

But, one of this mocked functions is called from a class's constructor. When I define a global instance, I get error from cmock-function-mockers.h, line=100. (error msg= Neither mock nor real function is present)

I made a change as below. It fixed my problem. Is there any better solution that you recommend?

image

@hjagodzinski
Copy link
Owner

Hi,

the exception is thrown because the constructor of a global class is called before the internal state of C-Mock is initialized. C-Mock was designed with the assumption a mocked function is called from the test code only. In your case, the code in question is called before a test suit even runs.

Your proposition allows a mocked function to be called outside a test. Since I don't know your case well enough I can only offer only general alternative suggestions.

  1. A client might not expect side effects during an object instantiation hence it's reasonable to avoid them in constructors.
  2. A global object having a state might lead to dependency between tests. You're better off having dedicated object instances per test.
  3. Consider using CMockMocker instead of deprecated macros.

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

2 participants