4

I know the following things work:

returning a parameter

subject.should_receive(:get_user_choice){ |choices| choices.to_a[0] }

and a sequence (it will return a 0 on the first call, and the second time "exit")

subject.should_receive(:get_user_choice).and_return(0, "exit")

But how to combine them? what if I would like to return the parameter the first time and then return "exit"

Len
  • 2,093
  • 4
  • 34
  • 51

2 Answers2

5

Alternatively:

subject.should_receive(:get_user_choice).ordered.and_return { |choices| choices.to_a[0] }
subject.should_receive(:get_user_choice).ordered.and_return { "exit" }
Jakob S
  • 19,575
  • 3
  • 40
  • 38
1

Not most elegant, but how about:

n = 0
subject.should_receive(:get_user_choice){|choices|
   (n += 1) < 2 ? choices.to_a[0] : "exit"
}
Laas
  • 5,978
  • 33
  • 52
  • when I tried this in irb, it didn't seem to update the counter... must have done something terribly wrong (probably spelling.. sigh ;-)) Thanks, this is working for me now! – Len Oct 13 '11 at 13:46