作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
升级到 Rails 6.1 后,测试失败:
class MyJob < ActiveJob::Base
class MyError < StandardError; end
def perform
raise MyError
end
end
describe MyJob, type: :job do
it "throws an error" do
expect do
perform_enqueued_jobs { MyJob.perform_later }
end.to raise_error(described_class::MyError)
end
end
此规范因错误而失败
expected MyJob::MyError, got #<Minitest::UnexpectedError: Unexpected exception> with backtrace:
当仔细观察抛出的错误时,它看起来像这样:
69:
70: describe MyJob, type: :job do
71: it "throws an error" do
72: expect do
73: binding.pry
=> 74: perform_enqueued_jobs { MyJob.perform_later }
75: end.to raise_error(described_class::MyError)
76: end
77: end
[1] pry(#<RSpec::ExampleGroups::MyJob>)> perform_enqueued_jobs { MyJob.perform_later }
Minitest::UnexpectedError: MyJob::MyError: MyJob::MyError
/usr/src/app/spec/lib/my_job_spec.rb:5:in `perform'
/usr/local/bundle/gems/activejob-6.1.6/lib/active_job/execution.rb:48:in `block in perform_now'
/usr/local/bundle/gems/activesupport-6.1.6/lib/active_support/callbacks.rb:117:in `block in run_callbacks'
/usr/local/bundle/gems/airbrake-13.0.0/lib/airbrake/rails/active_job.rb:22:in `block in perform'
/usr/local/bundle/gems/airbrake-ruby-6.1.0/lib/airbrake-ruby/benchmark.rb:13:in `measure'
/usr/local/bundle/gems/airbrake-13.0.0/lib/airbrake/rails/active_job.rb:21:in `perform'
/usr/local/bundle/gems/airbrake-13.0.0/lib/airbrake/rails/active_job.rb:45:in `block (2 levels) in <module:ActiveJob>'
/usr/local/bundle/gems/activesupport-6.1.6/lib/active_support/callbacks.rb:126:in `instance_exec'
/usr/local/bundle/gems/activesupport-6.1.6/lib/active_support/callbacks.rb:126:in `block in run_callbacks'
似乎错误以某种方式被 Minitest::UnexpectedError
包裹,但原始的底层错误仍然存在。
知道如何解决这个问题吗?
最佳答案
These在 Rails 6.1 中使用 perform_enqueued_jobs
因此,为了修复您的测试,您需要如下所示重写它:
describe MyJob do
it "throws an error" do
expect do
described_class.perform_later
perform_enqueued_jobs
end.to raise_error(described_class::MyError)
end
end
关于ruby-on-rails - Rails 6.1 升级后,作业规范用 Minitest::UnexpectedError 包装错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72674828/
升级到 Rails 6.1 后,测试失败: class MyJob with backtrace: 当仔细观察抛出的错误时,它看起来像这样: 69: 70: describe MyJ
我是一名优秀的程序员,十分优秀!