Pony-Test
A collection of helper methods and Cucumber steps to make testing email with Pony a piece of cake. It works simply by overwriting the Pony mail method with one that stores the email in an array instead of sending it. You can access this email with the helper methods and perform whatever checks you want.
Usage
Helper methods
deliveries
, all_email
returns the whole array of emails
current_email
returns either the last email sent, or the last email “opened”
current_email_address
returns the last address used in a lookup operation
reset_mailer
clears everything
last_email_sent
“opens” the last email sent
inbox
, inbox_for
returns all emails, filterable by address, pass the option :address => '[email protected]'
open_email
, open_email_for
“opens” the first email matching all criteria, accepts options :address
, :with_subject
, and :with_body
find_email
, find_email_for
returns all email matching all criteria, accepts options :address
, :with_subject
, and :with_body
email_links
returns all links in an email body, searching current_email
by default
email_links_matching
returns all links matching a pattern
Cucumber
Scenario: A new person signs up
Given I am at "/"
When I fill in "Email" with "[email protected]"
And I press "Sign up"
Then I should have an email
And I should see "confirm" in the email body
When I visit "confirm" in the email
Then I should see "Confirm your new account"
Please read lib/email_steps.rb
for the other steps. For more examples, check out the examples
folder for a small example app that implements the steps.
Setup
For now, I am including this project into others with a symlink. If you clone this project next to your current working project, run this inside you current project:
ln -s ../pony-test pony-test
To require files from this folder, you may first want to add the lib
dir to the load path. For example, from the file spec/spec_helper.rb
you could add:
$: << File.join(File.(File.dirname(__FILE__)), %w{ .. pony-test lib })
...
require 'pony-test'
A gem for this project is coming soon.
Cucumber
To use pony-test in Cucumber include the helpers in features/support/env.rb
:
World do
...
include Pony::TestHelpers
end
This will load all the helpers that the steps rely on. You probably want all emails to be cleared for each scenario:
Before do
reset_mailer
end
Also, copy email_steps.rb
to features/step_definitions
to get access to many predifined steps.
RSpec
You will need to include Pony::TestHelpers in your example groups. If you want to have access to the helpers in all of your examples you can do the following in your spec_helper.rb
:
Spec::Runner.configure do |config|
config.include(Pony::TestHelpers)
end
Otherwise, you will need to include them in the example groups you wish to use them:
describe "Signup Email" do
include Pony::TestHelpers
...
end
Remember to call reset_mailer
in a before
block or whenever you want the emails to be reset.
Acknowledgements
This project is a major simplification and rewrite of the Email-Spec project:
If you are a Rails/ActionMailer user, you may want to check it out.