This PR introduces enhancements to the mocking library, allowing for more robust and refactor-friendly mocking of methods.
Changes
Added OnFunc method to allow referencing the mocked method directly instead of using it string names.
Implemented MockCall helper function to simplify mocking method calls with multiple return values.
Refactored some functionalities to enable code reuse and avoid repetition.
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.
By using OnFunc, you can directly reference the method, making the code more robust against refactoring.
Using the mock
Now, we need to mock the call method inside the module as well
Current way
func (i *TestExampleImplementation) TheExampleMethod(et *ExampleType) (string, int, error) {
args := i.Called(et)
var r0 string
if args.Get(0) != nil {
r0 = args.String(0)
}
var r1 int
if args.Get(1) != nil {
r1 = args.Int(1)
}
var r2 error
if args.Get(2) != nil {
r2 = args.Error(2)
}
return r0, r1, r2
}
> New way with `MockCall`
```go
func (i *TestExampleImplementation) TheExampleMethod(et *ExampleType) (string, int, error) {
var r0 string
var r1 int
var r2 error
i.MockCall(CallSetup{Arguments: Arguments{et}, Returns: Returns{&r0, &r1, &r2}})
return r0, r1, r2
}
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.
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.
Setting a mock First we need to setup a mock
Using the mock Now, we need to mock the call method inside the module as well
return r0, r1, r2 }
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