GithubHelp home page GithubHelp logo

benhutchison / email-spec Goto Github PK

View Code? Open in Web Editor NEW

This project forked from email-spec/email-spec

0.0 2.0 0.0 873 KB

Collection of rspec matchers and cucumber steps for testing email in a ruby app using ActiveMailer or Pony

Home Page: http://gemcutter.org/gems/email_spec

License: MIT License

JavaScript 3.54% Ruby 96.46%

email-spec's Introduction

Email Spec

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

This library works with ActionMailer and Pony. When using it with ActionMailer it works with DelayedJob, ActiveRecord Mailer, and action_mailer_cache_delivery.

When using the action_mailer_cache_delivery library you must use this fork: github.com/liangzan/action_mailer_cache_delivery

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

gem install email_spec

# Gemfile
group :test do
  gem '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:

rails generate email_spec:steps

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

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:

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 "[email protected]"
    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 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 protected]", "Jimmy Bean")
    # when
    post :signup, "Email" => "[email protected]", "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("[email protected]", "Jojo Binks")
  end

  it "should be set to be delivered to the email passed in" do
    @email.should deliver_to("[email protected]")
  end

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

  it "should contain a link to the confirmation link" do
    @email.should have_body_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

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

Original Authors

Ben Mabey, Aaron Gibralter, Mischa Fierer

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.