Rspec-rails: 负数“have_enqueued_job”在处理多个同类作业时无法按预期工作

创建于 2017-09-13  ·  3评论  ·  资料来源: rspec/rspec-rails

这是我的概念证明:

require 'spec_helper'

class MyJob < ActiveJob::Base
  def perform
    OtherJob.perform_later
    OtherJob.perform_later
  end
end

class OtherJob < ActiveJob::Base
end

describe MyJob do
  subject(:my_job) { MyJob.new }

  it 'enqueues job' do
    expect { my_job.perform }.to have_enqueued_job(OtherJob).at_least(:once)
    expect { my_job.perform }.not_to have_enqueued_job(OtherJob)
  end
end

据我了解, have_enqueued_job检查作业是否只执行了一次,因此expect { my_job.perform }.to have_enqueued_job(OtherJob)expect { my_job.perform }.to have_enqueued_job(OtherJob).exactly(:once)

但在负面情况下, expect { my_job.perform }.not_to have_enqueued_job(OtherJob)expect { my_job.perform }.not_to have_enqueued_job(OtherJob).exactly(:once) 。 因此,当存在多个相同类型的作业时,为什么expect { my_job.perform }.not_to have_enqueued_job(OtherJob)返回 true 的原因很清楚,但行为令人困惑。

尝试使用 rspec-rails 3.6.1。

最有用的评论

我遇到了同样的问题。 你可以破解它:

expect{ my_job.perform }.to have_enqueued_job(OtherJob).exactly(0).times

所有3条评论

我不确定你在这里问什么,看起来你的代码片段应该失败,因为expect { my_job.perform }.not_to have_enqueued_job(OtherJob)意味着你没有排队任何工作,你有。 期望对彼此没有任何了解。

你是说通过了吗? 因为那是一个错误。

我是说expect { my_job.perform }.to have_enqueued_job(OtherJob).at_least(:once)通过和expect { my_job.perform }.not_to have_enqueued_job(OtherJob)通过

我遇到了同样的问题。 你可以破解它:

expect{ my_job.perform }.to have_enqueued_job(OtherJob).exactly(0).times
此页面是否有帮助?
0 / 5 - 0 等级