mischa-email-spec 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +4 -0
- data/MIT-LICENSE.txt +19 -0
- data/README.rdoc +56 -0
- data/Rakefile +49 -0
- data/install.rb +0 -0
- data/lib/email_spec/email_spec_feature_setup.rb +19 -0
- data/lib/email_spec/email_spec_helpers.rb +70 -0
- data/lib/email_spec/email_spec_matchers.rb +34 -0
- data/lib/email_spec/email_spec_steps.rb +46 -0
- data/lib/email_spec.rb +3 -0
- metadata +66 -0
data/History.txt
ADDED
data/MIT-LICENSE.txt
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
Copyright (c) 2007 Bryan Helmkamp, Seth Fitzsimmons
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
4
|
+
of this software and associated documentation files (the "Software"), to deal
|
5
|
+
in the Software without restriction, including without limitation the rights
|
6
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
7
|
+
copies of the Software, and to permit persons to whom the Software is
|
8
|
+
furnished to do so, subject to the following conditions:
|
9
|
+
|
10
|
+
The above copyright notice and this permission notice shall be included in
|
11
|
+
all copies or substantial portions of the Software.
|
12
|
+
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
19
|
+
THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
= Email Spec
|
2
|
+
|
3
|
+
A collection of RSpec matchers and Cucumber steps to make testing emails go smoothly.
|
4
|
+
|
5
|
+
|
6
|
+
== Setup
|
7
|
+
|
8
|
+
script/plugin install git://github.com/bmabey/email-spec.git
|
9
|
+
|
10
|
+
=== Cucumber
|
11
|
+
|
12
|
+
To use the steps in features put the following in your env.rb:
|
13
|
+
|
14
|
+
# Make sure this require is after you require cucumber/rails/world.
|
15
|
+
require 'email_spec_feature_setup'
|
16
|
+
|
17
|
+
=== RSpec
|
18
|
+
|
19
|
+
The matchers and helpers should be available for your specs by default, but you need to include them into the example groups you want to use them in. To include them in all of your specs you can do this in your spec_helper.rb:
|
20
|
+
|
21
|
+
Spec::Runner.configure do |config|
|
22
|
+
config.include(EmailSpec::Helpers)
|
23
|
+
config.include(EmailSpec::Matchers)
|
24
|
+
end
|
25
|
+
|
26
|
+
== Usage
|
27
|
+
|
28
|
+
=== Cucumber
|
29
|
+
|
30
|
+
Scenario: A new person signs up
|
31
|
+
Given I am at "/"
|
32
|
+
And a clear email queue
|
33
|
+
When I fill in "Email" with "quentin@example.com"
|
34
|
+
And I fill in "Password" with "monkey"
|
35
|
+
And I fill in "Password confirmation" with "monkey"
|
36
|
+
And I press "Sign up"
|
37
|
+
Then "quentin@example.com" should receive 1 email
|
38
|
+
And "quentin@example.com" should have 1 email
|
39
|
+
And "foo@bar.com" should not receive an email
|
40
|
+
When I open the email
|
41
|
+
Then I should see "confirm" in the email
|
42
|
+
And I should see "Account confirmation" in the subject
|
43
|
+
When I follow "confirm" in the email
|
44
|
+
Then I should see "Confirm your new account"
|
45
|
+
|
46
|
+
Check out an app that implements these features at http://github.com/bmabey/email-spec-sample-app
|
47
|
+
|
48
|
+
== TODO:
|
49
|
+
|
50
|
+
- refactor!
|
51
|
+
- provide custom matchers to give better messages
|
52
|
+
|
53
|
+
== Authors
|
54
|
+
|
55
|
+
Ben Mabey, Aaron Gibralter, Mischa Fierer
|
56
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require "rake/gempackagetask"
|
3
|
+
require 'rake/rdoctask'
|
4
|
+
require "rake/clean"
|
5
|
+
require 'spec'
|
6
|
+
require 'spec/rake/spectask'
|
7
|
+
require File.expand_path('./lib/email_spec.rb')
|
8
|
+
|
9
|
+
# Package && release
|
10
|
+
spec = Gem::Specification.new do |s|
|
11
|
+
s.name = "email-spec"
|
12
|
+
s.version = EmailSpec::VERSION
|
13
|
+
s.platform = Gem::Platform::RUBY
|
14
|
+
s.authors = ['Ben Mabrey', 'Aaron Gibralter', 'Mischa Fierer']
|
15
|
+
s.email = "f.mischa@gmail.com"
|
16
|
+
s.homepage = "http://github.com/bmabey/email-spec/"
|
17
|
+
s.summary = "Easily test email in rspec and cucumber"
|
18
|
+
s.bindir = "bin"
|
19
|
+
s.description = s.summary
|
20
|
+
s.require_path = "lib"
|
21
|
+
s.files = %w(History.txt install.rb MIT-LICENSE.txt README.rdoc Rakefile) + Dir["lib/**/*"]
|
22
|
+
# rdoc
|
23
|
+
s.has_rdoc = true
|
24
|
+
s.extra_rdoc_files = %w(README.rdoc MIT-LICENSE.txt)
|
25
|
+
end
|
26
|
+
|
27
|
+
desc 'Show information about the gem.'
|
28
|
+
task :debug_gem do
|
29
|
+
puts spec.to_ruby
|
30
|
+
end
|
31
|
+
|
32
|
+
CLEAN.include ["pkg", "*.gem", "doc", "ri", "coverage", '**/.*.sw?', '*.gem', '.config', '**/.DS_Store', '**/*.class', '**/*.jar', '**/.*.swp' ]
|
33
|
+
|
34
|
+
desc 'Install the package as a gem.'
|
35
|
+
task :install_gem => [:clean, :package] do
|
36
|
+
gem = Dir['pkg/*.gem'].first
|
37
|
+
sh "sudo gem install --local #{gem}"
|
38
|
+
end
|
39
|
+
|
40
|
+
task :gemspec do
|
41
|
+
system "rake debug_gem | grep -v \"(in \" > email-spec.gemspec"
|
42
|
+
end
|
43
|
+
|
44
|
+
task :features do
|
45
|
+
system("cd spec/rails_root; rake features; cd ../..")
|
46
|
+
end
|
47
|
+
|
48
|
+
task :default => :features
|
49
|
+
|
data/install.rb
ADDED
File without changes
|
@@ -0,0 +1,19 @@
|
|
1
|
+
# require this in your env.rb file after you require cucumber/rails/world
|
2
|
+
|
3
|
+
%w[steps].each do |file|
|
4
|
+
require File.join(File.dirname(__FILE__), "email_spec_#{file}.rb")
|
5
|
+
end
|
6
|
+
|
7
|
+
# Global Setup
|
8
|
+
ActionMailer::Base.delivery_method = :test
|
9
|
+
ActionMailer::Base.perform_deliveries = true
|
10
|
+
|
11
|
+
Before do
|
12
|
+
# Scenario setup
|
13
|
+
ActionMailer::Base.deliveries.clear
|
14
|
+
end
|
15
|
+
|
16
|
+
World do |world|
|
17
|
+
world.extend EmailSpec::Helpers
|
18
|
+
world.extend EmailSpec::Matchers
|
19
|
+
end
|
@@ -0,0 +1,70 @@
|
|
1
|
+
require 'uri'
|
2
|
+
|
3
|
+
module EmailSpec
|
4
|
+
|
5
|
+
module Helpers
|
6
|
+
|
7
|
+
def self.extended(base)
|
8
|
+
base.instance_eval do
|
9
|
+
@email_spec_hash = {}
|
10
|
+
@email_spec_hash[:read_emails] = {}
|
11
|
+
@email_spec_hash[:unread_emails] = {}
|
12
|
+
@email_spec_hash[:current_emails] = {}
|
13
|
+
@email_spec_hash[:current_email] = nil
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def reset_mailer
|
18
|
+
ActionMailer::Base.deliveries.clear
|
19
|
+
end
|
20
|
+
|
21
|
+
def open_email(email_address, opts={})
|
22
|
+
if opts[:with_subject]
|
23
|
+
email = mailbox_for(email_address).find { |m| m.subject =~ Regexp.new(opts[:with_subject]) }
|
24
|
+
elsif opts[:with_text]
|
25
|
+
email = mailbox_for(email_address).find { |m| m.body =~ Regexp.new(opts[:with_text]) }
|
26
|
+
else
|
27
|
+
email = mailbox_for(email_address).first
|
28
|
+
end
|
29
|
+
|
30
|
+
read_emails_for(email_address) << email if email
|
31
|
+
@email_spec_hash[:current_emails][email_address] = email
|
32
|
+
@email_spec_hash[:current_email] = email
|
33
|
+
end
|
34
|
+
|
35
|
+
def open_last_email
|
36
|
+
email = ActionMailer::Base.deliveries.last
|
37
|
+
read_emails_for(email.to) << email if email
|
38
|
+
@email_spec_hash[:current_emails][email.to] = email
|
39
|
+
@email_spec_hash[:current_email] = email
|
40
|
+
end
|
41
|
+
|
42
|
+
def current_email(email_address=nil)
|
43
|
+
email_address ? @email_spec_hash[:current_emails][email_address] : @email_spec_hash[:current_email]
|
44
|
+
end
|
45
|
+
|
46
|
+
def unread_emails_for(email_address)
|
47
|
+
mailbox_for(email_address) - read_emails_for(email_address)
|
48
|
+
end
|
49
|
+
|
50
|
+
def read_emails_for(email_address)
|
51
|
+
@email_spec_hash[:read_emails][email_address] ||= []
|
52
|
+
end
|
53
|
+
|
54
|
+
def mailbox_for(email)
|
55
|
+
ActionMailer::Base.deliveries.select { |m| m.to.include?(email) }
|
56
|
+
end
|
57
|
+
|
58
|
+
def parse_email_for_link(mail, link_text)
|
59
|
+
if mail.body.include?(link_text)
|
60
|
+
if link_text =~ %r{^/.*$}
|
61
|
+
# if it's an explicit link
|
62
|
+
link_text
|
63
|
+
elsif mail.body =~ %r{<a[^>]*href=['"]?([^'"]*)['"]?[^>]*?>[^<]*?#{link_text}[^<]*?</a>}
|
64
|
+
# if it's an anchor tag
|
65
|
+
URI.split($~[1])[5..-1].compact!.join("?")
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
module EmailSpec
|
2
|
+
|
3
|
+
module Matchers
|
4
|
+
|
5
|
+
class DeliverTo
|
6
|
+
|
7
|
+
def initialize(expected_email_addresses)
|
8
|
+
@expected_email_addresses = expected_email_addresses.sort
|
9
|
+
end
|
10
|
+
|
11
|
+
def matches?(email)
|
12
|
+
@email = email
|
13
|
+
@actual_recipients = (email.to || []).sort
|
14
|
+
@actual_recipients.should == @expected_email_addresses
|
15
|
+
true
|
16
|
+
end
|
17
|
+
|
18
|
+
def failure_message
|
19
|
+
"expected #{@email.inspect} to deliver to #{@expected_email_addresses.inspect}, but it delievered to #{@actual_recipients.inspect}"
|
20
|
+
end
|
21
|
+
|
22
|
+
def negative_failure_message
|
23
|
+
"expected #{@email.inspect} not to deliver to #{@expected_email_addresses.inspect}, but it did"
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def deliver_to(*expected_email_addresses_or_objects_that_respond_to_email)
|
28
|
+
emails = expected_email_addresses_or_objects_that_respond_to_email.map do |email_or_object|
|
29
|
+
email_or_object.kind_of?(String) ? email_or_object : email_or_object.email
|
30
|
+
end
|
31
|
+
DeliverTo.new(emails)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
Given /^(?:a clear email queue|no emails have been sent)$/ do
|
2
|
+
reset_mailer
|
3
|
+
end
|
4
|
+
|
5
|
+
# Action
|
6
|
+
#
|
7
|
+
When /^I open the email$/ do
|
8
|
+
open_last_email.should_not be_nil
|
9
|
+
end
|
10
|
+
|
11
|
+
When /^I follow "(.*)" in the email$/ do |link_text|
|
12
|
+
current_email.should_not be_nil
|
13
|
+
link = parse_email_for_link(current_email, link_text)
|
14
|
+
visit(link)
|
15
|
+
end
|
16
|
+
|
17
|
+
When /^I click "(.*)" in the email$/ do |link_text|
|
18
|
+
current_email.should_not be_nil
|
19
|
+
link = parse_email_for_link(current_email, link_text)
|
20
|
+
visit(link)
|
21
|
+
end
|
22
|
+
|
23
|
+
|
24
|
+
# Verification
|
25
|
+
Then /^"([^']*?)" should receive (\d+) emails?$/ do |email, n|
|
26
|
+
unread_emails_for(email).size.should == n.to_i
|
27
|
+
end
|
28
|
+
|
29
|
+
Then /^"([^']*?)" should have (\d+) emails?$/ do |email, n|
|
30
|
+
mailbox_for(email).size.should == n.to_i
|
31
|
+
end
|
32
|
+
|
33
|
+
Then /^"([^']*?)" should not receive an email$/ do |email|
|
34
|
+
open_email(email).should be_nil
|
35
|
+
end
|
36
|
+
|
37
|
+
Then /^I should see "(.*)" in the subject$/ do |text|
|
38
|
+
raise ArgumentError, "To check the subject, you must first open an e-mail" if current_email.nil?
|
39
|
+
current_email.should_not be_nil
|
40
|
+
current_email.subject.should =~ Regexp.new(text)
|
41
|
+
end
|
42
|
+
|
43
|
+
Then /^I should see "(.*)" in the email$/ do |text|
|
44
|
+
current_email.should_not be_nil
|
45
|
+
current_email.body.should =~ Regexp.new(text)
|
46
|
+
end
|
data/lib/email_spec.rb
ADDED
metadata
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mischa-email-spec
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Ben Mabrey
|
8
|
+
- Aaron Gibralter
|
9
|
+
- Mischa Fierer
|
10
|
+
autorequire:
|
11
|
+
bindir: bin
|
12
|
+
cert_chain: []
|
13
|
+
|
14
|
+
date: 2008-12-18 00:00:00 -08:00
|
15
|
+
default_executable:
|
16
|
+
dependencies: []
|
17
|
+
|
18
|
+
description: Easily test email in rspec and cucumber
|
19
|
+
email: f.mischa@gmail.com
|
20
|
+
executables: []
|
21
|
+
|
22
|
+
extensions: []
|
23
|
+
|
24
|
+
extra_rdoc_files:
|
25
|
+
- README.rdoc
|
26
|
+
- MIT-LICENSE.txt
|
27
|
+
files:
|
28
|
+
- History.txt
|
29
|
+
- install.rb
|
30
|
+
- MIT-LICENSE.txt
|
31
|
+
- README.rdoc
|
32
|
+
- Rakefile
|
33
|
+
- lib/email_spec
|
34
|
+
- lib/email_spec/email_spec_feature_setup.rb
|
35
|
+
- lib/email_spec/email_spec_helpers.rb
|
36
|
+
- lib/email_spec/email_spec_matchers.rb
|
37
|
+
- lib/email_spec/email_spec_steps.rb
|
38
|
+
- lib/email_spec.rb
|
39
|
+
has_rdoc: true
|
40
|
+
homepage: http://github.com/bmabey/email-spec/
|
41
|
+
post_install_message:
|
42
|
+
rdoc_options: []
|
43
|
+
|
44
|
+
require_paths:
|
45
|
+
- lib
|
46
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
47
|
+
requirements:
|
48
|
+
- - ">="
|
49
|
+
- !ruby/object:Gem::Version
|
50
|
+
version: "0"
|
51
|
+
version:
|
52
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
53
|
+
requirements:
|
54
|
+
- - ">="
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: "0"
|
57
|
+
version:
|
58
|
+
requirements: []
|
59
|
+
|
60
|
+
rubyforge_project:
|
61
|
+
rubygems_version: 1.2.0
|
62
|
+
signing_key:
|
63
|
+
specification_version: 2
|
64
|
+
summary: Easily test email in rspec and cucumber
|
65
|
+
test_files: []
|
66
|
+
|