Rspec:测试救援

时间:2011-06-29 19:00:52

标签: ruby-on-rails 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

尝试了一些但没有任何作用。有什么想法吗?

提前致谢!

1 个答案:

答案 0 :(得分:5)

您应该使用and_raise而不是and_return

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