ruby-on-rails - Rspec:测试救援

标签 ruby-on-rails rspec

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

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

规范

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

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

提前致谢!

最佳答案

您应该使用 and_raise 而不是 and_return

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

关于ruby-on-rails - Rspec:测试救援,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6525819/

相关文章:

ruby-on-rails - 将 Ruby 控制台输出写入文本文件

ruby-on-rails - Rspec/Rails3 : NoMethodError: undefined method `text' for nil:NilClass

ruby-on-rails - capybara Chrome headless (headless)确认对话框

ruby-on-rails - Rspec,FactoryGirl : FactoryGirl. 在之前创建(:all) isn't persisting records?

ruby-on-rails - Rails 的 Activerecord 表如何引用自身?

ruby-on-rails - 我们的rails应用程序如何找到routes.rb?

ruby-on-rails - 如何使用 rspec 测试 Cloudinary rails 和 Attachinary?

ruby - 如何在之前访问标签过滤器(:suite)/before(:all) hook in RSpec?

ruby-on-rails - RSpec 和 capybara : how to determine if a select control has a specific option selected?

rspec - Selenium::WebDriver::Error::JavascriptError:WAITING评估.js 加载失败 Firefox 23