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

(syntax sugar) Implement an InOrder func for mocks #1639

Open
jybp opened this issue Sep 25, 2024 · 0 comments · May be fixed by #1637
Open

(syntax sugar) Implement an InOrder func for mocks #1639

jybp opened this issue Sep 25, 2024 · 0 comments · May be fixed by #1637

Comments

@jybp
Copy link

jybp commented Sep 25, 2024

Description

Since gomock has been archived, I am switching to testify's mock pkg.

One extremely neat feature that gomock had is the InOrder func:
https://pkg.go.dev/github.com/golang/mock/gomock#InOrder

Which allowed to very easily enforce expectation order by going from:

someMock.EXPECT().ValidateArgs(gomock.Any(), 1).Return(nil),
someMock.EXPECT().ValidateArgs(gomock.Any(), 2).Return(nil)

to

gomock.InOrder(
    someMock.EXPECT().ValidateArgs(gomock.Any(), 1).Return(nil),
    someMock.EXPECT().ValidateArgs(gomock.Any(), 2).Return(nil)
)

As opposed to the current API implemented in testify which is more cumbersome and less intuitive with the need to declare vars and pass them inside the last chained method.
#741
#1106

call1 := mockThing.On("Init").Return(nil)
call2 := mockThing.On("Do").Return(nil).NotBefore(call1)
mockThing.On("Close").Return(nil).NotBefore(call1, call2)

Or I guess do some kind of nesting which reverse the chronological order of events and creates nesting.

mockThing.On("Close").Return(nil).NotBefore(
    mockThing.On("Do").Return(nil).NotBefore(
        mockThing.On("Init").Return(nil).NotBefore(
            ... nesting
)

Ideally could be turned into:

mock.InOrder(
    mockThing.On("Init").Return(nil),
    mockThing.On("Do").Return(nil),
    mockThing.On("Close").Return(nil)
)

Proposed solution

I didn't looked into the codebase yet to know if that's easily feasible.
But my gut feeling says it could be trivial to chain the NotBefore inside the mock.InOrder variadic function.
This issue is mostly to gather feedback.

Use case

More intuitive API.

@jybp jybp changed the title (syntax sugar) Implement an InOrder func (syntax sugar) Implement an InOrder func for mocks Sep 25, 2024
@brackendawson brackendawson linked a pull request Sep 25, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant