Skip to content

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

@jybp

Description

@jybp

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions