Skip to content
This repository has been archived by the owner on Jun 27, 2023. It is now read-only.

Order of calls is enforced when InOrder() or After() were not invoked #653

Open
alexykot opened this issue Jun 13, 2022 · 4 comments
Open

Comments

@alexykot
Copy link

Actual behaviour
When I have EXPECT set for two calls to the same method with different parameters - the order of EXPECT definitions enforces the order of the actual calls.

I.e. when having these expectations defined:

    test.mock.EXPECT().DoSomething("foo").Return(&Response{})
    test.mock.EXPECT().DoSomething("bar").Return(&Response{})

and code under test defined as

    service.DoSomething("foo")
    service.DoSomething("bar")

Just changing the order of definitions of calls expected with no other changes, i.e. making it

test.mock.EXPECT().DoSomething("bar").Return(&Response{})
test.mock.EXPECT().DoSomething("foo").Return(&Response{})

makes the test fail.

Expected behavior
As per documentation:

By default, expected calls are not enforced to run in any particular order.

So no order should be enforced as long as the actual calls match any of the defined expectations.

Additional Information

  • gomock mode: source
  • gomock version: 1.6.0
  • golang version: 1.17.2
@codyoss
Copy link
Member

codyoss commented Jul 8, 2022

Thanks for the issue, I will take a look!

@elchead
Copy link

elchead commented Nov 9, 2022

Bug still persists, but I used a workaround with a custom matcher

test.mock.EXPECT().DoSomething(AnyOfStrings([]string{"foo","bar})).Return(&Response{})


type AnyOfStrings ([]string)
func (m *AnyOfStrings) Matches(x interface{}) bool {
	for _, v := range *m {
		if v == x.(string) {
			return true
		}
	}
	return false
}

func (m *AnyOfStrings) String() string {
	return fmt.Sprintf("is one of %v", *m)
}

@KabudoWiseMan
Copy link

Any updates on the issue?

@nicolasassi
Copy link

FYI Same problem were

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants