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

mock: in order mock calls #1637

Merged
merged 4 commits into from
Oct 2, 2024
Merged

mock: in order mock calls #1637

merged 4 commits into from
Oct 2, 2024

Conversation

ReyOrtiz
Copy link

@ReyOrtiz ReyOrtiz commented Sep 6, 2024

Summary

Add helper method to declare the order of the mock calls.

Changes

  • add InOrder(calls ...*Call) helper method to declare the order of mock calls by calling NotBefore()

Motivation

This is probably the only feature I miss from gomock.InOrder(), it is more intuitive and cleaner to declare the order of the mock calls. Probably most cases can be satisfied with this method.

InOrder(
	mockedService1.On("TheExampleMethod", 1, 2, 3).Return(4, nil),
	mockedService2.On("TheExampleMethod2", true).Return(),
	mockedService3.On("TheExampleMethod3", "abc", 3).Return(false, nil),
)

Related issues

Closes #1639

Copy link
Collaborator

@brackendawson brackendawson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we get a unit test, please.

@ReyOrtiz
Copy link
Author

unit test added

Copy link
Collaborator

@brackendawson brackendawson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Positive and negative tests, nice one. 👍 Just a couple of small comments.

c,
)

require.Equal(t, []*Call{b, c}, mockedService.ExpectedCalls)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line is testing the implementation. A cost effective test would only test the behaviour, as you are doing just fine after this line.

If you remove this assertion then we are free to change the implementation of the intended behaviour without breaking the test. It also removes the need to declare b and c, so the test can be written as InOrder is intended to be used.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, it's just comparing pointer values. If InOrder did literally nothing then that assertion would still pass.

mock/mock.go Show resolved Hide resolved
(requested changes applied)
@ReyOrtiz
Copy link
Author

Good comments, thanks! Requested changes applied.

Copy link
Collaborator

@brackendawson brackendawson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me, should be helpful to all the developers moving from gomock. I'll give some time for other maintainers to review before I merge.

@jybp
Copy link

jybp commented Sep 26, 2024

Ah you beat me to it, thanks! And missed that existing PR

@ReyOrtiz
Copy link
Author

ReyOrtiz commented Oct 2, 2024

Hello, any ETA on merging this PR?

@brackendawson brackendawson merged commit c4b8421 into stretchr:master Oct 2, 2024
7 checks passed
@brackendawson
Copy link
Collaborator

Thanks @ReyOrtiz. This can be consumed now by gomock expats with: go get github.com/stretchr/testify@master. Once testify v1.10 is released it can be consumed as per the README. I'll have a look at drafting a release soon.

@ReyOrtiz
Copy link
Author

ReyOrtiz commented Oct 2, 2024

Thanks @brackendawson for all the help! 🙏

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.

(syntax sugar) Implement an InOrder func for mocks
4 participants