email_spec 0.3.0 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -5,29 +5,28 @@ module EmailSpec
5
5
 
6
6
  module Helpers
7
7
  include Deliveries
8
-
8
+
9
9
  def visit_in_email(link_text)
10
10
  visit(parse_email_for_link(current_email, link_text))
11
11
  end
12
-
12
+
13
13
  def click_email_link_matching(regex, email = current_email)
14
14
  url = links_in_email(email).detect { |link| link =~ regex }
15
15
  raise "No link found matching #{regex.inspect} in #{email.body}" unless url
16
16
  request_uri = URI::parse(url).request_uri
17
17
  visit request_uri
18
18
  end
19
-
19
+
20
20
  def click_first_link_in_email(email = current_email)
21
21
  link = links_in_email(email).first
22
22
  request_uri = URI::parse(link).request_uri
23
23
  visit request_uri
24
24
  end
25
-
25
+
26
26
  def open_email(address, opts={})
27
- address = convert_address(address)
28
27
  set_current_email(find_email!(address, opts))
29
28
  end
30
-
29
+
31
30
  alias_method :open_email_for, :open_email
32
31
 
33
32
  def open_last_email
@@ -35,38 +34,35 @@ module EmailSpec
35
34
  end
36
35
 
37
36
  def open_last_email_for(address)
38
- address = convert_address(address)
39
37
  set_current_email(mailbox_for(address).last)
40
38
  end
41
-
39
+
42
40
  def current_email(address=nil)
43
41
  address = convert_address(address)
44
42
  email = address ? email_spec_hash[:current_emails][address] : email_spec_hash[:current_email]
45
- raise Spec::Expectations::ExpectationNotMetError, "Expected an open email but none was found. Did you forget to call open_email?" unless email
43
+ raise Spec::Expectations::ExpectationNotMetError, "Expected an open email but none was found. Did you forget to call open_email?" unless email
46
44
  email
47
45
  end
48
-
46
+
49
47
  def unread_emails_for(address)
50
- address = convert_address(address)
51
48
  mailbox_for(address) - read_emails_for(address)
52
49
  end
53
-
50
+
54
51
  def read_emails_for(address)
55
- address = convert_address(address)
56
- email_spec_hash[:read_emails][address] ||= []
52
+ email_spec_hash[:read_emails][convert_address(address)] ||= []
57
53
  end
58
-
54
+
59
55
  def find_email(address, opts={})
60
56
  address = convert_address(address)
61
- if opts[:with_subject]
62
- email = mailbox_for(address).find { |m| m.subject =~ Regexp.new(opts[:with_subject]) }
57
+ if opts[:with_subject]
58
+ mailbox_for(address).find { |m| m.subject =~ Regexp.new(opts[:with_subject]) }
63
59
  elsif opts[:with_text]
64
- email = mailbox_for(address).find { |m| m.body =~ Regexp.new(opts[:with_text]) }
60
+ mailbox_for(address).find { |m| m.body =~ Regexp.new(opts[:with_text]) }
65
61
  else
66
- email = mailbox_for(address).first
62
+ mailbox_for(address).first
67
63
  end
68
64
  end
69
-
65
+
70
66
  def links_in_email(email)
71
67
  URI.extract(email.body, ['http', 'https'])
72
68
  end
@@ -78,7 +74,6 @@ module EmailSpec
78
74
  end
79
75
 
80
76
  def find_email!(address, opts={})
81
- address = convert_address(address)
82
77
  email = find_email(address, opts)
83
78
  if email.nil?
84
79
  error = "#{opts.keys.first.to_s.humanize unless opts.empty?} #{('"' + opts.values.first.to_s.humanize + '"') unless opts.empty?}"
@@ -90,43 +85,65 @@ module EmailSpec
90
85
  def set_current_email(email)
91
86
  return unless email
92
87
  email.to.each do |to|
93
- read_emails_for(to) << email
88
+ read_emails_for(to) << email
94
89
  email_spec_hash[:current_emails][to] = email
95
90
  end
96
91
  email_spec_hash[:current_email] = email
97
92
  end
98
-
93
+
99
94
  def parse_email_for_link(email, text_or_regex)
