【问题标题】:minitest test rescue block for Resque::TermExceptionResque::TermException 的最小测试救援块
【发布时间】:2015-02-21 01:23:25
【问题描述】:

我正在使用 minitest 为 resque worker 编写测试。

如果 Resque 被终止并向工作人员发送 Resque::TermException,我该如何为救援块编写测试?

我的方法是存根工作人员调用的方法之一,并让它引发 Resque::TermException。

有时我可以引发异常,但它发生在错误的时间并终止了测试运行。现在我尝试了这样的事情: (我正在使用固定装置和摩卡咖啡)

test "if interrupted it removes the file" do
  CSV.stub(:open).and_raise(Resque::TermException.new(15))

  report = Report.find_by_id(1)
  Report.expects(:find_by_id).returns(report)

  ReportJob.perform({ 'test' => 'test' })
  refute File.exist? ReportJob.report_file(report)
end

但运行它没有运气:

1) Error:
ReportTest#test_if_interrupted_it_removes_file:
ArgumentError: wrong number of arguments (1 for 2)
test/resque-tasks/spreadsheet_report_test.rb:2:in `block in <class:ReportTest>'
/Users/me/.rvm/rubies/ruby-1.9.3-p448/lib/ruby/1.9.1/test/unit/testcase.rb:17:in `run'
/Users/me/.rvm/gems/ruby-1.9.3-p448/gems/activesupport-3.1.10/lib/active_support/testing/setup_and_teardown.rb:36:in `block in run'
...

【问题讨论】:

  • 你有什么问题?
  • 他的问题是“我如何为救援块编写测试,以防 Resque 被终止并向工作人员发送 Resque::TermException?”

标签: ruby exception resque minitest rescue


【解决方案1】:

Mocha 目前将此语法用于存根和引发:

object.stubs(:expected_method).raises(Exception, 'message')

http://www.rubydoc.info/github/floehopper/mocha/Mocha/Expectation:raises

您是否在使用旧版本的 Mocha,但语法不同?

或者您是否可能不小心使用了 RSpec 语法?

【讨论】:

  • 谢谢 - 是的,我的代码中有 2 个错误:stub 而不是 stubsand_raises 而不是 raises,现在它可以很好地工作并引发 Resque::TermException测试而不终止测试:)
猜你喜欢
  • 1970-01-01
  • 2021-07-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-26
  • 2023-01-13
相关资源
最近更新 更多