W0068 - meck_restricted
Severity: Error
Problem
This diagnostic reports an error when mocking certain restricted modules using meck:new/2 or meck:expect/3,4 in tests.
Example
-module(my_SUITE).
init_per_testcase(_TestCase, Config) ->
meck:new(restricted_mod, [passthrough]),
%% ^^^^^^^^^^^ 💡 error: W0068: Mocking 'restricted_mod' is almost always the wrong approach
meck:expect(restricted_mod, some_function, 1, ok),
%% ^^^^^^^^^^^ 💡 error: W0068: Mocking 'restricted_mod' is almost always the wrong approach
% ...
Explanation
Certain core infrastructure modules should not be mocked in tests because mocking them can hide real bugs that would only surface in production. In addition, it makes it harder to make foundational changes to these modules.
How to Fix
Instead of mocking restricted modules, use the real module APIs or dedicated test utilities:
- Use the module's own test helpers or generators to set up the state you need
- Consider refactoring the code under test to accept the relevant values as parameters
Adding More Restricted Modules
To add more modules to this linter, edit the RESTRICTED_MODULES constant in meck_restricted.rs.