100
95
  email.should have_body_text(text_or_regex)
101
-
102
- url = parse_email_for_explicit_link(email, text_or_regex)
96
+
97
+ url = parse_email_for_explicit_link(email, text_or_regex)
103
98
  url ||= parse_email_for_anchor_text_link(email, text_or_regex)
104
99
 
105
100
  raise "No link found matching #{text_or_regex.inspect} in #{email}" unless url
106
101
  url
107
102
  end
108
-
103
+
109
104
  # e.g. confirm in http://confirm
110
105
  def parse_email_for_explicit_link(email, regex)
111
106
  regex = /#{Regexp.escape(regex)}/ unless regex.is_a?(Regexp)
112
107
  url = links_in_email(email).detect { |link| link =~ regex }
113
108
  URI::parse(url).request_uri if url
114
109
  end
115
-
110
+
116
111
  # e.g. Click here in <a href="http://confirm">Click here</a>
117
112
  def parse_email_for_anchor_text_link(email, link_text)
118
113
  email.body =~ %r{<a[^>]*href=['"]?([^'"]*)['"]?[^>]*?>[^<]*?#{link_text}[^<]*?</a>}
119
114
  URI.split($~[1])[5..-1].compact!.join("?").gsub("&amp;", "&")
120
- # sub correct ampersand after rails switches it (http://dev.rubyonrails.org/ticket/4002)
115
+ # sub correct ampersand after rails switches it (http://dev.rubyonrails.org/ticket/4002)
121
116
  end
122
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
123
128
 
124
129
  attr_reader :last_email_address
125
-
130
+
126
131
  def convert_address(address)
127
- @last_email_address = address
128
- AddressConverter.instance.convert(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
129
139
  end
140
+
141
+ def email_spec_deprecate(text)
142
+ puts ""
143
+ puts "DEPRECATION: #{text.split.join(' ')}"
144
+ puts ""
145
+ end
146
+
130
147
  end
131
148
  end
132
149
 
@@ -1,9 +1,10 @@
1
- #Commonly used email steps
1
+ # Commonly used email steps
2
2
  #
3
3
  # To add your own steps make a custom_email_steps.rb
4
4
  # The provided methods are:
5
5
  #
6
- # reset_mailer
6
+ # last_email_address
7
+ # reset_mailer
7
8
  # open_last_email
8
9
  # visit_in_email
9
10
  # unread_emails_for
@@ -12,6 +13,16 @@
12
13
  # open_email
13
14
  # read_emails_for
14
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.
15
26
 
16
27
  module EmailHelpers
17
28
  def current_email_address
@@ -21,63 +32,88 @@ module EmailHelpers
21
32
  last_email_address || "example@example.com"
22
33
  end
23
34
  end
35
+
24
36
  World(EmailHelpers)
25
37
 
26
- # Use this step to reset the e-mail queue within a scenario.
38
+ #
39
+ # Reset the e-mail queue within a scenario.
27
40
  # This is done automatically before each scenario.
41
+ #
42
+
28
43
  Given /^(?:a clear email queue|no emails have been sent)$/ do
29
44
  reset_mailer
30
45
  end
31
46
 
32
- # Use this step to open the most recently sent e-mail.
33
- When /^(?:I|they) open the email$/ do
34
- open_email(current_email_address)
35
- end
47
+ #
48
+ # Check how many emails have been sent/received
49
+ #
36
50
 
37
- When %r{^(?:I|they) follow "([^"]*?)" in the email$} do |link|
38
- visit_in_email(link)
51
+ Then /^(?:I|they|"([^"]*?)") should receive (an|no|\d+) emails?$/ do |address, amount|
52
+ unread_emails_for(address).size.should == parse_email_count(amount)
39
53
  end
40
54
 
41
- Then /^(?:I|they) should receive (an|\d+) emails?$/ do |amount|
42
- amount = 1 if amount == "an"
43
- unread_emails_for(current_email_address).size.should == amount.to_i
55
+ Then /^(?:I|they|"([^"]*?)") should have (an|no|\d+) emails?$/ do |address, amount|
56
+ mailbox_for(address).size.should == parse_email_count(amount)
44
57
  end
