RSpec Stubbing: return in a sequence

纵然是瞬间 提交于 2019-12-10 17:09:43

问题


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"


回答1:


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



回答2:


Not most elegant, but how about:

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


来源:https://stackoverflow.com/questions/7754733/rspec-stubbing-return-in-a-sequence

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!