glennr-email_spec 0.3.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (99) hide show
  1. data/History.txt +135 -0
  2. data/MIT-LICENSE.txt +19 -0
  3. data/README.rdoc +125 -0
  4. data/Rakefile +75 -0
  5. data/examples/rails_root/Rakefile +13 -0
  6. data/examples/rails_root/app/controllers/application_controller.rb +15 -0
  7. data/examples/rails_root/app/controllers/welcome_controller.rb +12 -0
  8. data/examples/rails_root/app/helpers/application_helper.rb +3 -0
  9. data/examples/rails_root/app/helpers/welcome_helper.rb +2 -0
  10. data/examples/rails_root/app/models/user.rb +2 -0
  11. data/examples/rails_root/app/models/user_mailer.rb +18 -0
  12. data/examples/rails_root/app/views/user_mailer/newsletter.erb +8 -0
  13. data/examples/rails_root/app/views/user_mailer/signup.erb +3 -0
  14. data/examples/rails_root/app/views/welcome/confirm.html.erb +1 -0
  15. data/examples/rails_root/app/views/welcome/index.html.erb +8 -0
  16. data/examples/rails_root/app/views/welcome/newsletter.html.erb +0 -0
  17. data/examples/rails_root/app/views/welcome/signup.html.erb +1 -0
  18. data/examples/rails_root/config/boot.rb +110 -0
  19. data/examples/rails_root/config/database.yml +22 -0
  20. data/examples/rails_root/config/environment.rb +22 -0
  21. data/examples/rails_root/config/environments/development.rb +17 -0
  22. data/examples/rails_root/config/environments/production.rb +24 -0
  23. data/examples/rails_root/config/environments/test.rb +30 -0
  24. data/examples/rails_root/config/initializers/inflections.rb +10 -0
  25. data/examples/rails_root/config/initializers/mime_types.rb +5 -0
  26. data/examples/rails_root/config/initializers/new_rails_defaults.rb +17 -0
  27. data/examples/rails_root/config/routes.rb +44 -0
  28. data/examples/rails_root/db/migrate/20090125013728_create_users.rb +11 -0
  29. data/examples/rails_root/db/migrate/20090908054656_create_delayed_jobs.rb +20 -0
  30. data/examples/rails_root/db/schema.rb +32 -0
  31. data/examples/rails_root/doc/README_FOR_APP +5 -0
  32. data/examples/rails_root/features/delayed_job.feature +13 -0
  33. data/examples/rails_root/features/errors.feature +32 -0
  34. data/examples/rails_root/features/example.feature +62 -0
  35. data/examples/rails_root/features/step_definitions/user_steps.rb +26 -0
  36. data/examples/rails_root/features/step_definitions/webrat_steps.rb +137 -0
  37. data/examples/rails_root/features/support/env.rb +32 -0
  38. data/examples/rails_root/features/support/paths.rb +24 -0
  39. data/examples/rails_root/public/404.html +30 -0
  40. data/examples/rails_root/public/422.html +30 -0
  41. data/examples/rails_root/public/500.html +33 -0
  42. data/examples/rails_root/public/dispatch.rb +10 -0
  43. data/examples/rails_root/public/favicon.ico +0 -0
  44. data/examples/rails_root/public/images/rails.png +0 -0
  45. data/examples/rails_root/public/javascripts/application.js +2 -0
  46. data/examples/rails_root/public/javascripts/controls.js +963 -0
  47. data/examples/rails_root/public/javascripts/dragdrop.js +973 -0
  48. data/examples/rails_root/public/javascripts/effects.js +1128 -0
  49. data/examples/rails_root/public/javascripts/prototype.js +4320 -0
  50. data/examples/rails_root/public/robots.txt +5 -0
  51. data/examples/rails_root/script/about +4 -0
  52. data/examples/rails_root/script/autospec +5 -0
  53. data/examples/rails_root/script/console +3 -0
  54. data/examples/rails_root/script/cucumber +9 -0
  55. data/examples/rails_root/script/dbconsole +3 -0
  56. data/examples/rails_root/script/delayed_job +5 -0
  57. data/examples/rails_root/script/destroy +3 -0
  58. data/examples/rails_root/script/generate +3 -0
  59. data/examples/rails_root/script/performance/benchmarker +3 -0
  60. data/examples/rails_root/script/performance/profiler +3 -0
  61. data/examples/rails_root/script/performance/request +3 -0
  62. data/examples/rails_root/script/plugin +3 -0
  63. data/examples/rails_root/script/process/inspector +3 -0
  64. data/examples/rails_root/script/process/reaper +3 -0
  65. data/examples/rails_root/script/process/spawner +3 -0
  66. data/examples/rails_root/script/runner +3 -0
  67. data/examples/rails_root/script/server +3 -0
  68. data/examples/rails_root/script/spec +5 -0
  69. data/examples/rails_root/script/spec_server +125 -0
  70. data/examples/rails_root/spec/controllers/welcome_controller_spec.rb +15 -0
  71. data/examples/rails_root/spec/model_factory.rb +6 -0
  72. data/examples/rails_root/spec/models/user_mailer_spec.rb +58 -0
  73. data/examples/rails_root/spec/models/user_spec.rb +5 -0
  74. data/examples/rails_root/spec/rcov.opts +2 -0
  75. data/examples/rails_root/spec/spec.opts +4 -0
  76. data/examples/rails_root/spec/spec_helper.rb +51 -0
  77. data/examples/sinatra/app.rb +33 -0
  78. data/examples/sinatra/features/errors.feature +32 -0
  79. data/examples/sinatra/features/example.feature +62 -0
  80. data/examples/sinatra/features/step_definitions/user_steps.rb +26 -0
  81. data/examples/sinatra/features/step_definitions/webrat_steps.rb +137 -0
  82. data/examples/sinatra/features/support/env.rb +33 -0
  83. data/examples/sinatra/features/support/paths.rb +24 -0
  84. data/install.rb +0 -0
  85. data/lib/email-spec.rb +1 -0
  86. data/lib/email_spec.rb +15 -0
  87. data/lib/email_spec/address_converter.rb +29 -0
  88. data/lib/email_spec/background_processes.rb +31 -0
  89. data/lib/email_spec/cucumber.rb +21 -0
  90. data/lib/email_spec/deliveries.rb +71 -0
  91. data/lib/email_spec/email_viewer.rb +70 -0
  92. data/lib/email_spec/helpers.rb +149 -0
  93. data/lib/email_spec/matchers.rb +186 -0
  94. data/rails_generators/email_spec/email_spec_generator.rb +17 -0
  95. data/rails_generators/email_spec/templates/email_steps.rb +122 -0
  96. data/spec/email_spec/matchers_spec.rb +213 -0
  97. data/spec/spec.opts +4 -0
  98. data/spec/spec_helper.rb +4 -0
  99. metadata +154 -0