45
58
 
46
- Then /^(?:I|they) should not receive any emails?$/ do
47
- unread_emails_for(current_email_address).size.should == 0
59
+ # DEPRECATED
60
+ # The following methods are left in for backwards compatibility and
61
+ # should be removed by version 0.3.5.
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
48
66
  end
49
67
 
50
- Then %r{^"([^"]*?)" should receive (an|\d+) emails?$} do |address, amount|
51
- amount = 1 if amount == "an"
52
- unread_emails_for(address).size.should == amount.to_i
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)
53
75
  end
54
76
 
55
- Then %r{^"([^"]*?)" should have (\d+) emails?$} do |address, n|
56
- mailbox_for(address).size.should == n.to_i
77
+ When /^(?:I|they|"([^"]*?)") opens? the email with subject "([^"]*?)"$/ do |address, subject|
78
+ open_email(address, :with_subject => subject)
57
79
  end
58
80
 
59
- Then %r{^"([^"]*?)" should not receive an email$} do |address|
60
- find_email(address).should be_nil
81
+ When /^(?:I|they|"([^"]*?)") opens? the email with text "([^"]*?)"$/ do |address, text|
82
+ open_email(address, :with_text => text)
61
83
  end
62
84
 
63
- Then %r{^(?:I|they) should see "([^"]*?)" in the subject$} do |text|
85
+ #
86
+ # Inspect the Email Contents
87
+ #
88
+
89
+ Then /^(?:I|they) should see "([^"]*?)" in the email subject$/ do |text|
64
90
  current_email.should have_subject(Regexp.new(text))
65
91
  end
66
92
 
67
- Then %r{^(?:I|they) should see "([^"]*?)" in the email$} do |text|
93
+ Then /^(?:I|they) should see "([^"]*?)" in the email body$/ do |text|
68
94
  current_email.body.should =~ Regexp.new(text)
69
95
  end
70
96
 
71
- When %r{^"([^"]*?)" opens? the email$} do |address|
72
- open_email(address)
97
+ # DEPRECATED
98
+ # The following methods are left in for backwards compatibility and
99
+ # should be removed by version 0.3.5.
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))
73
104
  end
74
-
75
- When %r{^"([^"]*?)" opens? the email with subject "([^"]*?)"$} do |address, subject|
76
- open_email(address, :with_subject => subject)
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)
77
109
  end
78
110
 
79
- When %r{^"([^"]*?)" opens? the email with text "([^"]*?)"$} do |address, text|
80
- open_email(address, :with_text => text)
111
+ #
112
+ # Interact with Email Contents
113
+ #
114
+
115
+ When /^(?:I|they) follow "([^"]*?)" in the email$/ do |link|
116
+ visit_in_email(link)
81
117
  end
82
118
 
83
119
  When /^(?:I|they) click the first link in the email$/ do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: email_spec
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ben Mabey
@@ -11,7 +11,7 @@ autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
13
 
14
- date: 2009-08-13 00:00:00 -06:00
14
+ date: 2009-08-19 00:00:00 -06:00
15
15
  default_executable:
16
16
  dependencies: []
17
17
 
@@ -29,78 +29,6 @@ files:
29
29
  - MIT-LICENSE.txt
30
30
  - README.rdoc
31
31
  - Rakefile
