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