@@ -0,0 +1,70 @@
1
+ module EmailSpec
2
+ class EmailViewer
3
+ extend Deliveries
4
+
5
+ def self.save_and_open_all_raw_emails
6
+ filename = "#{RAILS_ROOT}/tmp/email-#{Time.now.to_i}.txt"
7
+
8
+ File.open(filename, "w") do |f|
9
+ all_emails.each do |m|
10
+ f.write m.to_s
11
+ f.write "\n" + '='*80 + "\n"
12
+ end
13
+ end
14
+
15
+ open_in_text_editor(filename)
16
+ end
17
+
18
+ def self.save_and_open_all_html_emails
19
+ all_emails.each_with_index do |m, index|
20
+ if m.multipart? && html_part = m.parts.detect{ |p| p.content_type == 'text/html' }
21
+ filename = tmp_email_filename("-#{index}.html")
22
+ File.open(filename, "w") do |f|
23
+ f.write m.parts[1].body
24
+ end
25
+ open_in_browser(filename)
26
+ end
27
+ end
28
+ end
29
+
30
+ def self.save_and_open_all_text_emails
31
+ filename = tmp_email_filename
32
+
33
+ File.open(filename, "w") do |f|
34
+ all_emails.each do |m|
35
+ if m.multipart? && text_part = m.parts.detect{ |p| p.content_type == 'text/plain' }
36
+ m.ordered_each{|k,v| f.write "#{k}: #{v}\n" }
37
+ f.write text_part.body
38
+ else
39
+ f.write m.to_s
40
+ end
41
+ f.write "\n" + '='*80 + "\n"
42
+ end
43
+ end
44
+
45
+ open_in_text_editor(filename)
46
+ end
47
+
48
+ def self.save_and_open_email(mail)
49
+ filename = "#{RAILS_ROOT}/tmp/email-#{Time.now.to_i}.txt"
50
+
51
+ File.open(filename, "w") do |f|
52
+ f.write mail.to_s
53
+ end
54
+
55
+ open_in_text_editor(filename)
56
+ end
57
+
58
+ def self.open_in_text_editor(filename)
59
+ `mate #{filename}`
60
+ end
61
+
62
+ def self.open_in_browser(filename)
63
+ `open #{filename}`
64
+ end
65
+
66
+ def self.tmp_email_filename(extension = '.txt')
67
+ "#{RAILS_ROOT}/tmp/email-#{Time.now.to_i}#{extension}"
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,149 @@
1
+ require 'uri'
2
+ require 'email_spec/deliveries'
3
+
4
+ module EmailSpec
5
+
6
+ module Helpers
7
+ include Deliveries
8
+
9
+ def visit_in_email(link_text)
10
+ visit(parse_email_for_link(current_email, link_text))
11
+ end
12
+
13
+ def click_email_link_matching(regex, email = current_email)
14
+ url = links_in_email(email).detect { |link| link =~ regex }
15
+ raise "No link found matching #{regex.inspect} in #{email.body}" unless url
16
+ request_uri = URI::parse(url).request_uri
17
+ visit request_uri
18
+ end
19
+
20
+ def click_first_link_in_email(email = current_email)
21
+ link = links_in_email(email).first
22
+ request_uri = URI::parse(link).request_uri
23
+ visit request_uri
24
+ end
25
+
26
+ def open_email(address, opts={})
27
+ set_current_email(find_email!(address, opts))
28
+ end
29
+
30
+ alias_method :open_email_for, :open_email
31
+
32
+ def open_last_email
33
+ set_current_email(last_email_sent)
34
+ end
35
+
36
+ def open_last_email_for(address)
37
+ set_current_email(mailbox_for(address).last)
38
+ end
39
+
40
+ def current_email(address=nil)
41
+ address = convert_address(address)
42
+ email = address ? email_spec_hash[:current_emails][address] : email_spec_hash[:current_email]
43
+ raise Spec::Expectations::ExpectationNotMetError, "Expected an open email but none was found. Did you forget to call open_email?" unless email
44
+ email
45
+ end
46
+
47
+ def unread_emails_for(address)
48
+ mailbox_for(address) - read_emails_for(address)
49
+ end
50
+
51
+ def read_emails_for(address)
52
+ email_spec_hash[:read_emails][convert_address(address)] ||= []
53
+ end
54
+
55
+ def find_email(address, opts={})
56
+ address = convert_address(address)
57
+ if opts[:with_subject]
58
+ mailbox_for(address).find { |m| m.subject =~ Regexp.new(opts[:with_subject]) }
59
+ elsif opts[:with_text]
60
+ mailbox_for(address).find { |m| m.body =~ Regexp.new(opts[:with_text]) }
61
+ else
62
+ mailbox_for(address).first
63
+ end
64
+ end
65
+
66
+ def links_in_email(email)
67
+ URI.extract(email.body, ['http', 'https'])
68
+ end
69
+
70
+ private
71
+
72
+ def email_spec_hash
73
+ @email_spec_hash ||= {:read_emails => {}, :unread_emails => {}, :current_emails => {}, :current_email => nil}
74
+ end
75
+
76
+ def find_email!(address, opts={})
77
+ email = find_email(address, opts)
78
+ if email.nil?
79
+ error = "#{opts.keys.first.to_s.humanize unless opts.empty?} #{('"' + opts.values.first.to_s.humanize + '"') unless opts.empty?}"
80
+ raise Spec::Expectations::ExpectationNotMetError, "Could not find email #{error}. \n Found the following emails:\n\n #{all_emails.to_s}"
81
+ end
82
+ email
83
+ end
84
+
85
+ def set_current_email(email)
86
+ return unless email
87
+ email.to.each do |to|
88
+ read_emails_for(to) << email
89
+ email_spec_hash[:current_emails][to] = email
90
+ end
91
+ email_spec_hash[:current_email] = email
92
+ end
93
+
94
+ def parse_email_for_link(email, text_or_regex)
95
+ email.should have_body_text(text_or_regex)
96
+
97
+ url = parse_email_for_explicit_link(email, text_or_regex)
98
+ url ||= parse_email_for_anchor_text_link(email, text_or_regex)
99
+
100
+ raise "No link found matching #{text_or_regex.inspect} in #{email}" unless url
101
+ url
102
+ end
103
+
104
+ # e.g. confirm in http://confirm
105
+ def parse_email_for_explicit_link(email, regex)
106
+ regex = /#{Regexp.escape(regex)}/ unless regex.is_a?(Regexp)
107
+ url = links_in_email(email).detect { |link| link =~ regex }
108
+ URI::parse(url).request_uri if url
109
+ end
110
+
111
+ # e.g. Click here in <a href="http://confirm">Click here</a>
112
+ def parse_email_for_anchor_text_link(email, link_text)
113
+ email.body =~ %r{<a[^>]*href=['"]?([^'"]*)['"]?[^>]*?>[^<]*?#{link_text}[^<]*?</a>}
114
+ URI.split($~[1])[5..-1].compact!.join("?").gsub("&amp;", "&")
115
+ # sub correct ampersand after rails switches it (http://dev.rubyonrails.org/ticket/4002)
116
+ end
117
+
118
+ def parse_email_count(amount)
119
+ case amount
120
+ when "no"
121
+ 0
122
+ when "an"
123
+ 1
124
+ else
125
+ amount.to_i
126
+ end
127
+ end
128
+
129
+ attr_reader :last_email_address
130
+
131
+ def convert_address(address)
132
+ @last_email_address = (address || current_email_address)
133
+ AddressConverter.instance.convert(@last_email_address)
134
+ end
135
+
136
+
137
+ def mailbox_for(address)
138
+ super(convert_address(address)) # super resides in Deliveries
139
+ end
140
+
141
+ def email_spec_deprecate(text)
142
+ puts ""
143
+ puts "DEPRECATION: #{text.split.join(' ')}"
144
+ puts ""
145
+ end
146
+
147
+ end
148
+ end
149
+
@@ -0,0 +1,186 @@
1
+ module EmailSpec
2
+
3
+ module Matchers
4
+
5
+ class DeliverTo
6
+
7
+ def initialize(expected_email_addresses_or_objects_that_respond_to_email)
8
+ emails = expected_email_addresses_or_objects_that_respond_to_email.map do |email_or_object|
9
+ email_or_object.kind_of?(String) ? email_or_object : email_or_object.email
10
+ end
11
+
12
+ @expected_email_addresses = emails.sort
13
+ end
14
+
15
+ def description
16
+ "be delivered to #{@expected_email_addresses.inspect}"
17
+ end
18
+
19
+ def matches?(email)
20
+ @email = email
21
+ @actual_recipients = (email.to || []).sort
22
+ @actual_recipients == @expected_email_addresses
23
+ end
24
+
25
+ def failure_message
26
+ "expected #{@email.inspect} to deliver to #{@expected_email_addresses.inspect}, but it delivered to #{@actual_recipients.inspect}"
27
+ end
28
+
29
+ def negative_failure_message
30
+ "expected #{@email.inspect} not to deliver to #{@expected_email_addresses.inspect}, but it did"
31
+ end
32
+ end
33
+
34
+ def deliver_to(*expected_email_addresses_or_objects_that_respond_to_email)
35
+ DeliverTo.new(expected_email_addresses_or_objects_that_respond_to_email.flatten)
36
+ end
37
+
38
+ alias :be_delivered_to :deliver_to
39
+
40
+ class DeliverFrom
41
+
42
+ def initialize(email)
43
+ @expected_email_addresses = email
44
+ end
45
+
46
+ def description
47
+ "be delivered from #{@expected_email_addresses.inspect}"
48
+ end
49
+
50
+ def matches?(email)
51
+ @email = email
52
+ @actual_sender = (email.from || []).first
53
+ @actual_sender.eql? @expected_email_addresses
54
+ end
55
+
56
+ def failure_message
57
+ "expected #{@email.inspect} to deliver from #{@expected_email_addresses.inspect}, but it delivered from #{@actual_sender.inspect}"
58
+ end
59
+
60
+ def negative_failure_message
61
+ "expected #{@email.inspect} not to deliver from #{@expected_email_addresses.inspect}, but it did"
62
+ end
63
+ end
64
+
65
+ def deliver_from(email)
66
+ DeliverFrom.new(email)
67
+ end
68
+
69
+ alias :be_delivered_from :deliver_from
70
+
71
+ class BccTo
72
+
73
+ def initialize(expected_email_addresses_or_objects_that_respond_to_email)
74
+ emails = expected_email_addresses_or_objects_that_respond_to_email.map do |email_or_object|
75
+ email_or_object.kind_of?(String) ? email_or_object : email_or_object.email
76
+ end
77
+
78
+ @expected_email_addresses = emails.sort
79
+ end
80
+
81
+ def description
82
+ "be bcc'd to #{@expected_email_addresses.inspect}"
83
+ end
84
+
85
+ def matches?(email)
86
+ @email = email
87
+ @actual_recipients = (email.bcc || []).sort
88
+ @actual_recipients == @expected_email_addresses
89
+ end
90
+
91
+ def failure_message
92
+ "expected #{@email.inspect} to bcc to #{@expected_email_addresses.inspect}, but it was bcc'd to #{@actual_recipients.inspect}"
93
+ end
94
+
95
+ def negative_failure_message
96
+ "expected #{@email.inspect} not to bcc to #{@expected_email_addresses.inspect}, but it did"
97
+ end
98
+ end
99
+
100
+ def bcc_to(*expected_email_addresses_or_objects_that_respond_to_email)
101
+ BccTo.new(expected_email_addresses_or_objects_that_respond_to_email.flatten)
102
+ end
103
+
104
+ def have_subject(expected)
105
+ simple_matcher do |given, matcher|
106
+ given_subject = given.subject
107
+
108
+ if expected.is_a?(String)
109
+ matcher.description = "have subject of #{expected.inspect}"
110
+ matcher.failure_message = "expected the subject to be #{expected.inspect} but was #{given_subject.inspect}"
111
+ matcher.negative_failure_message = "expected the subject not to be #{expected.inspect} but was"
112
+
113
+ given_subject == expected
114
+ else
115
+ matcher.description = "have subject matching #{expected.inspect}"
116
+ matcher.failure_message = "expected the subject to match #{expected.inspect}, but did not. Actual subject was: #{given_subject.inspect}"
117
+ matcher.negative_failure_message = "expected the subject not to match #{expected.inspect} but #{given_subject.inspect} does match it."
118
+
119
+ !!(given_subject =~ expected)
120
+ end
121
+ end
122
+ end
123
+
124
+ def include_email_with_subject(expected)
125
+ simple_matcher do |given_emails, matcher|
126
+
127
+ if expected.is_a?(String)
128
+ matcher.description = "include email with subject of #{expected.inspect}"
129
+ matcher.failure_message = "expected at least one email to have the subject #{expected.inspect} but none did. Subjects were #{given_emails.map(&:subject).inspect}"
130
+ matcher.negative_failure_message = "expected no email with the subject #{expected.inspect} but found at least one. Subjects were #{given_emails.map(&:subject).inspect}"
131
+
132
+ given_emails.map(&:subject).include?(expected)
133
+ else
134
+ matcher.description = "include email with subject matching #{expected.inspect}"
135
+ matcher.failure_message = "expected at least one email to have a subject matching #{expected.inspect}, but none did. Subjects were #{given_emails.map(&:subject).inspect}"
136
+ matcher.negative_failure_message = "expected no email to have a subject matching #{expected.inspect} but found at least one. Subjects were #{given_emails.map(&:subject).inspect}"
137
+
138
+ !!(given_emails.any?{ |mail| mail.subject =~ expected })
139
+ end
140
+ end
141
+ end
142
+
143
+ def have_body_text(expected)
144
+ simple_matcher do |given, matcher|
145
+
146
+ if expected.is_a?(String)
147
+ normalized_body = given.body.gsub(/\s+/, " ")
148
+ normalized_expected = expected.gsub(/\s+/, " ")
149
+ matcher.description = "have body including #{normalized_expected.inspect}"
150
+ matcher.failure_message = "expected the body to contain #{normalized_expected.inspect} but was #{normalized_body.inspect}"
151
+ matcher.negative_failure_message = "expected the body not to contain #{normalized_expected.inspect} but was #{normalized_body.inspect}"
152
+
153
+ normalized_body.include?(normalized_expected)
154
+ else
155
+ given_body = given.body
156
+ matcher.description = "have body matching #{expected.inspect}"
157
+ matcher.failure_message = "expected the body to match #{expected.inspect}, but did not. Actual body was: #{given_body.inspect}"
158
+ matcher.negative_failure_message = "expected the body not to match #{expected.inspect} but #{given_body.inspect} does match it."
159
+
160
+ !!(given_body =~ expected)
161
+ end
162
+ end
163
+ end
164
+
165
+ def have_header(expected_name, expected_value)
166
+ simple_matcher do |given, matcher|
167
+ given_header = given.header
168
+
169
+ if expected_value.is_a?(String)
170
+ matcher.description = "have header #{expected_name}: #{expected_value}"
171
+ matcher.failure_message = "expected the headers to include '#{expected_name}: #{expected_value}' but they were #{given_header.inspect}"
172
+ matcher.negative_failure_message = "expected the headers not to include '#{expected_name}: #{expected_value}' but they were #{given_header.inspect}"
173
+
174
+ given_header[expected_name].to_s == expected_value
175
+ else
176
+ matcher.description = "have header #{expected_name} with value matching #{expected_value.inspect}"
177
+ matcher.failure_message = "expected the headers to include '#{expected_name}' with a value matching #{expected_value.inspect} but they were #{given_header.inspect}"
178
+ matcher.negative_failure_message = "expected the headers not to include '#{expected_name}' with a value matching #{expected_value.inspect} but they were #{given_header.inspect}"
179
+
180
+ given_header[expected_name].to_s =~ expected_value
181
+ end
182
+ end
183
+ end
184
+
185
+ end
186
+ end
@@ -0,0 +1,17 @@
1
+ # This generator adds email steps to the step definitions directory
2
+ generator_base = defined?(Rails) ? Rails::Generator::Base : RubiGen::Base
3
+ class EmailSpecGenerator < generator_base
4
+ def manifest
5
+ record do |m|
6
+ m.directory 'features/step_definitions'
7
+ m.file 'email_steps.rb', 'features/step_definitions/email_steps.rb'
8
+ end
9
+ end
10
+
11
+ protected
12
+
13
+ def banner
14
+ "Usage: #{$0} email_spec"
15
+ end
16
+
17
+ end
@@ -0,0 +1,122 @@
1
+ # Commonly used email steps
2
+ #
3
+ # To add your own steps make a custom_email_steps.rb
4
+ # The provided methods are:
5
+ #
6
+ # last_email_address
7
+ # reset_mailer
8
+ # open_last_email
9
+ # visit_in_email
10
+ # unread_emails_for
11
+ # mailbox_for
12
+ # current_email
13
+ # open_email
14
+ # read_emails_for
15
+ # find_email
16
+ #
17
+ # General form for email scenarios are:
18
+ # - clear the email queue (done automatically by email_spec)
19
+ # - execute steps that sends an email
20
+ # - check the user received an/no/[0-9] emails
21
+ # - open the email
22
+ # - inspect the email contents
23
+ # - interact with the email (e.g. click links)
24
+ #
25
+ # The Cucumber steps below are setup in this order.
26
+
27
+ module EmailHelpers
28
+ def current_email_address
29
+ # Replace with your a way to find your current email. e.g @current_user.email
30
+ # last_email_address will return the last email address used by email spec to find an email.
31
+ # Note that last_email_address will be reset after each Scenario.
32
+ last_email_address || "example@example.com"
33
+ end
34
+ end
35
+
36
+ World(EmailHelpers)
37
+
38
+ #
39
+ # Reset the e-mail queue within a scenario.
40
+ # This is done automatically before each scenario.
41
+ #
42
+
43
+ Given /^(?:a clear email queue|no emails have been sent)$/ do
44
+ reset_mailer
45
+ end
46
+
47
+ #
48
+ # Check how many emails have been sent/received
49
+ #
50
+
51
+ Then /^(?:I|they|"([^"]*?)") should receive (an|no|\d+) emails?$/ do |address, amount|
52
+ unread_emails_for(address).size.should == parse_email_count(amount)
53
+ end
54
+
55
+ Then /^(?:I|they|"([^"]*?)") should have (an|no|\d+) emails?$/ do |address, amount|
56
+ mailbox_for(address).size.should == parse_email_count(amount)
57
+ end
58
+
59
+ # DEPRECATED
60
+ # The following methods are left in for backwards compatibility and
61
+ # should be removed by version 0.4.0
62
+ Then /^(?:I|they|"([^"]*?)") should not receive an email$/ do |address|
63
+ email_spec_deprecate "The step 'I/they/[email] should not receive an email' is no longer supported.
64
+ Please use 'I/they/[email] should receive no emails' instead."
65
+ unread_emails_for(address).size.should == 0
66
+ end
67
+
68
+ #
69
+ # Accessing emails
70
+ #
71
+
72
+ # Opens the most recently received email
73
+ When /^(?:I|they|"([^"]*?)") opens? the email$/ do |address|
74
+ open_email(address)
75
+ end
76
+
77
+ When /^(?:I|they|"([^"]*?)") opens? the email with subject "([^"]*?)"$/ do |address, subject|
78
+ open_email(address, :with_subject => subject)
79
+ end
80
+
81
+ When /^(?:I|they|"([^"]*?)") opens? the email with text "([^"]*?)"$/ do |address, text|
82
+ open_email(address, :with_text => text)
83
+ end
84
+
85
+ #
86
+ # Inspect the Email Contents
87
+ #
88
+
89
+ Then /^(?:I|they) should see "([^"]*?)" in the email subject$/ do |text|
90
+ current_email.should have_subject(Regexp.new(text))
91
+ end
92
+
93
+ Then /^(?:I|they) should see "([^"]*?)" in the email body$/ do |text|
94
+ current_email.body.should =~ Regexp.new(text)
95
+ end
96
+
97
+ # DEPRECATED
98
+ # The following methods are left in for backwards compatibility and
99
+ # should be removed by version 0.4.0.
100
+ Then /^(?:I|they) should see "([^"]*?)" in the subject$/ do |text|
101
+ email_spec_deprecate "The step 'I/they should see [text] in the subject' is no longer supported.
102
+ Please use 'I/they should see [text] in the email subject' instead."
103
+ current_email.should have_subject(Regexp.new(text))
104
+ end
105
+ Then /^(?:I|they) should see "([^"]*?)" in the email$/ do |text|
106
+ email_spec_deprecate "The step 'I/they should see [text] in the email' is no longer supported.
107
+ Please use 'I/they should see [text] in the email body' instead."
108
+ current_email.body.should =~ Regexp.new(text)
109
+ end
110
+
111
+ #
112
+ # Interact with Email Contents
113
+ #
114
+
115
+ When /^(?:I|they) follow "([^"]*?)" in the email$/ do |link|
116
+ visit_in_email(link)
117
+ end
118
+
119
+ When /^(?:I|they) click the first link in the email$/ do
120
+ click_first_link_in_email
121
+ end
122
+