1
0

deliver_later をテストの時だけ即時実行したい

Posted at

時は、perform_enqueued_jobs と書く。

  it do
    perform_enqueued_jobs do
      subject
    end

    expect(ActionMailer::Base.deliveries.size).to(eq(1))
  end

とりあえず動いてるか?のテストしたい時はこれで良さそう。

ちゃんとどのメソッドが呼ばれてるか?みたいなテストにしたいなら、呼ばれてるMailerをスタブしないとダメそう。

一周回って、結合テスト的に呼び出し〜どんな内容のメールが送信されているか?でテストするなら、このように書くことでメールの内容が検証できる。

  mail = ActionMailer::Base.deliveries.last

  expect(mail.subject).to eq('Expected Subject')
  expect(mail.to).to eq(['example@example.com'])
  expect(mail.from).to eq(['from@example.com'])
  expect(mail.body.encoded).to match('Expected Body Content')

サービスの開始時なんかはこういうテストだけを書いておくほうが変更時に楽かもしれないなー。

という何番煎じかわからん小ネタでした。

1
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
1
0