-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
assert mock calls in order #741
Comments
There are also cases where order does not, and should not matter. I have test cases which will break if the order is preserved in a future release without the test code having to change in some way to say that order is important. |
I was about to open an issue about this... Currently, the fact that this feature is missing is the only thing that stops me from moving from GoMock (this library is better in basically everything else). I wouldn't want the order to be strict all the time - it's unnecessary. But there are times when the order really matters, and I want to be sure that the code really executes certain methods right after other ones. Do you plan on adding this feature (again, just to be clear: as an option, not the default)? Thanks for the consideration :) cc @boyan-soubachov |
This adds a function to assert mock calls in order, resolving stretchr#741. I've added all the test cases I can think of and tried to design the edge cases around repeatability and optionality to make sense with the rest of the package. My one concern is about the v2 work - I hope this can fit into v1 with minimal pain on the maintainers side. > AssertExpectationsInOrder asserts that everything specified with On and Return was in fact called as expected in the order expected. Expectations set up for a specific number of times must be called that number of times before the next call to the mock is made. If optional, they do not need to be called the full number of times. Expectation with no specific limit must be called at least once unless optional.
I had a thought about how to gracefully implement this as a backward compatible, up-front expectation. Give Call a new method: func (c *Call) NotBefore(calls ...*Call) *Call A call to this mock call will be treated as unexpected if all This would let you do: call1 := mockThing.On("Init").Return(nil)
call2 := mockThing.On("Do").Return(nil).NotBefore(call1)
mockThing.On("Close").Return(nil).NotBefore(call1, call2) An advantage to this approach is you don't have to rigidly define the order of all calls and make tests which aren't cost-effective. Instead you're just building the minimum required dependency tree of calls which must come before others, if functions live outside this dependency then that's fine: mockThing.On("Do").Return(nil).Once().NotBefore(
mockThing.On("Init").Return(nil).Once()
)
mockThing.On("HasAttr").Return(true).Maybe() // This can still be called or not whenever you want I'm happy to open a PR for this, it should be pretty trivial to implement. What do others think? |
Since #1106 has been approved, those of us hoping for this feature would love to see it merged 🙂 |
Done :) |
This was added around github.com/stretchr/testify v1.8.0 and did not work for me in v1.7.0 IIRC. I've modified the go.mod from v1.7.0 to v1.8.0:
And did call
The |
No description provided.
The text was updated successfully, but these errors were encountered: