升级到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包装,但原始的底层错误仍然存在.

你知道怎么解决这个问题吗?

推荐答案

Rails 6.1中进行了These次更改,共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 7.1.3如何在生产环境中查看堆栈跟踪

使用超级用户角色和未知密码创建与POSTRES用户的连接

Heroku、Selenium、Chome和Chromedriver的复杂问题(抱歉,无法简单描述,请阅读详细信息)

如何从 gem 覆盖 JSONAPI.configure

Rails HMAC - 使用应用程序机密作为加密密钥

我需要在我的 Rails 7 应用程序中保留 app/assets/config/manifest.js 吗?

Rails:序列化数据库中的对象?

设计 token_authenticable 已弃用,有什么替代方案?

Rails - 按连接表数据排序

2个空格或1个制表符,Rails社区的缩进标准是什么?

有没有办法在模型而不是视图中使用复数()?

上传图片 Ruby on Rails

在 Ruby on Rails 中获取空临时目录的最佳方法是什么?

jQuery ajax 请求不会触发 Rails 控制器的 JS 响应?

Rails 5:重命名表迁移

长期运行项目中的 Rebase Rails 迁移

在 Rails 中,在特定时区创建特定时间(不是现在)的最佳方式是什么?

向 Rails 模型添加外键

rake 任务中的 def 块

如何在 Rails 表单中将复选框和标签保持在同一行?