2011-10-13 22 views
4

我知道以下幾點工作:RSpec的成株:返回序列中

返回參數

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

和序列(它會在第一次調用返回0,第二次「退出「)

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

但如何將它們結合? 如果我想返回該參數的第一時間,然後返回「退出」

回答

5

或者:

subject.should_receive(:get_user_choice).ordered.and_return { |choices| choices.to_a[0] } 
subject.should_receive(:get_user_choice).ordered.and_return { "exit" } 
+0

謝謝,我不知道這會工作 – SirLenz0rlot

1

不是最優雅的,但如何:

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

當我嘗試這樣做在irb中,它似乎沒有更新計數器......一定是做了一些非常錯誤的事情(可能是拼寫..嘆氣;-)) 謝謝,這對我來說現在就工作了! – SirLenz0rlot