How to test that ActiveJob is enqueued?

落花浮王杯 提交于 2019-12-02 06:50:28

问题


I have a create action that calls an ActiveJob if the record is successfully saved.

def create
  @object = Object.new(importer_params)
  respond_to do |format|
    if @object.save
      MyJob.perform_later( @object.id )
      format.html { redirect_to @object, notice: t('.notice') }
    else
      format.html { render :new }
    end
  end
end

I want to test that the Job is correctly called in a controller spec.

describe "POST #create" do
  it {
    expect {
      post :create, { object: valid_attributes }
    }.to change(Object, :count).by(1)
  }
  it { 
    expect {
      post :create, { object: valid_attributes }
    }.to have_enqueued_job(MyJob) 
  }  
end

But I get

Failure/Error:
  expect {
    post :create, { object: valid_attributes }
  }.to have_enqueued_job(MyJob)
  expected to enqueue exactly 1 jobs, but enqueued 0

The first test is passing, so I know the Object is saved successfully. What is the correct way to test that an ActiveJob is enqueued?


回答1:


I've always looked at the size of ActiveJob::Base.queue_adapter.enqueued_jobs to test if a job was called. giving the code

it 'does something' do
  expect {
    post :create, { object: valid_attributes }
  }.to change {
    ActiveJob::Base.queue_adapter.enqueued_jobs.count
  }.by 1
end

You should make sure that you are setting the enqueued_jobs to an empty array after each spec to avoid any unexpected behaviour. You can do this in the spec/rails_helper.rb




回答2:


If you need to check that your job has been enqueued several times, you can now do this:

expect {
  3.times { HelloJob.perform_later }
}.to have_enqueued_job(HelloJob).at_least(2).times



回答3:


In official docs here is have_enqueued_job matcher

The have_enqueued_job (also aliased as enqueue_job) matcher is used to check if given ActiveJob job was enqueued.

https://relishapp.com/rspec/rspec-rails/docs/matchers/have-enqueued-job-matcher



来源:https://stackoverflow.com/questions/41008740/how-to-test-that-activejob-is-enqueued

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!