作业规格包装错误与Minitest :: Rails 6.1升级后的InfirantError

发布于 2025-02-08 11:17:12 字数 1978 浏览 3 评论 0原文

升级到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 :: Infirate> firtesterror包裹的错误。 ,但是原始的基础错误仍然存​​在。

知道如何解决这个问题?

After upgrading to Rails 6.1, a test is broken:

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

This spec fails with the error

expected MyJob::MyError, got #<Minitest::UnexpectedError: Unexpected exception> with backtrace:

When looking closer what error is thrown, it looks like this:

    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'

It seems like the error is somehow wrapped with Minitest::UnexpectedError, but the original underlying error is still there.

Any idea how to fix this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

颜漓半夏 2025-02-15 11:17:12

这些percorm_enqueed_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

These changes were made in Rails 6.1 with perform_enqueued_jobs

So to fix your test you need to rewrite it like shown below:

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文