为什么在请求测试中没有使用 ActiveJob InlineAdapter?
Why is ActiveJob InlineAdapter not used in request tests?
我的 test.rb 中有此代码:
config.active_job.queue_adapter = :inline
在测试中我有:
scenario '15 minutes after last call a recall should happen' do
p ActiveJob::Base.queue_adapter
end
这个returns:
ActiveJob::QueueAdapters::InlineAdapter
这很好,因为 perform_later 会立即执行。
然而,当我像这样将 type::request 添加到测试中时:
scenario '15 minutes after last call a recall should happen', type: :request do
p ActiveJob::Base.queue_adapter
end
我得到:requestActiveJob::QueueAdapters::TestAdapter 并且 perform_later 不再执行。这是有意的行为吗?如何确保 perform_later 块始终在测试中执行?
作业未排队等待执行,但已排队以检查是否已排队(basically just saved in an array 因此您可以测试它们是否存在):
https://edgeapi.rubyonrails.org/classes/ActiveJob/QueueAdapters/TestAdapter.html
既然你用 rspec 标记了这个问题,那么有一个完美的匹配器适合你:have_been_enqueued
可以像这样使用:
RSpec.describe UploadBackupsJob do
it "matches with enqueued job" do
ActiveJob::Base.queue_adapter = :test
UploadBackupsJob.perform_later
expect(UploadBackupsJob).to have_been_enqueued
end
end
这与此问题相关:https://github.com/rails/rails/issues/37270
放
(ActiveJob::Base.descendants << ActiveJob::Base).each(&:disable_test_adapter)
在实际测试中修复它。
另一个更简洁的选择是(如果可能)将测试类型更改为:type::system 并确保您的 rspec-rails版本 >= 4.1
我的 test.rb 中有此代码:
config.active_job.queue_adapter = :inline
在测试中我有:
scenario '15 minutes after last call a recall should happen' do
p ActiveJob::Base.queue_adapter
end
这个returns: ActiveJob::QueueAdapters::InlineAdapter
这很好,因为 perform_later 会立即执行。
然而,当我像这样将 type::request 添加到测试中时:
scenario '15 minutes after last call a recall should happen', type: :request do
p ActiveJob::Base.queue_adapter
end
我得到:requestActiveJob::QueueAdapters::TestAdapter 并且 perform_later 不再执行。这是有意的行为吗?如何确保 perform_later 块始终在测试中执行?
作业未排队等待执行,但已排队以检查是否已排队(basically just saved in an array 因此您可以测试它们是否存在):
https://edgeapi.rubyonrails.org/classes/ActiveJob/QueueAdapters/TestAdapter.html
既然你用 rspec 标记了这个问题,那么有一个完美的匹配器适合你:have_been_enqueued
可以像这样使用:
RSpec.describe UploadBackupsJob do
it "matches with enqueued job" do
ActiveJob::Base.queue_adapter = :test
UploadBackupsJob.perform_later
expect(UploadBackupsJob).to have_been_enqueued
end
end
这与此问题相关:https://github.com/rails/rails/issues/37270
放
(ActiveJob::Base.descendants << ActiveJob::Base).each(&:disable_test_adapter)
在实际测试中修复它。
另一个更简洁的选择是(如果可能)将测试类型更改为:type::system 并确保您的 rspec-rails版本 >= 4.1