I have a list of stories sent to me at Cucumber, one of which is "Then the user should receive a confirmation email." I think the testing that the user receives is outside the scope of the application, but how can I verify that the email was sent?
You can use this step definition:
Then "the user should receive a confirmation email" do # this will get the first email, so we can check the email headers and body. email = ActionMailer::Base.deliveries.first email.from.should == " admin@example.com " email.to.should == @user.email email.body.should include("some key word or something....") end
Tested with Rails 3.2
A source
email_spec + action_mailer_cache_delivery gems are your friends for this
I suggest that you check last_response after some action, for example, clicking on a button or something like that.
last_response
Or, if you update a record after doing something, check the updated_at attribute to see if it has changed or not.
updated_at
Check the dockyard / capybara-email gem:
feature 'Emailer' do background do # will clear the message queue clear_emails visit email_trigger_path # Will find an email sent to test@example.com # and set `current_email` open_email(' test@example.com ') end scenario 'following a link' do current_email.click_link 'your profile' expect(page).to have_content 'Profile page' end scenario 'testing for content' do expect(current_email).to have_content 'Hello Joe!' end scenario 'testing for a custom header' do expect(current_email.headers).to include 'header-key' end scenario 'testing for a custom header value' do expect(current_email.header('header-key')).to eq 'header_value' end scenario 'view the email body in your browser' do # the `launchy` gem is required current_email.save_and_open end end
Another option is PutsBox . You can send an email to any-you-want@putsbox.com , wait a few seconds (the SMTP material is not instantaneous), and then check your email through http://preview.putsbox.com/p/whatever-you-want/last .
There are some examples in this post tutorial .