mock icon indicating copy to clipboard operation
mock copied to clipboard

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

Open alexykot opened this issue 2 years ago • 4 comments

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

alexykot avatar Jun 13 '22 22:06 alexykot

Thanks for the issue, I will take a look!

codyoss avatar Jul 08 '22 17:07 codyoss

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)
}

elchead avatar Nov 09 '22 13:11 elchead

Any updates on the issue?

KabudoWiseMan avatar Apr 03 '23 14:04 KabudoWiseMan

FYI Same problem were

nicolasassi avatar Jun 07 '23 12:06 nicolasassi