How to test content of Devise emails with email_spec gem and cucumber?

620 views Asked by At

I am learning how to test email generation and delivery with cucumber and th email-spec gem.

I have installed the gem and generated the email_steps.rb file.

However the ActionMailer::Base.deliveries array always appears to be empty so I am not able to test the email.

My scenario looks like this:

Background:
    Given I visit the User Registration page

  Scenario: Happy Path - Seccessful Sign Up
    And I fill in the User Registration form correctly
    And I click Register
    Then I am redirected to the Users index page
    And I can see email confirmation notification
    And "[email protected]" should receive an email 
    When I open the email with subject "Confirmation instructions"
    And I follow "Confirm my account" in the email
    And I am redirected to the Sign In page
    And I fill in the Sign In form correctly
    And I click Log In
    Then I am redirected to the Home page
    And I can see "Signed in successfully" notification

And it is failing on the

And "[email protected]" should receive an email with subject "Confirmation instructions"

I have checked that the email is sent in the development environment with the letter opener gem but I dont know why it isnt being found in the test environment by the

Then /^(?:I|they|"([^"]*?)") should receive (an|no|\d+) emails?$/ do |address, amount|
  unread_emails_for(address).size.should == parse_email_count(amount)
end

step from the email-spec gem.

Is there some configuarion settings I am missing?

1

There are 1 answers

0
Cu1ture On BEST ANSWER

The issue was that I had set the

  config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }

In the development environment but not in the test environment which meant there was an error with the email being sent in the test environment even though it appeared fine when doing it manually in the development environment.

This was the main issue, a second minor issue is that I had a capital letter in my email address which is downcased when sent. This meant when the email-spec gem looked for the email it could not find a match because they did not exactly match (the one in the deliveries array was downcased).

A great way to debug this is to set up a step definition for debugging pages in cucumber as below:

Then(/^show me the page$/) do
  save_and_open_page
end

This lets you view the current page in the browser which allowed me to see the error messages and figure out the problem.

I hope that helps if someone gets stuck on the same thing in the future