开发者

RSpec Stubbing: return in a sequence

开发者 https://www.devze.com 2023-04-12 23:23 出处:网络
I know the following things work: returning a parameter subject.should_receive(:get_user_choice){ |choices| choices.to_a[0] }

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 ret开发者_如何学Curn 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"


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


Not most elegant, but how about:

n = 0
subject.should_receive(:get_user_choice){|choices|
   (n += 1) < 2 ? choices.to_a[0] : "exit"
}
0

精彩评论

暂无评论...
验证码 换一张
取 消