You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When mocking a static method with Mockall, it's necessary to create a context object 1. Normally, the context object should live for the entire duration of the test case. But it's easy for users to forget, in which case they'll get confusing errors at runtime. The easiest way to forget is when users use method chaining inappropriately.
Example code
The lint should flag code like this:
MockA::foo_context().expect().returning(|| 99);
And suggest instead writing it like this:
let ctx = MockA::foo_context();
ctx.expect().returning(|| 99);
Notes
Possibly the easiest way to implement this would be to detect when a user uses a *_context() method without a let binding. And since the *_context() methods are autogenerated, Mockall can emit any attributes needed to guide the linter.
Lint explanation
When mocking a static method with Mockall, it's necessary to create a context object 1. Normally, the context object should live for the entire duration of the test case. But it's easy for users to forget, in which case they'll get confusing errors at runtime. The easiest way to forget is when users use method chaining inappropriately.
Example code
The lint should flag code like this:
And suggest instead writing it like this:
Notes
Possibly the easiest way to implement this would be to detect when a user uses a *_context() method without a
let
binding. And since the *_context() methods are autogenerated, Mockall can emit any attributes needed to guide the linter.Footnotes
https://docs.rs/mockall/latest/mockall/#static-methods ↩
The text was updated successfully, but these errors were encountered: