2011-06-29 104 views
3

試圖測試我的功能將正確地從異常中解救出來,更改參數(文件名),然後再試一次。我可以獲得第一次嘗試的功能,但無法獲得第二次嘗試。Rspec:測試救援

控制器

begin 
    @video = get_video(video_id) 
rescue 
    matches = video_id.match(/(.*)[-](\d+)+x(\d+)_(\d+)/) 
    swapped_dash = (matches && matches.size == 5) ? "#{matches[1]}_#{matches[2]}x#{matches[3]}_#{matches[4]}" : nil 
    @video = swapped_dash.nil? ? false : get_video(swapped_dash) rescue false 
end 

規格

it "attempts to find a video with an underscore if the original filename is not found" do 
    controller.stub(:get_video).and_return(Exception) 
    controller.should_receive(:get_video).with("MyString-480x272_8").once.ordered 
    controller.should_receive(:get_video).with("MyString_480x272_8").once.ordered 
    get 'player_only', :video_id => "MyString-480x272_8" 
end 

錯誤:

(#<WatchController:0x00000105d03c60>).get_video("MyString_480x272_8") 
    expected: 1 time 
    received: 0 times 

嘗試了一些東西,但沒有任何工程。有什麼想法嗎?

在此先感謝!

回答

5

您應該使用and_raise而不是and_return

controller.stub(:get_video).and_raise(Exception)