LoginSignup
0
0

More than 5 years have passed since last update.

RSpecでの一時的な非同期Jobのqueue_adapter設定変更メモ

Posted at

結論

あるspecファイルだけで一時的に queue_adapter を変更したい場合は

require 'spec_helper'

RSpec.describe HogeJob do
  include ActiveJob::TestHelper
  Rails.application.config.active_job.queue_adapter = :test # <= これ

  it do
    perform_enqueued_jobs do
      HogeJob.new.perform("my")
    end
  end
end

のように設定すること。

悪い例

次のように設定してはいけない。他のspecまで影響を受けてしまい、 :test アダプター以外を使う前提でテストが書かれている場合にテストが通らなくなってしまった。

require 'spec_helper'

RSpec.describe HogeJob do
  include ActiveJob::TestHelper
  ActiveJob::Base.queue_adapter = :test # <= ダメ

  it do
    perform_enqueued_jobs do
      HogeJob.new.perform("my")
    end
  end
end

理由

誰か教えて

ちなみに

すべてのspecに queue_adapter の変更を反映したい場合は普通 config/environments/test.rb で設定するよね。
今回は一部のspecでのみ、変更したくなって上記のハマりポイントを踏んだ。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0