jdkealy
jdkealy

Reputation: 4907

testing email gets sent in cucumber

I have a list of stories assigned to me in Cucumber, one of them being "Then the user should receive a confirmation email". I think testing that the user receives it is beyond the power of the application, but how can I test that an email had just been sent?

Upvotes: 5

Views: 3525

Answers (5)

Pablo Cantero
Pablo Cantero

Reputation: 6357

Another option is PutsBox. You can send an email to [email protected], wait for a few seconds (SMTP stuff ins't instantaneous) then check your email via http://preview.putsbox.com/p/whatever-you-want/last.

This post tutorial has some examples.

Upvotes: 0

MhdSyrwan
MhdSyrwan

Reputation: 1633

Check 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 [email protected]
    # and set `current_email`
    open_email('[email protected]')
  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

Upvotes: 0

fakeleft
fakeleft

Reputation: 2900

email_spec + action_mailer_cache_delivery gems are your friends for doing this

Upvotes: 2

ouranos
ouranos

Reputation: 1234

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 == "[email protected]"
  email.to.should == @user.email
  email.body.should include("some key word or something....")
end

Tested with Rails 3.2

Source

Upvotes: 7

Kleber S.
Kleber S.

Reputation: 8240

I would suggest you to verify the last_response after some action ocurrs, like, a user click on a button, or something like that.

Or if you are updating a record after doing something, check for the updated_at attribute to see if it was changed or not.

Upvotes: 0

Related Questions