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

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

why my job are not performed during test?

问题

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

这是我的代码的简要描述(简化版)

app/jobs/

class GenerateInvoiceJob < ActiveJob::Base
  queue_as :default

  def perform()
    Invoice.create
  end
end

app/models/

class Product < ActiveRecord::Base
  def buy
    GenerateInvoiceJob.perform_later
  end
end

spec/jobs

RSpec.describe AnotherJob, type: :job do

  context "with filter" do
    ...
  end
end

spec/models

RSpec.describe Product, type: :model do

    describe '#buy' do
      it "should generate invoice" do
        Product.create().buy

        expect(Invoice.all.size).to eq 1
      end
    end
end

使用Rails 4.2.11时,当我运行

> rspec spec/models/product_spec.rb

然后测试通过(作业已执行)

当我运行

> rspec spec -e 'should generate invoice'

然后测试失败,因为作业未执行

如果我从spec/jobs中删除所有测试作业,然后运行

> rspec spec -e 'should generate invoice'

然后测试通过(作业已执行)

我不明白为什么有一些作业的测试会阻止其他作业的执行?是否有解决方法?

Rails 5Rails 6中,无论我做什么,测试始终失败,因为作业从未执行?

自从Rails 5以来,在测试期间不再执行作业吗?

感谢您的帮助。

***更新1:在第一次回答后***

非常感谢您的回答,为了确保我做得正确:

我在environment/test.rb中添加了以下内容
config.active_job.queue_adapter = :test

并在我的spec/models/product_spec.rb中添加了以下内容

```ruby
RSpec.describe Product, type: :model do
  describe '#buy' do
    it "should generate invoice" do
      ActiveJob::Base.queue_adapter.perform_enqueued_jobs = true
      Product.create().buy

      expect(Invoice.all.size).to eq 1
    end
  end
end

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


希望这对您有所帮助。
<details>
<summary>英文:</summary>
here a small description of my code (simplified)
app/jobs/
class GenerateInvoiceJob &lt; ActiveJob::Base
queue_as :default
def perform()
Invoice.create
end
end
app/models/
class Product &lt; ActiveRecord::Base
def buy
GenerateInvoiceJob.perform_later
end
end
spec/jobs
RSpec.describe AnotherJob, type: :job do
context &quot;with filter&quot; do
...
end
end
spec/models
RSpec.describe Product, type: :model do
describe &#39;#buy&#39; do
it &quot;should generate invoice&quot; do
Product.create().buy
expect(Invoice.all.size).to eq 1
end
end
end
with rails 4.2.11
when I run 
&gt; rspec spec/models/product_spec.rb
then the test is ok (the job is performed)
when I run 
&gt; rspec spec -e &#39;should generate invoice&#39;
then the test fail cause the job is not performed
if I delete all test jobs from spec/jobs and then run
&gt; rspec spec -e &#39;should generate invoice&#39;
then the test is ok (the job is performed)
I can&#39;t understand why having some tests for jobs prevents other jobs to perform ? Is there a solution for this?
with rails 5 and rails 6
whatever I do, the test always failed as the job is never performed ?
Aren&#39;t jobs performed anymore during tests since rails 5 ?
thanks for help
***update 1 after first answer :***
thanks a lot for your answer
just to be sure I do correctly :
I added in environment/test.rb
config.active_job.queue_adapter = :test
and in my spec/models/product_spec.rb
```ruby
RSpec.describe Product, type: :model do
describe &#39;#buy&#39; do
it &quot;should generate invoice&quot; do
ActiveJob::Base.queue_adapter.perform_enqueued_jobs = true
Product.create().buy
expect(Invoice.all.size).to eq 1
end
end
end

not sure I put

    ActiveJob::Base.queue_adapter.perform_enqueued_jobs = true

at the good place ?!

答案1

得分: 6

需要设置:

ActiveJob::Base.queue_adapter.perform_enqueued_jobs = true

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

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

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

You need to set:

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:

ActiveJob::Base.queue_adapter = :inline

答案2

得分: 2

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

it 'test request' do
  perform_enqueued_jobs do
    expect(request).to be_success
  end
end
英文:

you need to wrap your code in perform_enqueued_jobs

it &#39;test request&#39; do
perform_enqueued_jobs do
expect(request).to be_success
end
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:

确定