32
- - examples/rails_root/Rakefile
33
- - examples/rails_root/app/controllers/application.rb
34
- - examples/rails_root/app/controllers/welcome_controller.rb
35
- - examples/rails_root/app/helpers/application_helper.rb
36
- - examples/rails_root/app/helpers/welcome_helper.rb
37
- - examples/rails_root/app/models/user.rb
38
- - examples/rails_root/app/models/user_mailer.rb
39
- - examples/rails_root/app/views/user_mailer/signup.erb
40
- - examples/rails_root/app/views/welcome/confirm.html.erb
41
- - examples/rails_root/app/views/welcome/index.html.erb
42
- - examples/rails_root/app/views/welcome/signup.html.erb
43
- - examples/rails_root/config/boot.rb
44
- - examples/rails_root/config/database.yml
45
- - examples/rails_root/config/environment.rb
46
- - examples/rails_root/config/environments/development.rb
47
- - examples/rails_root/config/environments/production.rb
48
- - examples/rails_root/config/environments/test.rb
49
- - examples/rails_root/config/initializers/inflections.rb
50
- - examples/rails_root/config/initializers/mime_types.rb
51
- - examples/rails_root/config/initializers/new_rails_defaults.rb
52
- - examples/rails_root/config/routes.rb
53
- - examples/rails_root/cucumber.yml
54
- - examples/rails_root/db/migrate/20090125013728_create_users.rb
55
- - examples/rails_root/db/schema.rb
56
- - examples/rails_root/doc/README_FOR_APP
57
- - examples/rails_root/features/errors.feature
58
- - examples/rails_root/features/example.feature
59
- - examples/rails_root/features/step_definitions/email_steps.rb
60
- - examples/rails_root/features/step_definitions/user_steps.rb
61
- - examples/rails_root/features/step_definitions/webrat_steps.rb
62
- - examples/rails_root/features/support/env.rb
63
- - examples/rails_root/public/404.html
64
- - examples/rails_root/public/422.html
65
- - examples/rails_root/public/500.html
66
- - examples/rails_root/public/dispatch.rb
67
- - examples/rails_root/public/favicon.ico
68
- - examples/rails_root/public/images/rails.png
69
- - examples/rails_root/public/javascripts/application.js
70
- - examples/rails_root/public/javascripts/controls.js
71
- - examples/rails_root/public/javascripts/dragdrop.js
72
- - examples/rails_root/public/javascripts/effects.js
73
- - examples/rails_root/public/javascripts/prototype.js
74
- - examples/rails_root/public/robots.txt
75
- - examples/rails_root/script/about
76
- - examples/rails_root/script/autospec
77
- - examples/rails_root/script/console
78
- - examples/rails_root/script/cucumber
79
- - examples/rails_root/script/dbconsole
80
- - examples/rails_root/script/destroy
81
- - examples/rails_root/script/generate
82
- - examples/rails_root/script/performance/benchmarker
83
- - examples/rails_root/script/performance/profiler
84
- - examples/rails_root/script/performance/request
85
- - examples/rails_root/script/plugin
86
- - examples/rails_root/script/process/inspector
87
- - examples/rails_root/script/process/reaper
88
- - examples/rails_root/script/process/spawner
89
- - examples/rails_root/script/runner
90
- - examples/rails_root/script/server
91
- - examples/rails_root/script/spec
92
- - examples/rails_root/script/spec_server
93
- - examples/rails_root/spec/controllers/welcome_controller_spec.rb
94
- - examples/rails_root/spec/model_factory.rb
95
- - examples/rails_root/spec/models/user_mailer_spec.rb
96
- - examples/rails_root/spec/models/user_spec.rb
97
- - examples/rails_root/spec/rcov.opts
98
- - examples/rails_root/spec/spec.opts
99
- - examples/rails_root/spec/spec_helper.rb
100
- - examples/rails_root/stories/all.rb
101
- - examples/rails_root/stories/helper.rb
102
- - examples/rails_root/vendor/plugins/email_spec/rails_generators/email_spec/email_spec_generator.rb
103
- - examples/rails_root/vendor/plugins/email_spec/rails_generators/email_spec/templates/email_steps.rb
104
32
  - install.rb
105
33
  - lib/email-spec.rb
106
34
  - lib/email_spec.rb
@@ -112,9 +40,6 @@ files:
112
40
  - lib/email_spec/matchers.rb
113
41
  - rails_generators/email_spec/email_spec_generator.rb
114
42
  - rails_generators/email_spec/templates/email_steps.rb
115
- - spec/email_spec/matchers_spec.rb
116
- - spec/spec.opts
117
- - spec/spec_helper.rb
118
43
  has_rdoc: true
119
44
  homepage: http://github.com/bmabey/email-spec/
120
45
  licenses: []
@@ -145,14 +70,20 @@ specification_version: 3
145
70
  summary: Easily test email in rspec and cucumber
146
71
  test_files:
