-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Feat: added OnFunc to mock using method directly and MockCall to mock quick methods #1678
base: master
Are you sure you want to change the base?
Conversation
It looks like you can provide a reference to any method or function in the first argument, should this be constrained to methods on the struct embedding the mock.Mock on which you are calling OnFunc? Could lead to very confusing misuse. |
@brackendawson Thank you for raising this important concern. The latest test results demonstrate exactly why this proposal adds significant value to the library: Key Evidence: // Current behavior - no validation
mockedService.On("GhostMethod", ...) // ✅ Passes silently (even with non-existent methods)
// Proposed behavior - type-safe
mockedService.OnFunc(mockedService.GhostMethod, ...) // ❌ Fails at compile time Critical improvements:
Why this matters:
Next steps I can take:
This solves a real, measurable problem in the current implementation while introducing robust type safety. I believe it directly addresses your original concern about misuse by making invalid states impossible to represent. Would you agree this represents a meaningful improvement to the library's reliability? Empirical evidenceCurrent behavior (
|
Validation aspect | On() (Current and working) |
OnFunc() (Proposed) |
Reliability impact |
---|---|---|---|
Method Existence | ❌ No validation | ✅ Compile-time check | Prevents ghost method mocking |
Signature Safety | ❌ Runtime only | ✅ Compile-time verify | Ensures type correctness |
Refactoring Support | ❌ Silent test passing | ✅ Breaks build | Forces test updates |
Error Detection | ❌ Production runtime | ✅ Development phase | Shifts left safety checks |
Argument Validation | ❌ Late failure | ✅ Immediate feedback | Prevents argument mismatches |
Key advantages demonstrated:
- Complete Safety: 100% prevention of invalid mocks
- Developer Experience: Fail-fast during development
- Maintainability: Automatic detection of broken interfaces
- Consistency: Uniform behavior for all test cases
- Zero False Positives: Impossible to create valid-seeming invalid mocks
Summary
This PR introduces enhancements to the mocking library, allowing for more robust and refactor-friendly mocking of methods.
Changes
Examples
Previously, mocking a method required writing the method name as a string, which could lead to issues during renaming or refactoring. Additionally, mocking methods with multiple return values involved writing extensive conditional logic. The new functionalities simplify these processes significantly.
First we need to setup a mock
By using OnFunc, you can directly reference the method, making the code more robust against refactoring.
Now, we need to mock the call method inside the module as well
Using MockCall, you can mock methods with multiple return values in a cleaner and more concise manner, reducing the amount of boilerplate code.
These enhancements make the mocking library easier to use and maintain, providing a more intuitive and error-resistant approach to mocking methods.
Motivation
After three weeks using golang I believe theese changes were necessary to improve the usability and maintainability of the mocking library. By allowing direct method references, we reduce the risk of errors during renaming or refactoring. The MockCall function simplifies the mocking process, making the code cleaner and easier to maintain.
Observations and comments with the aim of improving the proposal will be well received because my only intention is to contribute to the evolution of the project.
Related issues
N/A