20
11

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

ActiveJobでのRSpec

Posted at

どうしたもんやらと悩んでいたんだけど have_enqueued_job matcher ってのがあった!
これで勝つるっ!

簡単な使い方(enqueueされたかどうかだけを確認したい時)

RSpec.describe TestScheduleJob do
  ActiveJob::Base.queue_adapter = :test
  it {
    expect {
      TestScheduleJob.perform_later
    }.to have_enqueued_job(TestJob)
  }
end

enqueueされた件数をテストしたい時

RSpec.describe TestScheduleJob do
  ActiveJob::Base.queue_adapter = :test
  it {
    expect {
      TestScheduleJob.perform_later
    }.to have_enqueued_job(TestJob).exactly(1)
  }
end

exactly には数字が入ったり :once やら twice が入ったりするらしい。けど、日本人的には数字が入ってる方がわかりやすい気がする。まぁ、そこはお好みで。
(once, twice の次ってなんだっけ?ってので悩むぐらいなら...みたいな

exactly 以外にも、最低でもx件とか最高でもx件の時は at_least とか at_most でいけるっぽい

20
11
2

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
20
11

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?