public
Description: Collection of rspec matchers and cucumber steps for testing email in a ruby app using ActiveMailer or Pony
Homepage: http://gemcutter.org/gems/email_spec
Clone URL: git://github.com/bmabey/email-spec.git
Click here to lend your support to: email-spec and make a donation at www.pledgie.com !
name age message
file .gitignore Fri Dec 19 09:59:30 -0800 2008 moved files around and improved documenation on... [bmabey]
file History.txt Wed Apr 15 20:38:31 -0700 2009 Merge branch 'cucumber-master' Conflicts: His... [bmabey]
file MIT-LICENSE.txt Thu Jan 22 07:42:04 -0800 2009 License update [bmabey]
file README.rdoc Tue Jan 20 09:44:12 -0800 2009 Merge branch 'master' of git://github.com/bmabe... [bmabey]
file Rakefile Sun Apr 05 19:23:36 -0700 2009 working on making rubigen happy [Ben Mabey]
file VERSION.yml Wed Apr 15 20:39:19 -0700 2009 Version bump to 0.1.3 [bmabey]
file email_spec.gemspec Wed Apr 15 20:39:19 -0700 2009 Version bump to 0.1.3 [bmabey]
directory examples/ Wed Apr 15 20:38:31 -0700 2009 Merge branch 'cucumber-master' Conflicts: His... [bmabey]
file install.rb Thu Dec 18 01:10:24 -0800 2008 gemify [mischa]
directory lib/ Tue Apr 07 02:12:31 -0700 2009 World semantics changed in cucumber, made the n... [hectoregm]
directory rails_generators/ Wed Apr 15 20:38:31 -0700 2009 Merge branch 'cucumber-master' Conflicts: His... [bmabey]
directory spec/ Thu Mar 19 18:36:31 -0700 2009 added support for 'should deliver_to([email1, e... [dcrec1]
README.rdoc

Email Spec

A collection of RSpec matchers and Cucumber steps to make testing emails go smoothly.

Setup

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

Gem Setup

  gem install bmabey-email_spec

  # config/environments/test.rb
  config.gem 'bmabey-email_spec', :lib => 'email_spec'

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/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:

  script/generate email_spec

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

RSpec

        First you need to require the helpers and matchers in your spec_helper.rb like so:

  require "email_spec/helpers"
  require "email_spec/matchers"

  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:

  Spec::Runner.configure do |config|
    config.include(EmailSpec::Helpers)
    config.include(EmailSpec::Matchers)
  end

  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
      When I follow "confirm" in the email
      Then I should see "Confirm your new account"

For more examples, check out spec/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 focued 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 "POST /signup (#signup)" do
        it "should deliver the signup email" do
          # expect
          UserMailer.should_receive(:deliver_signup).with("email@example.com", "Jimmy Bean")
          # when
          post :signup, "Email" => "email@example.com", "Name" => "Jimmy Bean"
        end
      end

    Examples for the #signup method in UserMailer:

    describe "Signup Email" do
      include EmailSpec::Helpers
      include EmailSpec::Matchers
      include ActionController::UrlWriter

      before(:all) do
        @email = UserMailer.create_signup("jojo@yahoo.com", "Jojo Binks")
      end

      it "should be set to be delivered to the email passed in" do
        @email.should deliver_to("jojo@yahoo.com")
      end

      it "should contain the user's message in the mail body" do
        @email.should have_text(/Jojo Binks/)
      end

      it "should contain a link to the confirmation link" do
        @email.should have_text(/#{confirm_account_url}/)
      end

      it "should have the correct subject" do
        @email.should have_subject(/Account confirmation/)
      end

    end

    ==== Using the helpers when not testing in isolation ====

      WRITE ME
      For now take a look at Email::Helpers to see what you can do.

Original Authors

Ben Mabey, Aaron Gibralter, Mischa Fierer

Please see History.txt for upcoming changsets and other contributors.