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

Allow mocking a function that isn't linked into the exe #11

Closed
ugoren opened this issue Apr 17, 2018 · 10 comments
Closed

Allow mocking a function that isn't linked into the exe #11

ugoren opened this issue Apr 17, 2018 · 10 comments

Comments

@ugoren
Copy link
Contributor

ugoren commented Apr 17, 2018

When linking a test executable, we sometimes don't want to link it with all the libraries that are used in production. Mocking is very useful for this - if you mock all functions in a library, you don't need to link with the library itself. But C-mock searches for the real function with dlsym, and fails if it doesn't exist. So you have to link with it.

In my company linkage is a mess, so we have a local change that checks if the function exists only if we actually try to call it. This is very useful for us.
You can see the code in a branch in my clone.

But this changes behavior unconditionally, so I don't think it's appropriate for merge.

The only difficulty is how can we make it conditional. There are may alternatives, such as:

  • Compile time switch (e.g. make ALLOW_MISSING=1).
  • A separate set of macros (e.g. CMOCK_MOCK_FUNCTIONn_ALLOW_MISSING).
  • When using a mock class, a variation of CMockMocker (e.g. CMockMockerAllowMissing).
  • A macro defined by used before defining mocks (e.g. #define CMOCK_ALLOW_MISSING 1).

Can we add this functionality?

Thanks.

@hjagodzinski
Copy link
Owner

Sorry for late reply. I would opt for alternative approach. A lazy loading of a real function. As long as a given function is not mocked no error will be thrown even if it's not available.

@ugoren
Copy link
Contributor Author

ugoren commented May 7, 2018

This is actually what we do in my company.
But can it be the default behavior, or should the user choose between this and eager loading?

@hjagodzinski
Copy link
Owner

A situation in which you call a function which you neither linked with or mocked I see as a bug. I would make a lazy loading the default behaviour.

@ugoren
Copy link
Contributor Author

ugoren commented May 7, 2018 via email

@hjagodzinski
Copy link
Owner

👍

@ugoren
Copy link
Contributor Author

ugoren commented May 13, 2018 via email

@hjagodzinski
Copy link
Owner

Can you create an issue on that so we can address it as a distinct problem?

@ugoren
Copy link
Contributor Author

ugoren commented May 14, 2018 via email

@hjagodzinski
Copy link
Owner

I have in mind C-Mock now fails to compile with latest Google Mock.

@ugoren
Copy link
Contributor Author

ugoren commented May 14, 2018 via email

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