• Stars
    star
    339
  • Rank 124,632 (Top 3 %)
  • Language
    Ruby
  • License
    MIT License
  • Created over 12 years ago
  • Updated over 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Test your ActionMailer and Mailer messages with Capybara

CapybaraEmail

Build 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 [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 attachments' do
    expect(current_email.attachments.first.filename).to eq 'filename.csv'
  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

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 "[email protected]" is playing a game
  When that user picks a winning piece
  Then "[email protected]" 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)
  expect(current_email.subject).to 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 < ActionDispatch::IntegrationTest
  def setup
    # 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

  test 'testing any email is sent' do
    expect(all_emails).not_to be_empty
  end

  test 'following a link' do
    current_email.click_link 'your profile'
    expect(page).to have_content 'Profile page'
  end

  test 'testing for content' do
    expect(current_email).to have_content 'Hello Joe!'
  end

  test 'testing for a custom header' do
    expect(current_email.headers).to include 'header-key'
  end

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

Setting your test host

When testing, it's common to want to open an email and click through to your application. To do this, you'll probably need to update your test environment, as well as Capybara's configuration.

By default, Capybara's app_host is set to http://example.com. You should update this so that it points to the same host as your test environment. In our example, we'll update both to http://localhost:3001:

# tests/test_helper.rb
ActionDispatch::IntegrationTest do
  Capybara.server_port = 3001
  Capybara.app_host = 'http://localhost:3001'
end

# config/environments/test.rb
config.action_mailer.default_url_options = { host: 'localhost', 
                                             port: 3001 }

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 '[email protected]'

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!

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

Legal

DockYard, Inc. ยฉ 2014

@dockyard

Licensed under the MIT license

More Repositories

1

client_side_validations

Client Side Validations made easy for Ruby on Rails
Ruby
2,686
star
2

ember-validations

Validations for Ember Objects
JavaScript
832
star
3

postgres_ext

Adds support for missing PostgreSQL data types to ActiveRecord
Ruby
643
star
4

ember-easy-form

Easily build semantic forms in Ember
JavaScript
565
star
5

party_foul

Use GitHub to track your application errors!
Ruby
517
star
6

ruby-destroyed_at

ActiveRecord Mixin for Safe Destroys
Ruby
349
star
7

postgres_ext-serializers

Ruby
324
star
8

client_side_validations-simple_form

Simple Form plugin for ClientSideValidations
JavaScript
254
star
9

ember-appkit-rails

Ember Appkit for Rails
Ruby
238
star
10

ember-suave

Make your Ember App Stylish
JavaScript
179
star
11

ember-one-way-controls

Native one way input
JavaScript
176
star
12

ember-data-route

Common teardown scenario for ember routes backed by a data model
JavaScript
120
star
13

ember-cli-i18n

Simple Internationalization support for ember-cli apps
JavaScript
112
star
14

dismissible_helpers

Ruby
96
star
15

es6_module_transpiler-rails

Transpile ES6 Modules in the Rails Asset Pipeline
JavaScript
87
star
16

capybara-extensions

Complements Capybara with additional finders and matchers.
Ruby
66
star
17

ruby-context_validations

Context Aware Validations for Rails
Ruby
65
star
18

pg_array_parser

Ruby
61
star
19

pages

Easy pages in Rails
Ruby
49
star
20

ember-skeleton

Show fast-loading temporary images in place of an eventual slow-loading image
JavaScript
41
star
21

ember-cli-proxy-fixtures

Ember CLI Proxy Fixtures
JavaScript
38
star
22

client_side_validations-mongoid

Mongoid plugin for ClientSideValidations
Ruby
28
star
23

ruby-easy_auth

Dead simple drop-in identity based Rails authentication
Ruby
28
star
24

ember-admin-bootstrap

Ember Admin with a Twitter Bootstrap Theme
CSS
25
star
25

client_side_validations-formtastic

Formtastic plugin for ClientSideValidations
Ruby
18
star
26

ember-wuphf

JavaScript
17
star
27

postgres_ext-postgis

Ruby
14
star
28

ember-cli-one-script

This addon combines your `vendor.js` and `<your-app-name>.js` into a single file called `app.js`
JavaScript
14
star
29

client_side_validations-turbolinks

Turbolinks Plugin for ClientSideValidations
Ruby
9
star
30

fixtory

Not quite fixtures, not quite factories
Ruby
9
star
31

ember-new-modules-shim

JavaScript
6
star
32

client_side_validations-backbone

Backbone plugin for ClientSideValidations
JavaScript
6
star
33

ember-cli-test-interactions

Ember acceptance test helpers.
JavaScript
3
star
34

minitest-moar

Moar Minitest Pluzsh!
Ruby
3
star
35

ruby-easy_auth-password

Password plugin for EasyAuth
Ruby
3
star
36

mail_congress

Ruby
2
star
37

ruby-easy_auth-oauth2

Ruby
1
star
38

ruby-easy_auth-twitter

Ruby
1
star
39

ruby-easy_auth-linked_in

Ruby
1
star
40

comet

Elixir
1
star