简体   繁体   中英

How can I test an asyncronous mailer in a rails RSpec feature test?

I am trying to write a feature test where a user leaves a comment and then a notification is sent out via a rails mailer. Everything is working here if I change my mailer call to use .deliver_now but I don't want that in production. I need to be able to test the asynchronous mail delivery or even just force the mailer to deliver now in the test scenario.

login_as campaign_owner
visit campaign_path campaign
fill_in 'Comment', with: 'Foo'
click_on 'Submit'
expect(page).to have_content I18n.t('comments.create.success')
expect(campaign.comments.count).to eq 1
expect(UserMailer.deliveries.count { |d| d.to == [campaign_watcher.email]}).to eq 1

You could always just change the behaviour of your mail delivery by testing for what environment you are running in.

eg,

if Rails.env.test?
   UserMailer.mail_template().deliver_now
else
   UserMailer.mail_template().deliver_later
end

Though it's questionable what value your test is actually giving you then

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM