Your call to and_return(500, 100) is being set on each new
OtherFooClassinstance. It is
*not* a stub shared by all instances. So in your example, each new instance
of OtherFooClass will return 500 the first time you call other_foo and then
100 on all subsequent calls.

My 2cents: do not use allow_any_instance_of. Instead setup explicit doubles
for each new instance. Something like:

foo1 = double(OtherFooClass, other_foo: 500)
foo2 = double(OtherFooClass, other_foo: 100)
allow(OtherFooClass).to receive(:new).and_return(foo1, foo2)

*IF* you really must use any_instance you'll need to keep track of the
return values. By manipulating the block form of and_return along with a
closure you can achieve this with:

return_values = [500, 100]
allow_any_instance_of(OtherFooClass).to receive(:other_foo).and_return{
  return_values.shift
}

For completeness, receive could also be passed the block to achieve the
same result.

Aaron


On Sun, Feb 16, 2014 at 1:13 PM, Panayotis Matsinopoulos <
[email protected]> wrote:

> Hi,
>
> What am I doing wrong here?
>
> I am trying to stub a method on any instance of an object and return
> different results and this fails.
>
> Here is the project that demonstrates this particular bug(?).
>
>
> https://github.com/pmatsinopoulos/bug_rspec_2_14_7_stub_return_diff_results_on_multiple_calls
>
> (clone and run "bundle exec rspec"...you will see the failure)
>
> Let me know if I am doing something wrong
>
> Thanks in advance
>
> Panayotis
>
>
>
>
>  --
> 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 post to this group, send email to [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/rspec/71a081d4-c6a0-4227-bd3b-38d6697e4369%40googlegroups.com
> .
> For more options, visit https://groups.google.com/groups/opt_out.
>

-- 
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 post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/rspec/CAKCESdgEtZDOoBgPv4HnT3Do_rusZ207JQoAMc02CLV3%3Dqmm3Q%40mail.gmail.com.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to