为什么我的工作在测试期间没有执行?

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

why my job are not performed during test?

问题

以下是您的代码的翻译部分:

  1. 这是我的代码的简要描述(简化版)
  2. app/jobs/
  3. class GenerateInvoiceJob < ActiveJob::Base
  4. queue_as :default
  5. def perform()
  6. Invoice.create
  7. end
  8. end
  9. app/models/
  10. class Product < ActiveRecord::Base
  11. def buy
  12. GenerateInvoiceJob.perform_later
  13. end
  14. end
  15. spec/jobs
  16. RSpec.describe AnotherJob, type: :job do
  17. context "with filter" do
  18. ...
  19. end
  20. end
  21. spec/models
  22. RSpec.describe Product, type: :model do
  23. describe '#buy' do
  24. it "should generate invoice" do
  25. Product.create().buy
  26. expect(Invoice.all.size).to eq 1
  27. end
  28. end
  29. end
  30. 使用Rails 4.2.11时,当我运行
  31. > rspec spec/models/product_spec.rb
  32. 然后测试通过(作业已执行)
  33. 当我运行
  34. > rspec spec -e 'should generate invoice'
  35. 然后测试失败,因为作业未执行
  36. 如果我从spec/jobs中删除所有测试作业,然后运行
  37. > rspec spec -e 'should generate invoice'
  38. 然后测试通过(作业已执行)
  39. 我不明白为什么有一些作业的测试会阻止其他作业的执行?是否有解决方法?
  40. Rails 5Rails 6中,无论我做什么,测试始终失败,因为作业从未执行?
  41. 自从Rails 5以来,在测试期间不再执行作业吗?
  42. 感谢您的帮助。
  43. ***更新1:在第一次回答后***
  44. 非常感谢您的回答,为了确保我做得正确:
  45. 我在environment/test.rb中添加了以下内容
  46. config.active_job.queue_adapter = :test
  47. 并在我的spec/models/product_spec.rb中添加了以下内容
  48. ```ruby
  49. RSpec.describe Product, type: :model do
  50. describe '#buy' do
  51. it "should generate invoice" do
  52. ActiveJob::Base.queue_adapter.perform_enqueued_jobs = true
  53. Product.create().buy
  54. expect(Invoice.all.size).to eq 1
  55. end
  56. end
  57. end

不确定我是否在正确的位置放置了
ActiveJob::Base.queue_adapter.perform_enqueued_jobs = true

  1. 希望这对您有所帮助。
  2. <details>
  3. <summary>英文:</summary>
  4. here a small description of my code (simplified)
  5. app/jobs/
  6. class GenerateInvoiceJob &lt; ActiveJob::Base
  7. queue_as :default
  8. def perform()
  9. Invoice.create
  10. end
  11. end
  12. app/models/
  13. class Product &lt; ActiveRecord::Base
  14. def buy
  15. GenerateInvoiceJob.perform_later
  16. end
  17. end
  18. spec/jobs
  19. RSpec.describe AnotherJob, type: :job do
  20. context &quot;with filter&quot; do
  21. ...
  22. end
  23. end
  24. spec/models
  25. RSpec.describe Product, type: :model do
  26. describe &#39;#buy&#39; do
  27. it &quot;should generate invoice&quot; do
  28. Product.create().buy
  29. expect(Invoice.all.size).to eq 1
  30. end
  31. end
  32. end
  33. with rails 4.2.11
  34. when I run
  35. &gt; rspec spec/models/product_spec.rb
  36. then the test is ok (the job is performed)
  37. when I run
  38. &gt; rspec spec -e &#39;should generate invoice&#39;
  39. then the test fail cause the job is not performed
  40. if I delete all test jobs from spec/jobs and then run
  41. &gt; rspec spec -e &#39;should generate invoice&#39;
  42. then the test is ok (the job is performed)
  43. I can&#39;t understand why having some tests for jobs prevents other jobs to perform ? Is there a solution for this?
  44. with rails 5 and rails 6
  45. whatever I do, the test always failed as the job is never performed ?
  46. Aren&#39;t jobs performed anymore during tests since rails 5 ?
  47. thanks for help
  48. ***update 1 after first answer :***
  49. thanks a lot for your answer
  50. just to be sure I do correctly :
  51. I added in environment/test.rb
  52. config.active_job.queue_adapter = :test
  53. and in my spec/models/product_spec.rb
  54. ```ruby
  55. RSpec.describe Product, type: :model do
  56. describe &#39;#buy&#39; do
  57. it &quot;should generate invoice&quot; do
  58. ActiveJob::Base.queue_adapter.perform_enqueued_jobs = true
  59. Product.create().buy
  60. expect(Invoice.all.size).to eq 1
  61. end
  62. end
  63. end

not sure I put

  1. ActiveJob::Base.queue_adapter.perform_enqueued_jobs = true

at the good place ?!

答案1

得分: 6

需要设置:

  1. ActiveJob::Base.queue_adapter.perform_enqueued_jobs = true

然而,使用 have_enqueued_job 是一种更常见的方法。

编辑:我忘了还有一种更简单的方法:

  1. ActiveJob::Base.queue_adapter = :inline
英文:

You need to set:

  1. ActiveJob::Base.queue_adapter.perform_enqueued_jobs = true

However, using have_enqueued_job is a more common approach.

EDIT: There's even an easier way that slipped my mind:

  1. ActiveJob::Base.queue_adapter = :inline

答案2

得分: 2

你需要将你的代码包装在 perform_enqueued_jobs 中:

  1. it 'test request' do
  2. perform_enqueued_jobs do
  3. expect(request).to be_success
  4. end
  5. end
英文:

you need to wrap your code in perform_enqueued_jobs

  1. it &#39;test request&#39; do
  2. perform_enqueued_jobs do
  3. expect(request).to be_success
  4. end
  5. end

huangapple
  • 本文由 发表于 2020年1月6日 23:31:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/59614792.html
匿名

发表评论

匿名网友

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

确定