Skip to content
This repository

Test your ActionMailer and Mailer messages with Capybara

Merge pull request #39 from hasclass/patch-1

Make changes to spec_helper.rb more prominent
latest commit 86e0b7dd33
Brian Cardarella bcardarella authored
Octocat-spinner-32 lib Version bump February 09, 2014
Octocat-spinner-32 spec Delegation February 09, 2014
Octocat-spinner-32 .gitignore Ignore binstubs July 13, 2012
Octocat-spinner-32 .rspec Initial commit March 04, 2012
Octocat-spinner-32 .travis.yml Travis: Remove JRuby, Rubinius, and 1.9.3 December 04, 2013
Octocat-spinner-32 .yardopts Better documentation March 04, 2012
Octocat-spinner-32 CHANGELOG.md Delegation February 09, 2014
Octocat-spinner-32 Gemfile Eager load ActionMailer::Base June 10, 2013
Octocat-spinner-32 README.md Make changes to spec_helper.rb more prominent March 17, 2014
Octocat-spinner-32 Rakefile Fix Rakefile August 12, 2012
Octocat-spinner-32 capybara-email.gemspec Update Capybara to v2.2.0 December 05, 2013
README.md

CapybaraEmail

Build Status Dependency Status Code Climate

Easily test ActionMailer and Mail messages in your Capybara integration tests

Installation

Add this line to your application's Gemfile:

gem 'capybara-email'

And then execute:

$ bundle

Or install it yourself as:

$ gem install capybara-email

Usage

RSpec

In your spec_helper.rb require capybara/email/rspec.

require 'capybara/email/rspec'

Example:

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'
    page.should have_content 'Profile page'
  end

  scenario 'testing for content' do
    current_email.should have_content 'Hello Joe!'
  end

  scenario 'testing for a custom header' do
    current_email.headers.should include 'header-key'
  end

  scenario 'testing for a custom header value' do
    current_email.header('header-key').should 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

Cucumber

Require capybara/email in your features/support/env.rb

require 'capybara/email'

Once you have required capybara-email, gaining access to usable methods is easy as adding this module to your Cucumber World:

World(Capybara::Email::DSL)

I recommend adding this to a support file such as features/support/capybara_email.rb

require 'capybara/email'
World(Capybara::Email::DSL)

Example:

Scenario: Email is sent to winning user
  Given "me@example.com" is playing a game
  When that user picks a winning piece
  Then "me@example.com" receives an email with "You've Won!" as the subject

Then /^"([^"]*)" receives an email with "([^"]*)" as the subject$/ do |email_address, subject|
  open_email(email_address)
  current_email.subject.should eq subject
end

Test::Unit

Require capybara/email at the top of test/test_helper.rb

  require 'capybara/email'

Include Capybara::Email::DSL in your test class

class ActionDispatch::IntegrationTest
  include Capybara::Email::DSL
end

Example:

class EmailTriggerControllerTest < ActionController::IntegrationTest
  def setup
    # 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

  test 'following a link' do
    current_email.click_link 'your profile'
    page.should have_content 'Profile page'
  end

  test 'testing for content' do
    current_email.should have_content 'Hello Joe!'
  end

  test 'testing for a custom header' do
    current_email.headers.should include 'header-key'
  end

  test 'testing for a custom header value' do
    current_email.header('header-key').should eq 'header_value'
  end

  test 'view the email body in your browser' do
    # the `launchy` gem is required
    current_email.save_and_open
  end
end

CurrentEmail API

The current_email method will delegate all necessary method calls to Mail::Message. So if you need to access the subject of an email:

current_email.subject`

Check out API for the mail gem for details on what methods are available.

Sending Emails with JavaScript

Sending emails asynchronously will cause #open_email to not open the correct email or not find any email at all depending on the state of the email queue. We recommend forcing a sleep prior to trying to read any email after an asynchronous event:

click_link 'Send email'
sleep 0.1
open_email 'test@example.com'

Authors

Brian Cardarella

We are very thankful for the many contributors

Versioning

This gem follows Semantic Versioning

Want to help?

Stable branches are created based upon each minor version. Please make pull requests to specific branches rather than master.

Please make sure you include tests!

Unless Rails drops support for Ruby 1.8.7 we will continue to use the hash-rocket syntax. Please respect this.

Don't use tabs to indent, two spaces are the standard.

Legal

DockYard, LLC © 2012

@dockyard

Licensed under the MIT license

Something went wrong with that request. Please try again.