Closed KerbsOD closed 2 months ago
When you call On
, The mock appends a Call
to a list. When your mock calls the interface method, it will iterate through that list looking for a match on the Method name and arguments, but not the return arguments. You have two calls with arguments that match exactly, so its always going to pick the first one you defined in SetupTest
, since it was inserted first in the list.
https://github.com/stretchr/testify/blob/v1.9.0/mock/mock.go#L366-L376
some solutions are discussed in https://github.com/stretchr/testify/issues/558. Creating a new mock per test is pretty common.
Context
I'm mocking a service and i generalize the mocking of a method in the SetupTest() method like this:
For a specific test i want the mocked service to return an actual error like this:
Problem
The test fails because the return is
nil
instead oferrorCardWithNoFunds
. I've debugged it and it seems like mocked returns for a call are stacked in a return array.Question
Is there a way, for a single test, to override the return of a method?
P.S. I'm new to testify and Go. I'm learning by doing some projects. I'm open to suggestions about a more idiomatic way of solving the problem.
My solution: Define
s.mockMerchantProcessor.On("DebitOn", mock.Anything, mock.Anything).Return(nil)
on each test where theDebitOn
method is called