147
72
  - spec/email_spec/matchers_spec.rb
73
+ - spec/spec.opts
148
74
  - spec/spec_helper.rb
149
- - examples/rails_root/app/controllers/application.rb
75
+ - examples/rails_root/app/controllers/application_controller.rb
150
76
  - examples/rails_root/app/controllers/welcome_controller.rb
151
77
  - examples/rails_root/app/helpers/application_helper.rb
152
78
  - examples/rails_root/app/helpers/welcome_helper.rb
153
79
  - examples/rails_root/app/models/user.rb
154
80
  - examples/rails_root/app/models/user_mailer.rb
81
+ - examples/rails_root/app/views/user_mailer/signup.erb
82
+ - examples/rails_root/app/views/welcome/confirm.html.erb
83
+ - examples/rails_root/app/views/welcome/index.html.erb
84
+ - examples/rails_root/app/views/welcome/signup.html.erb
155
85
  - examples/rails_root/config/boot.rb
86
+ - examples/rails_root/config/database.yml
156
87
  - examples/rails_root/config/environment.rb
157
88
  - examples/rails_root/config/environments/development.rb
158
89
  - examples/rails_root/config/environments/production.rb
@@ -163,15 +94,51 @@ test_files:
163
94
  - examples/rails_root/config/routes.rb
164
95
  - examples/rails_root/db/migrate/20090125013728_create_users.rb
165
96
  - examples/rails_root/db/schema.rb
97
+ - examples/rails_root/doc/README_FOR_APP
98
+ - examples/rails_root/features/errors.feature
99
+ - examples/rails_root/features/example.feature
166
100
  - examples/rails_root/features/step_definitions/email_steps.rb
167
101
  - examples/rails_root/features/step_definitions/user_steps.rb
168
102
  - examples/rails_root/features/step_definitions/webrat_steps.rb
169
103
  - examples/rails_root/features/support/env.rb
104
+ - examples/rails_root/features/support/paths.rb
105
+ - examples/rails_root/public/404.html
106
+ - examples/rails_root/public/422.html
107
+ - examples/rails_root/public/500.html
170
108
  - examples/rails_root/public/dispatch.rb
109
+ - examples/rails_root/public/favicon.ico
110
+ - examples/rails_root/public/images/rails.png
111
+ - examples/rails_root/public/javascripts/application.js
112
+ - examples/rails_root/public/javascripts/controls.js
113
+ - examples/rails_root/public/javascripts/dragdrop.js
114
+ - examples/rails_root/public/javascripts/effects.js
115
+ - examples/rails_root/public/javascripts/prototype.js
116
+ - examples/rails_root/public/robots.txt
117
+ - examples/rails_root/Rakefile
118
+ - examples/rails_root/script/about
119
+ - examples/rails_root/script/autospec
120
+ - examples/rails_root/script/console
121
+ - examples/rails_root/script/cucumber
122
+ - examples/rails_root/script/dbconsole
123
+ - examples/rails_root/script/destroy
124
+ - examples/rails_root/script/generate
125
+ - examples/rails_root/script/performance/benchmarker
126
+ - examples/rails_root/script/performance/profiler
127
+ - examples/rails_root/script/performance/request
128
+ - examples/rails_root/script/plugin
129
+ - examples/rails_root/script/process/inspector
130
+ - examples/rails_root/script/process/reaper
131
+ - examples/rails_root/script/process/spawner
132
+ - examples/rails_root/script/runner
133
+ - examples/rails_root/script/server
134
+ - examples/rails_root/script/spec
135
+ - examples/rails_root/script/spec_server
171
136
  - examples/rails_root/spec/controllers/welcome_controller_spec.rb
172
137
  - examples/rails_root/spec/model_factory.rb
173
138
  - examples/rails_root/spec/models/user_mailer_spec.rb
174
139
  - examples/rails_root/spec/models/user_spec.rb
140
+ - examples/rails_root/spec/rcov.opts
141
+ - examples/rails_root/spec/spec.opts
175
142
  - examples/rails_root/spec/spec_helper.rb
176
143
  - examples/rails_root/stories/all.rb
177
144
  - examples/rails_root/stories/helper.rb