LoginSignup
36
38

More than 5 years have passed since last update.

ActionMailerのテスト

Posted at

delivery_methodを :test に設定する

Rails 4 ではデフォでなってた。確認するだけ?

config/environments/test.rb
  # Tell Action Mailer not to deliver emails to the real world.                                                                                                                   
  # The :test delivery method accumulates sent emails in the                                                                                                                      
  # ActionMailer::Base.deliveries array.  
  config.action_mailer.delivery_method = :test

ActionMailer::Base.deliveriesで送信したMail::Messageにアクセスする

spec/some_spec.rb
describe "sample" do 
  #  グローバルな変数みたいなので毎回クリアする。
  after { ActionMailer::Base.deliveries.clear }

  it "sample" do 
    # メール送る処理
    ActionMailer::Base.deliveries.last #=> #<Mail::Message>
    ActionMailer::Base.deliveries.last.body #=> #<Mail::Body>

    expect(ActionMailer::Base.deliveries.last.body).to match(/wellcom/)
  end
end
36
38
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
36
38