Rspec:测试救援 [英] Rspec: testing a rescue

查看:37
本文介绍了Rspec:测试救援的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试测试我的函数是否能正确地从异常中拯救出来,更改参数(文件名),然后再试一次.我可以让函数接收第一次尝试,但无法接收第二次尝试.

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.

控制器

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

尝试了一些东西,但没有任何效果.有什么想法吗?

Tried a few things but nothing works. Any thoughts?

提前致谢!

推荐答案

你应该使用 and_raise 而不是 and_return.

You should use and_raise not and_return.

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

这篇关于Rspec:测试救援的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