我正在使用 minitest 为 resque 工作人员编写测试。
如何为 Resque 终止并向工作人员发送 Resque::TermException 时调用的救援 block 编写测试?
我的方法是 stub 工作程序调用的方法之一,并让它引发 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'
...
最佳答案
Mocha 目前使用此语法进行 stub 和提升:
object.stubs(:expected_method).raises(Exception, 'message')
参见http://www.rubydoc.info/github/floehopper/mocha/Mocha/Expectation:raises
您是否正在使用具有不同语法的旧版 Mocha?
或者您是否有可能不小心使用了 RSpec 语法?
关于ruby - Minitest 测试 Resque::TermException 的救援 block ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28640959/