Rails:如何在测试中使用内联适配器正确执行 ActiveJob?

huangapple go评论136阅读模式
英文:

Rails: How to correctly execute an ActiveJob within a test using inline adapter?

问题

我有以下的测试代码:

  1. require "test_helper"
  2. class DisbursementGenerationTest < ActionDispatch::IntegrationTest
  3. test "disburse test orders" do
  4. puts "Performing job..."
  5. Merchant.all.each do |merchant|
  6. DisburseJob.perform_later(merchant)
  7. end
  8. puts "Job performed"
  9. sleep 10
  10. assert_equal 2, Disbursement.count
  11. end
  12. end

/config/environment/test.rb 文件中,我将 ActiveJob 的队列适配器设置为 :inline

  1. Rails.application.configure do
  2. config.active_job.queue_adapter = :inline

测试中的任务根本没有执行。我可以看到两个 puts 语句的输出,但是任务没有运行。我已经检查了预期的两个商家对象存在。在开发环境中,我使用 Sidekiq 作为任务后端,它可以在 Rails 控制台手动执行任务,并且也可以执行应用程序中的定时任务。

如何在测试中正确执行 ActiveJob?

我已经执行了这个测试,并期望任务运行两次(每个商家对象运行一次)。但是任务根本没有执行。

英文:

I have the following test:

  1. require &quot;test_helper&quot;
  2. class DisbursementGenerationTest &lt; ActionDispatch::IntegrationTest
  3. test &quot;disburse test orders&quot; do
  4. puts &quot;Performing job...&quot;
  5. Merchant.all.each do |merchant|
  6. DisburseJob.perform_later(merchant)
  7. end
  8. puts &quot;Job performed&quot;
  9. sleep 10
  10. assert_equal 2, Disbursement.count
  11. end
  12. end

In the /config/environment/test.rb I set the ActiveJob queue adapter to inline:

  1. Rails.application.configure do
  2. config.active_job.queue_adapter = :inline

The Job in the test is not executed at all. I see output from both puts statements, but job is not running. I have checked there are 2 merchant objects as expected. In Dev environment I use sidekiq as a job backend and it executes jobs fine both from rails console manually and also scheduled jobs within an app.

How to correctly execute ActiveJob within a test?

I have executed the test and expected the job to run twice (once for each merchant object). The job has not executed at all.

答案1

得分: 3

为了进行测试,你应该将队列适配器设置为test

  1. config.active_job.queue_adapter = :test

然后你可以使用perform_enqueued_jobs来执行作业:

  1. class DisbursementGenerationTest &lt; ActionDispatch::IntegrationTest
  2. test &quot;disburse test orders&quot; do
  3. Merchant.all.each do |merchant|
  4. DisburseJob.perform_later(merchant)
  5. end
  6. perform_enqueued_jobs
  7. assert_equal 2, Disbursement.count
  8. assert_performed_jobs 2
  9. end
  10. end

有关test helper测试AJ一般的更多信息。

英文:

For testing, you should set the queue adapter to test:

  1. config.active_job.queue_adapter = :test

And then you can execute the jobs with the perform_enqueued_jobs:

  1. class DisbursementGenerationTest &lt; ActionDispatch::IntegrationTest
  2. test &quot;disburse test orders&quot; do
  3. Merchant.all.each do |merchant|
  4. DisburseJob.perform_later(merchant)
  5. end
  6. perform_enqueued_jobs
  7. assert_equal 2, Disbursement.count
  8. assert_performed_jobs 2
  9. end
  10. end

Further info about the test helper and testing AJ generally.

huangapple
  • 本文由 发表于 2023年8月9日 02:22:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/76862248.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定