email_spec 0.6.5 → 0.6.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -3,6 +3,9 @@ This is the Rails 2 compatible branch/gem.
3
3
 
4
4
  == 0.6.x (git)
5
5
 
6
+ == 0.6.6
7
+ * Backports "Support cache method in reset_mailer" (Rob Olson)
8
+
6
9
  == 0.6.5
7
10
  * Fixes gemspec by adding RSpec as a dependency. (Ben Hutchison)
8
11
 
data/Rakefile CHANGED
@@ -20,9 +20,10 @@ begin
20
20
  s.has_rdoc = true
21
21
  s.extra_rdoc_files = %w(README.rdoc MIT-LICENSE.txt)
22
22
  s.rubyforge_project = 'email-spec'
23
+ s.add_dependency 'rspec'
23
24
  end
24
25
  rescue LoadError
25
- puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
26
+ puts "Jeweler not available. Install it with: sudo gem install jeweler"
26
27
  end
27
28
 
28
29
  # TODO: switch to gem bundler
@@ -0,0 +1,194 @@
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
+ Then /^(?:I|they|"([^"]*?)") should receive (an|no|\d+) emails? with subject "([^"]*?)"$/ do |address, amount, subject|
60
+ unread_emails_for(address).select { |m| m.subject =~ Regexp.new(subject) }.size.should == parse_email_count(amount)
61
+ end
62
+
63
+ Then /^(?:I|they|"([^"]*?)") should receive an email with the following body:$/ do |address, expected_body|
64
+ open_email(address, :with_text => expected_body)
65
+ end
66
+
67
+ #
68
+ # Accessing emails
69
+ #
70
+
71
+ # Opens the most recently received email
72
+ When /^(?:I|they|"([^"]*?)") opens? the email$/ do |address|
73
+ open_email(address)
74
+ end
75
+
76
+ When /^(?:I|they|"([^"]*?)") opens? the email with subject "([^"]*?)"$/ do |address, subject|
77
+ open_email(address, :with_subject => subject)
78
+ end
79
+
80
+ When /^(?:I|they|"([^"]*?)") opens? the email with text "([^"]*?)"$/ do |address, text|
81
+ open_email(address, :with_text => text)
82
+ end
83
+
84
+ #
85
+ # Inspect the Email Contents
86
+ #
87
+
88
+ Then /^(?:I|they) should see "([^"]*?)" in the email subject$/ do |text|
89
+ current_email.should have_subject(text)
90
+ end
91
+
92
+ Then /^(?:I|they) should see \/([^"]*?)\/ in the email subject$/ do |text|
93
+ current_email.should have_subject(Regexp.new(text))
94
+ end
95
+
96
+ Then /^(?:I|they) should see "([^"]*?)" in the email body$/ do |text|
97
+ current_email.default_part_body.to_s.should include(text)
98
+ end
99
+
100
+ Then /^(?:I|they) should see \/([^"]*?)\/ in the email body$/ do |text|
101
+ current_email.default_part_body.to_s.should =~ Regexp.new(text)
102
+ end
103
+
104
+ Then /^(?:I|they) should see the email delivered from "([^"]*?)"$/ do |text|
105
+ current_email.should be_delivered_from(text)
106
+ end
107
+
108
+ Then /^(?:I|they) should see "([^\"]*)" in the email "([^"]*?)" header$/ do |text, name|
109
+ current_email.should have_header(name, text)
110
+ end
111
+
112
+ Then /^(?:I|they) should see \/([^\"]*)\/ in the email "([^"]*?)" header$/ do |text, name|
113
+ current_email.should have_header(name, Regexp.new(text))
114
+ end
115
+
116
+ Then /^I should see it is a multi\-part email$/ do
117
+ current_email.should be_multipart
118
+ end
119
+
120
+ Then /^(?:I|they) should see "([^"]*?)" in the email html part body$/ do |text|
121
+ current_email.html_part.body.to_s.should include(text)
122
+ end
123
+
124
+ Then /^(?:I|they) should see "([^"]*?)" in the email text part body$/ do |text|
125
+ current_email.text_part.body.to_s.should include(text)
126
+ end
127
+
128
+ #
129
+ # Inspect the Email Attachments
130
+ #
131
+
132
+ Then /^(?:I|they) should see (an|no|\d+) attachments? with the email$/ do |amount|
133
+ current_email_attachments.size.should == parse_email_count(amount)
134
+ end
135
+
136
+ Then /^there should be (an|no|\d+) attachments? named "([^"]*?)"$/ do |amount, filename|
137
+ current_email_attachments.select { |a| a.filename == filename }.size.should == parse_email_count(amount)
138
+ end
139
+
140
+ Then /^attachment (\d+) should be named "([^"]*?)"$/ do |index, filename|
141
+ current_email_attachments[(index.to_i - 1)].filename.should == filename
142
+ end
143
+
144
+ Then /^there should be (an|no|\d+) attachments? of type "([^"]*?)"$/ do |amount, content_type|
145
+ current_email_attachments.select { |a| a.content_type.include?(content_type) }.size.should == parse_email_count(amount)
146
+ end
147
+
148
+ Then /^attachment (\d+) should be of type "([^"]*?)"$/ do |index, content_type|
149
+ current_email_attachments[(index.to_i - 1)].content_type.should include(content_type)
150
+ end
151
+
152
+ Then /^all attachments should not be blank$/ do
153
+ current_email_attachments.each do |attachment|
154
+ attachment.read.size.should_not == 0
155
+ end
156
+ end
157
+
158
+ Then /^show me a list of email attachments$/ do
159
+ EmailSpec::EmailViewer::save_and_open_email_attachments_list(current_email)
160
+ end
161
+
162
+ #
163
+ # Interact with Email Contents
164
+ #
165
+
166
+ When /^(?:I|they) follow "([^"]*?)" in the email$/ do |link|
167
+ visit_in_email(link)
168
+ end
169
+
170
+ When /^(?:I|they) click the first link in the email$/ do
171
+ click_first_link_in_email
172
+ end
173
+
174
+ #
175
+ # Debugging
176
+ # These only work with Rails and OSx ATM since EmailViewer uses RAILS_ROOT and OSx's 'open' command.
177
+ # Patches accepted. ;)
178
+ #
179
+
180
+ Then /^save and open current email$/ do
181
+ EmailSpec::EmailViewer::save_and_open_email(current_email)
182
+ end
183
+
184
+ Then /^save and open all text emails$/ do
185
+ EmailSpec::EmailViewer::save_and_open_all_text_emails
186
+ end
187
+
188
+ Then /^save and open all html emails$/ do
189
+ EmailSpec::EmailViewer::save_and_open_all_html_emails
190
+ end
191
+
192
+ Then /^save and open all raw emails$/ do
193
+ EmailSpec::EmailViewer::save_and_open_all_raw_emails
194
+ end
@@ -0,0 +1,182 @@
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
+ Then /^(?:I|they|"([^"]*?)") should receive (an|no|\d+) emails? with subject "([^"]*?)"$/ do |address, amount, subject|
60
+ unread_emails_for(address).select { |m| m.subject =~ Regexp.new(subject) }.size.should == parse_email_count(amount)
61
+ end
62
+
63
+ Then /^(?:I|they|"([^"]*?)") should receive an email with the following body:$/ do |address, expected_body|
64
+ open_email(address, :with_text => expected_body)
65
+ end
66
+
67
+ #
68
+ # Accessing emails
69
+ #
70
+
71
+ # Opens the most recently received email
72
+ When /^(?:I|they|"([^"]*?)") opens? the email$/ do |address|
73
+ open_email(address)
74
+ end
75
+
76
+ When /^(?:I|they|"([^"]*?)") opens? the email with subject "([^"]*?)"$/ do |address, subject|
77
+ open_email(address, :with_subject => subject)
78
+ end
79
+
80
+ When /^(?:I|they|"([^"]*?)") opens? the email with text "([^"]*?)"$/ do |address, text|
81
+ open_email(address, :with_text => text)
82
+ end
83
+
84
+ #
85
+ # Inspect the Email Contents
86
+ #
87
+
88
+ Then /^(?:I|they) should see "([^"]*?)" in the email subject$/ do |text|
89
+ current_email.should have_subject(text)
90
+ end
91
+
92
+ Then /^(?:I|they) should see \/([^"]*?)\/ in the email subject$/ do |text|
93
+ current_email.should have_subject(Regexp.new(text))
94
+ end
95
+
96
+ Then /^(?:I|they) should see "([^"]*?)" in the email body$/ do |text|
97
+ current_email.body.should include(text)
98
+ end
99
+
100
+ Then /^(?:I|they) should see \/([^"]*?)\/ in the email body$/ do |text|
101
+ current_email.body.should =~ Regexp.new(text)
102
+ end
103
+
104
+ Then /^(?:I|they) should see the email delivered from "([^"]*?)"$/ do |text|
105
+ current_email.should be_delivered_from(text)
106
+ end
107
+
108
+ Then /^(?:I|they) should see "([^\"]*)" in the email "([^"]*?)" header$/ do |text, name|
109
+ current_email.should have_header(name, text)
110
+ end
111
+
112
+ Then /^(?:I|they) should see \/([^\"]*)\/ in the email "([^"]*?)" header$/ do |text, name|
113
+ current_email.should have_header(name, Regexp.new(text))
114
+ end
115
+
116
+ #
117
+ # Inspect the Email Attachments
118
+ #
119
+
120
+ Then /^(?:I|they) should see (an|no|\d+) attachments? with the email$/ do |amount|
121
+ current_email_attachments.size.should == parse_email_count(amount)
122
+ end
123
+
124
+ Then /^there should be (an|no|\d+) attachments? named "([^"]*?)"$/ do |amount, filename|
125
+ current_email_attachments.select { |a| a.original_filename == filename }.size.should == parse_email_count(amount)
126
+ end
127
+
128
+ Then /^attachment (\d+) should be named "([^"]*?)"$/ do |index, filename|
129
+ current_email_attachments[(index.to_i - 1)].original_filename.should == filename
130
+ end
131
+
132
+ Then /^there should be (an|no|\d+) attachments? of type "([^"]*?)"$/ do |amount, content_type|
133
+ current_email_attachments.select { |a| a.content_type == content_type }.size.should == parse_email_count(amount)
134
+ end
135
+
136
+ Then /^attachment (\d+) should be of type "([^"]*?)"$/ do |index, content_type|
137
+ current_email_attachments[(index.to_i - 1)].content_type.should == content_type
138
+ end
139
+
140
+ Then /^all attachments should not be blank$/ do
141
+ current_email_attachments.each do |attachment|
142
+ attachment.size.should_not == 0
143
+ end
144
+ end
145
+
146
+ Then /^show me a list of email attachments$/ do
147
+ EmailSpec::EmailViewer::save_and_open_email_attachments_list(current_email)
148
+ end
149
+
150
+ #
151
+ # Interact with Email Contents
152
+ #
153
+
154
+ When /^(?:I|they) follow "([^"]*?)" in the email$/ do |link|
155
+ visit_in_email(link)
156
+ end
157
+
158
+ When /^(?:I|they) click the first link in the email$/ do
159
+ click_first_link_in_email
160
+ end
161
+
162
+ #
163
+ # Debugging
164
+ # These only work with Rails and OSx ATM since EmailViewer uses RAILS_ROOT and OSx's 'open' command.
165
+ # Patches accepted. ;)
166
+ #
167
+
168
+ Then /^save and open current email$/ do
169
+ EmailSpec::EmailViewer::save_and_open_email(current_email)
170
+ end
171
+
172
+ Then /^save and open all text emails$/ do
173
+ EmailSpec::EmailViewer::save_and_open_all_text_emails
174
+ end
175
+
176
+ Then /^save and open all html emails$/ do
177
+ EmailSpec::EmailViewer::save_and_open_all_html_emails
178
+ end
179
+
180
+ Then /^save and open all raw emails$/ do
181
+ EmailSpec::EmailViewer::save_and_open_all_raw_emails
182
+ end
@@ -0,0 +1,194 @@
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
+ Then /^(?:I|they|"([^"]*?)") should receive (an|no|\d+) emails? with subject "([^"]*?)"$/ do |address, amount, subject|
60
+ unread_emails_for(address).select { |m| m.subject =~ Regexp.new(subject) }.size.should == parse_email_count(amount)
61
+ end
62
+
63
+ Then /^(?:I|they|"([^"]*?)") should receive an email with the following body:$/ do |address, expected_body|
64
+ open_email(address, :with_text => expected_body)
65
+ end
66
+
67
+ #
68
+ # Accessing emails
69
+ #
70
+
71
+ # Opens the most recently received email
72
+ When /^(?:I|they|"([^"]*?)") opens? the email$/ do |address|
73
+ open_email(address)
74
+ end
75
+
76
+ When /^(?:I|they|"([^"]*?)") opens? the email with subject "([^"]*?)"$/ do |address, subject|
77
+ open_email(address, :with_subject => subject)
78
+ end
79
+
80
+ When /^(?:I|they|"([^"]*?)") opens? the email with text "([^"]*?)"$/ do |address, text|
81
+ open_email(address, :with_text => text)
82
+ end
83
+
84
+ #
85
+ # Inspect the Email Contents
86
+ #
87
+
88
+ Then /^(?:I|they) should see "([^"]*?)" in the email subject$/ do |text|
89
+ current_email.should have_subject(text)
90
+ end
91
+
92
+ Then /^(?:I|they) should see \/([^"]*?)\/ in the email subject$/ do |text|
93
+ current_email.should have_subject(Regexp.new(text))
94
+ end
95
+
96
+ Then /^(?:I|they) should see "([^"]*?)" in the email body$/ do |text|
97
+ current_email.default_part_body.to_s.should include(text)
98
+ end
99
+
100
+ Then /^(?:I|they) should see \/([^"]*?)\/ in the email body$/ do |text|
101
+ current_email.default_part_body.to_s.should =~ Regexp.new(text)
102
+ end
103
+
104
+ Then /^(?:I|they) should see the email delivered from "([^"]*?)"$/ do |text|
105
+ current_email.should be_delivered_from(text)
106
+ end
107
+
108
+ Then /^(?:I|they) should see "([^\"]*)" in the email "([^"]*?)" header$/ do |text, name|
109
+ current_email.should have_header(name, text)
110
+ end
111
+
112
+ Then /^(?:I|they) should see \/([^\"]*)\/ in the email "([^"]*?)" header$/ do |text, name|
113
+ current_email.should have_header(name, Regexp.new(text))
114
+ end
115
+
116
+ Then /^I should see it is a multi\-part email$/ do
117
+ current_email.should be_multipart
118
+ end
119
+
120
+ Then /^(?:I|they) should see "([^"]*?)" in the email html part body$/ do |text|
121
+ current_email.html_part.body.to_s.should include(text)
122
+ end
123
+
124
+ Then /^(?:I|they) should see "([^"]*?)" in the email text part body$/ do |text|
125
+ current_email.text_part.body.to_s.should include(text)
126
+ end
127
+
128
+ #
129
+ # Inspect the Email Attachments
130
+ #
131
+
132
+ Then /^(?:I|they) should see (an|no|\d+) attachments? with the email$/ do |amount|
133
+ current_email_attachments.size.should == parse_email_count(amount)
134
+ end
135
+
136
+ Then /^there should be (an|no|\d+) attachments? named "([^"]*?)"$/ do |amount, filename|
137
+ current_email_attachments.select { |a| a.filename == filename }.size.should == parse_email_count(amount)
138
+ end
139
+
140
+ Then /^attachment (\d+) should be named "([^"]*?)"$/ do |index, filename|
141
+ current_email_attachments[(index.to_i - 1)].filename.should == filename
142
+ end
143
+
144
+ Then /^there should be (an|no|\d+) attachments? of type "([^"]*?)"$/ do |amount, content_type|
145
+ current_email_attachments.select { |a| a.content_type.include?(content_type) }.size.should == parse_email_count(amount)
146
+ end
147
+
148
+ Then /^attachment (\d+) should be of type "([^"]*?)"$/ do |index, content_type|
149
+ current_email_attachments[(index.to_i - 1)].content_type.should include(content_type)
150
+ end
151
+
152
+ Then /^all attachments should not be blank$/ do
153
+ current_email_attachments.each do |attachment|
154
+ attachment.read.size.should_not == 0
155
+ end
156
+ end
157
+
158
+ Then /^show me a list of email attachments$/ do
159
+ EmailSpec::EmailViewer::save_and_open_email_attachments_list(current_email)
160
+ end
161
+
162
+ #
163
+ # Interact with Email Contents
164
+ #
165
+
166
+ When /^(?:I|they) follow "([^"]*?)" in the email$/ do |link|
167
+ visit_in_email(link)
168
+ end
169
+
170
+ When /^(?:I|they) click the first link in the email$/ do
171
+ click_first_link_in_email
172
+ end
173
+
174
+ #
175
+ # Debugging
176
+ # These only work with Rails and OSx ATM since EmailViewer uses RAILS_ROOT and OSx's 'open' command.
177
+ # Patches accepted. ;)
178
+ #
179
+
180
+ Then /^save and open current email$/ do
181
+ EmailSpec::EmailViewer::save_and_open_email(current_email)
182
+ end
183
+
184
+ Then /^save and open all text emails$/ do
185
+ EmailSpec::EmailViewer::save_and_open_all_text_emails
186
+ end
187
+
188
+ Then /^save and open all html emails$/ do
189
+ EmailSpec::EmailViewer::save_and_open_all_html_emails
190
+ end
191
+
192
+ Then /^save and open all raw emails$/ do
193
+ EmailSpec::EmailViewer::save_and_open_all_raw_emails
194
+ end
@@ -0,0 +1,168 @@
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, opts={})
10
+ visit(parse_email_for_link(current_email, link_text, opts))
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.default_part_body}" unless url
16
+ visit request_uri(url)
17
+ end
18
+
19
+ def click_first_link_in_email(email = current_email, opts={})
20
+ link = links_in_email(email).first
21
+ visit request_uri(link, opts)
22
+ end
23
+
24
+ def open_email(address, opts={})
25
+ set_current_email(find_email!(address, opts))
26
+ end
27
+
28
+ alias_method :open_email_for, :open_email
29
+
30
+ def open_last_email
31
+ set_current_email(last_email_sent)
32
+ end
33
+
34
+ def open_last_email_for(address)
35
+ set_current_email(mailbox_for(address).last)
36
+ end
37
+
38
+ def current_email(address=nil)
39
+ address = convert_address(address)
40
+ email = address ? email_spec_hash[:current_emails][address] : email_spec_hash[:current_email]
41
+ raise RSpec::Expectations::ExpectationNotMetError, "Expected an open email but none was found. Did you forget to call open_email?" unless email
42
+ email
43
+ end
44
+
45
+ def current_email_attachments(address=nil)
46
+ current_email(address).attachments || Array.new
47
+ end
48
+
49
+ def unread_emails_for(address)
50
+ mailbox_for(address) - read_emails_for(address)
51
+ end
52
+
53
+ def read_emails_for(address)
54
+ email_spec_hash[:read_emails][convert_address(address)] ||= []
55
+ end
56
+
57
+ def find_email(address, opts={})
58
+ address = convert_address(address)
59
+ if opts[:with_subject]
60
+ mailbox_for(address).find { |m| m.subject =~ Regexp.new(opts[:with_subject]) }
61
+ elsif opts[:with_text]
62
+ mailbox_for(address).find { |m| m.default_part_body =~ Regexp.new(opts[:with_text]) }
63
+ else
64
+ mailbox_for(address).first
65
+ end
66
+ end
67
+
68
+ def links_in_email(email)
69
+ URI.extract(email.default_part_body.to_s, ['http', 'https'])
70
+ end
71
+
72
+ private
73
+
74
+ def email_spec_hash
75
+ @email_spec_hash ||= {:read_emails => {}, :unread_emails => {}, :current_emails => {}, :current_email => nil}
76
+ end
77
+
78
+ def find_email!(address, opts={})
79
+ email = find_email(address, opts)
80
+ if email.nil?
81
+ error = "#{opts.keys.first.to_s.humanize unless opts.empty?} #{('"' + opts.values.first.to_s.humanize + '"') unless opts.empty?}"
82
+ raise RSpec::Expectations::ExpectationNotMetError, "Could not find email #{error}. \n Found the following emails:\n\n #{all_emails.to_s}"
83
+ end
84
+ email
85
+ end
86
+
87
+ def set_current_email(email)
88
+ return unless email
89
+ [email.to, email.cc, email.bcc].compact.flatten.each do |to|
90
+ read_emails_for(to) << email
91
+ email_spec_hash[:current_emails][to] = email
92
+ end
93
+ email_spec_hash[:current_email] = email
94
+ end
95
+
96
+ def parse_email_for_link(email, text_or_regex, opts={})
97
+ email.should have_body_text(text_or_regex)
98
+
99
+ url = parse_email_for_explicit_link(email, text_or_regex, opts)
100
+ url ||= parse_email_for_anchor_text_link(email, text_or_regex, opts)
101
+
102
+ raise "No link found matching #{text_or_regex.inspect} in #{email}" unless url
103
+ url
104
+ end
105
+
106
+ def request_uri(link, opts={})
107
+ return unless link
108
+ url = URI::parse(link)
109
+ # url.fragment ? (url.request_uri + "#" + url.fragment) : url.request_uri
110
+ path = url.fragment ? (url.request_uri + "#" + url.fragment) : url.request_uri
111
+ opts[:path_only] != false ? path : url.to_s
112
+ end
113
+
114
+ # e.g. confirm in http://confirm
115
+ def parse_email_for_explicit_link(email, regex, opts={})
116
+ regex = /#{Regexp.escape(regex)}/ unless regex.is_a?(Regexp)
117
+ url = links_in_email(email).detect { |link| link =~ regex }
118
+ request_uri(url, opts)
119
+ end
120
+
121
+ # e.g. Click here in <a href="http://confirm">Click here</a>
122
+ def parse_email_for_anchor_text_link(email, link_text, opts={})
123
+ if textify_images(email.default_part_body) =~ %r{<a[^>]*href=['"]?([^'"]*)['"]?[^>]*?>[^<]*?#{link_text}[^<]*?</a>}
124
+ url = $1
125
+ path = URI.split(url)[5..-1].compact!.join("?").gsub("&amp;", "&")
126
+ opts[:path_only] ? path : url.to_s
127
+ # sub correct ampersand after rails switches it (http://dev.rubyonrails.org/ticket/4002)
128
+ else
129
+ return nil
130
+ end
131
+ end
132
+
133
+ def textify_images(email_body)
134
+ email_body.to_s.gsub(%r{<img[^>]*alt=['"]?([^'"]*)['"]?[^>]*?/>}) { $1 }
135
+ end
136
+
137
+ def parse_email_count(amount)
138
+ case amount
139
+ when "no"
140
+ 0
141
+ when "an"
142
+ 1
143
+ else
144
+ amount.to_i
145
+ end
146
+ end
147
+
148
+ attr_reader :last_email_address
149
+
150
+ def convert_address(address)
151
+ @last_email_address = (address || current_email_address)
152
+ AddressConverter.instance.convert(@last_email_address)
153
+ end
154
+
155
+
156
+ def mailbox_for(address)
157
+ super(convert_address(address)) # super resides in Deliveries
158
+ end
159
+
160
+ def email_spec_deprecate(text)
161
+ puts ""
162
+ puts "DEPRECATION: #{text.split.join(' ')}"
163
+ puts ""
164
+ end
165
+
166
+ end
167
+ end
168
+
@@ -9,7 +9,11 @@ module EmailSpec
9
9
  end
10
10
 
11
11
  def reset_mailer
12
- deliveries.clear
12
+ if ActionMailer::Base.delivery_method == :cache
13
+ mailer.clear_cache
14
+ else
15
+ deliveries.clear
16
+ end
13
17
  end
14
18
 
15
19
  def mailbox_for(address)
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: email_spec
3
3
  version: !ruby/object:Gem::Version
4
- hash: 13
4
+ hash: 11
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 6
9
- - 5
10
- version: 0.6.5
9
+ - 6
10
+ version: 0.6.6
11
11
  platform: ruby
12
12
  authors:
13
13
  - Ben Mabey
@@ -17,10 +17,23 @@ autorequire:
17
17
  bindir: bin
18
18
  cert_chain: []
19
19
 
20
- date: 2011-01-11 00:00:00 -07:00
20
+ date: 2011-03-26 00:00:00 -06:00
21
21
  default_executable:
22
- dependencies: []
23
-
22
+ dependencies:
23
+ - !ruby/object:Gem::Dependency
24
+ name: rspec
25
+ prerelease: false
26
+ requirement: &id001 !ruby/object:Gem::Requirement
27
+ none: false
28
+ requirements:
29
+ - - ">="
30
+ - !ruby/object:Gem::Version
31
+ hash: 3
32
+ segments:
33
+ - 0
34
+ version: "0"
35
+ type: :runtime
36
+ version_requirements: *id001
24
37
  description: Easily test email in rspec and cucumber
25
38
  email: ben@benmabey.com
26
39
  executables: []
@@ -38,6 +51,7 @@ files:
38
51
  - install.rb
39
52
  - lib/email-spec.rb
40
53
  - lib/email_spec.rb
54
+ - lib/email_spec/#helpers.rb#
41
55
  - lib/email_spec/address_converter.rb
42
56
  - lib/email_spec/background_processes.rb
43
57
  - lib/email_spec/cucumber.rb
@@ -51,6 +65,7 @@ files:
51
65
  - spec/email_spec/matchers_spec.rb
52
66
  - spec/spec.opts
53
67
  - spec/spec_helper.rb
68
+ - examples/rails3_root/features/step_definitions/email_steps.rb
54
69
  - examples/rails_root/app/controllers/application_controller.rb
55
70
  - examples/rails_root/app/controllers/welcome_controller.rb
56
71
  - examples/rails_root/app/helpers/application_helper.rb
@@ -135,10 +150,12 @@ files:
135
150
  - examples/sinatra/app.rb
136
151
  - examples/sinatra/features/errors.feature
137
152
  - examples/sinatra/features/example.feature
153
+ - examples/sinatra/features/step_definitions/email_steps.rb
138
154
  - examples/sinatra/features/step_definitions/user_steps.rb
139
155
  - examples/sinatra/features/step_definitions/web_steps.rb
140
156
  - examples/sinatra/features/support/env.rb
141
157
  - examples/sinatra/features/support/paths.rb
158
+ - examples/sinatra_root/features/step_definitions/email_steps.rb
142
159
  has_rdoc: true
143
160
  homepage: http://github.com/bmabey/email-spec/
144
161
  licenses: []
@@ -178,6 +195,7 @@ test_files:
178
195
  - spec/email_spec/matchers_spec.rb
179
196
  - spec/spec.opts
180
197
  - spec/spec_helper.rb
198
+ - examples/rails3_root/features/step_definitions/email_steps.rb
181
199
  - examples/rails_root/app/controllers/application_controller.rb
182
200
  - examples/rails_root/app/controllers/welcome_controller.rb
183
201
  - examples/rails_root/app/helpers/application_helper.rb
@@ -262,7 +280,9 @@ test_files:
262
280
  - examples/sinatra/app.rb
263
281
  - examples/sinatra/features/errors.feature
264
282
  - examples/sinatra/features/example.feature
283
+ - examples/sinatra/features/step_definitions/email_steps.rb
265
284
  - examples/sinatra/features/step_definitions/user_steps.rb
266
285
  - examples/sinatra/features/step_definitions/web_steps.rb
267
286
  - examples/sinatra/features/support/env.rb
268
287
  - examples/sinatra/features/support/paths.rb
288
+ - examples/sinatra_root/features/step_definitions/email_steps.rb