How to check what is queued in ActiveJob using Rspec

后端 未结 7 1219
感动是毒
感动是毒 2021-01-31 08:33

I\'m working on a reset_password method in a Rails API app. When this endpoint is hit, an ActiveJob is queued that will fire off a request to Mandrill (our transactional email c

相关标签:
7条回答
  • 2021-01-31 09:09

    I had some problems, maybe because I didn't include ActiveJob::TestHelper, but this worked for me...

    Firstly ensure, that you have the queue adapter set to :test as above answers show.

    For some reason clear_enqueued_jobs jobs in the after block didn't work for me, but the source shows we can do the following: enqueued_jobs.clear

    require 'rails_helper'
    include RSpec::Rails::Matchers
    
    RSpec.describe "my_rake_task", type: :rake do
    
      after do
        ActiveJob::Base.queue_adapter.enqueued_jobs.clear
      end  
    
    
      context "when #all task is run" do
        it "enqueues jobs which have been enabled" do
          enabled_count = get_enabled_count
          subject.execute
          expect(ActiveJob::Base.queue_adapter.enqueued_jobs.size).to eq(enabled_count)
        end
    
        it "doesn't enqueues jobs which have been disabled" do
          enabled_count = get_enabled_count
          subject.execute
          expect(ActiveJob::Base.queue_adapter.enqueued_jobs.size).to eq(enabled_count)
        end
      end
    
    end
    
    0 讨论(0)
提交回复
热议问题