The technique I would use is as follows:
allow(client_mock).to_receive(:get_opportunity) do |arg|
case arg
when “1234567890”
opportunity1
when “0987654321”
opportunity2
end
end
You can see this documented here:
https://relishapp.com/rspec/rspec-mocks/v/2-99/docs/method-stubs
<https://relishapp.com/rspec/rspec-mocks/v/2-99/docs/method-stubs>
Cheers!
> On Feb 11, 2021, at 7:27 AM, Aaric Pittman <[email protected]> wrote:
>
> I've got an instance double of a class and I want to stub two calls to the
> same method and return different values based on different arguments. This is
> an example of what I have.
>
> allow(client_mock).to
> receive(:get_opportunity).with("1234567890").and_return(opportunity1)
> allow(client_mock).to
> receive(:get_opportunity).with("0987654321").and_return(opportunity2)
>
> I am getting the following error though
>
> #<InstanceDouble(Client) (anonymous)> received :get_opportunity with
> unexpected arguments
> expected: ("1234567890")
> got: ("0987654321")
> Please stub a default value first if message might be received with other
> args as well.
>
> I feel like I've done this before without problems. Could someone tell me
> what I'm doing wrong?
>
> --
> You received this message because you are subscribed to the Google Groups
> "rspec" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected]
> <mailto:[email protected]>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/rspec/74a89ab9-4d86-4ddf-913d-42b928ecbac9n%40googlegroups.com
>
> <https://groups.google.com/d/msgid/rspec/74a89ab9-4d86-4ddf-913d-42b928ecbac9n%40googlegroups.com?utm_medium=email&utm_source=footer>.
--
You received this message because you are subscribed to the Google Groups
"rspec" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/rspec/D699B720-0D35-49A2-B541-DBF2E23CF5E4%40pobox.com.