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

assert mock calls in order #741

Closed
ran-berenfeld opened this issue Mar 21, 2019 · 6 comments · Fixed by #1106 · May be fixed by #1082
Closed

assert mock calls in order #741

ran-berenfeld opened this issue Mar 21, 2019 · 6 comments · Fixed by #1106 · May be fixed by #1082

Comments

@ran-berenfeld
Copy link

No description provided.

@ecbaldwin
Copy link

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.

@grongor
Copy link

grongor commented Feb 9, 2021

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)?
I would even be willing to try to implement it (although I never contributed to testify before), I just don't want my time to go to waste :D

Thanks for the consideration :) cc @boyan-soubachov

apexskier added a commit to apexskier/testify that referenced this issue May 30, 2021
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.
@brackendawson
Copy link
Collaborator

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 calls are not fully satisfied. (After() is already defined).

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?

@johncmerfeld
Copy link

Since #1106 has been approved, those of us hoping for this feature would love to see it merged 🙂

@boyan-soubachov
Copy link
Collaborator

Since #1106 has been approved, those of us hoping for this feature would love to see it merged 🙂

Done :)

@qknight
Copy link

qknight commented Apr 25, 2023

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:

require (
	github.com/golang/mock v1.6.0
	github.com/google/uuid v1.3.0
	github.com/gorilla/mux v1.8.0
	github.com/jellydator/ttlcache/v2 v2.11.1
	github.com/kelseyhightower/envconfig v1.4.0
	github.com/pquerna/cachecontrol v0.1.0
	github.com/sirupsen/logrus v1.9.0
	github.com/stretchr/testify v1.7.0
        ...

And did call

go mod tidy

The NotBefore works as advertised -> I'm happy! Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
7 participants