作业为某些测试内联运行

作业为某些测试内联运行

本文介绍了rspec rails 测试:如何强制 ActiveJob 作业为某些测试内联运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望我的后台作业针对某些标记测试内联运行.我可以通过用 perform_enqueued do 包装测试来实现,但我希望能够用元数据标记它们,如果可能的话,它会自动发生.

I would like my background jobs to run inline for certain marked tests. I can do it by wrapping the test with perform_enqueued do but I'd like to just be able to tag them with metadata and it happens automatically, if possible.

我尝试了以下方法:

it "does everything in the job too", perform_enqueued: true do
end

config.around(:each) do |example|
  if example.metadata[:perform_enqueued]
    perform_enqueued_jobs do
      example.run
    end
  end
end

但会导致错误:

undefined method `perform_enqueued_jobs=' for ActiveJob::QueueAdapters::InlineAdapter:Class

推荐答案

您需要将您的测试适配器设置为 ActiveJob::QueueAdapters::TestAdapter 响应 .perform_enqueued_jobs =.您可以在 spec/rails_helper.rb 文件中执行此操作:

You need to set your adapter for tests to ActiveJob::QueueAdapters::TestAdapter which responds to .perform_enqueued_jobs =. You can do that on your spec/rails_helper.rb file:

ActiveJob::Base.queue_adapter = :test

这篇关于rspec rails 测试:如何强制 ActiveJob 作业为某些测试内联运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 23:11