Rspec: testing a rescue

一个人想着一个人 提交于 2021-02-07 20:17:44

问题


Trying to test that my function will properly rescue from an exception, change the parameters (filename), and try again one time. I can get the function to receive the first try, but can't get it to receive the 2nd attempt.

Controller

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

Spec

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

Error:

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

Tried a few things but nothing works. Any thoughts?

Thanks in advance!


回答1:


You should use and_raise not and_return.

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


来源:https://stackoverflow.com/questions/6525819/rspec-testing-a-rescue

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