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

Feat: added OnFunc to mock using method directly and MockCall to mock quick methods #1678

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

harvic3
Copy link

@harvic3 harvic3 commented Nov 12, 2024

Summary

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.

  • Setting a mock
    First we need to setup a mock

Current way (Still available)

mockedService.On("TheExampleMethod", mock.AnythingOfType("*ExampleType")).Return(nil)

New available way

mockedService.OnFunc(mockedService.TheExampleMethod, mock.AnythingOfType("*ExampleType")).Return(nil)

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

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.

Related issues

N/A

@harvic3 harvic3 changed the title Feat: added OnFunc to mock using method direclty and MockCall to mock quick methods Feat: added OnFunc to mock using method directly and MockCall to mock quick methods Nov 13, 2024
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

Successfully merging this pull request may close these issues.

1 participant