If you are testing emails in conjunction with an automated browser
solution, like Selenium, you will want to use action_mailer_cache_delivery
in your test environment. (This is because your test process and server
processes are distinct and therefore need an intermediate store for the
emails.) DelayedJob and ActiveRecord Mailer will also work but you
generally don't want to include those projects unless you need them in
production.

Setup

script/plugin install git://github.com/bmabey/email-spec.git

Gem Setup

geminstallemail_spec# Gemfile
group:testdogem'email_spec'end

Cucumber

To use the steps in features put the following in your env.rb:

# Make sure this require is after you require cucumber/rails/world.
require'email_spec'# add this line if you use spork
require'email_spec/cucumber'

This will load all the helpers that the steps rely on. It will also add a
Before hook for Cucumber so that emails are cleared at the start of each
scenario.

Then:

railsgenerateemail_spec:steps

This will give you a bunch of steps to get started with in
step_definitions/email_steps.rb

By default, the generated file will look for email to example@example.com.
You can either change this by editing the current_email_address method in
email_steps.rb, or by simply specifying the target email in your features:

Scenario: A new person signs up
Given I am at "/"
When I fill in "Email" with "quentin@example.com"
And I press "Sign up"
And "quentin@example.com" should receive an email # Specify who should receive the email

RSpec

First you need to require email_spec in your spec_helper.rb:

require"email_spec"

You will then need to include EmailSpec::Helpers and EmailSpec::Matchers in
your example groups. If you want to have access to the helpers and matchers
in all of your examples you can do the following in your spec_helper.rb:

Otherwise, you will need to include them in the example groups you wish to
use them:

describe "Signup Email" do
include EmailSpec::Helpers
include EmailSpec::Matchers
...
end

Usage

Cucumber

Scenario: A new person signs up
Given I am at "/"
When I fill in "Email" with "quentin@example.com"
And I press "Sign up"
And I should receive an email
When I open the email
Then I should see "confirm" in the email body
When I follow "confirm" in the email
Then I should see "Confirm your new account"

For more examples, check out examples/rails_root in the source for a small
example app that implements these steps.

RSpec

Testing In Isolation

It is often useful to test your mailers in isolation. You can accomplish
this by using mocks to verify that the mailer is being called in the
correct place and then write focused examples for the actual mailer. This
is a simple example from the sample app found in the gem:

Verify that the mailer is used correctly in the controller (this would
apply to a model as well):

describe"Signup Email"doincludeEmailSpec::HelpersincludeEmailSpec::MatchersincludeActionController::UrlWriterbefore(:all)do@email=UserMailer.create_signup("jojo@yahoo.com","Jojo Binks")endit"should be set to be delivered to the email passed in"do@email.shoulddeliver_to("jojo@yahoo.com")endit"should contain the user's message in the mail body"do@email.shouldhave_body_text(/Jojo Binks/)endit"should contain a link to the confirmation link"do@email.shouldhave_body_text(/#{confirm_account_url}/)endit"should have the correct subject"do@email.shouldhave_subject(/Account confirmation/)endend

Using the helpers when not testing in isolation

Don't. :) Seriously, if you do just take a look at the helpers and use
them as you wish.