opsb-email_spec 0.6.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (109) hide show
  1. data/History.txt +199 -0
  2. data/MIT-LICENSE.txt +19 -0
  3. data/README.rdoc +135 -0
  4. data/Rakefile +77 -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 +16 -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 +39 -0
  12. data/examples/rails_root/app/views/user_mailer/attachments.erb +6 -0
  13. data/examples/rails_root/app/views/user_mailer/newsletter.erb +8 -0
  14. data/examples/rails_root/app/views/user_mailer/signup.erb +3 -0
  15. data/examples/rails_root/app/views/welcome/attachments.html.erb +0 -0
  16. data/examples/rails_root/app/views/welcome/confirm.html.erb +1 -0
  17. data/examples/rails_root/app/views/welcome/index.html.erb +8 -0
  18. data/examples/rails_root/app/views/welcome/newsletter.html.erb +0 -0
  19. data/examples/rails_root/app/views/welcome/signup.html.erb +1 -0
  20. data/examples/rails_root/attachments/document.pdf +0 -0
  21. data/examples/rails_root/attachments/image.png +0 -0
  22. data/examples/rails_root/config/boot.rb +110 -0
  23. data/examples/rails_root/config/cucumber.yml +7 -0
  24. data/examples/rails_root/config/database.yml +25 -0
  25. data/examples/rails_root/config/environment.rb +28 -0
  26. data/examples/rails_root/config/environments/cucumber.rb +29 -0
  27. data/examples/rails_root/config/environments/development.rb +17 -0
  28. data/examples/rails_root/config/environments/production.rb +28 -0
  29. data/examples/rails_root/config/environments/test.rb +28 -0
  30. data/examples/rails_root/config/initializers/inflections.rb +10 -0
  31. data/examples/rails_root/config/initializers/mime_types.rb +5 -0
  32. data/examples/rails_root/config/initializers/new_rails_defaults.rb +17 -0
  33. data/examples/rails_root/config/routes.rb +45 -0
  34. data/examples/rails_root/db/migrate/20090125013728_create_users.rb +11 -0
  35. data/examples/rails_root/db/migrate/20090908054656_create_delayed_jobs.rb +20 -0
  36. data/examples/rails_root/db/schema.rb +32 -0
  37. data/examples/rails_root/doc/README_FOR_APP +5 -0
  38. data/examples/rails_root/features/attachments.feature +27 -0
  39. data/examples/rails_root/features/delayed_job.feature +13 -0
  40. data/examples/rails_root/features/errors.feature +32 -0
  41. data/examples/rails_root/features/example.feature +62 -0
  42. data/examples/rails_root/features/step_definitions/user_steps.rb +26 -0
  43. data/examples/rails_root/features/step_definitions/web_steps.rb +273 -0
  44. data/examples/rails_root/features/support/env.rb +58 -0
  45. data/examples/rails_root/features/support/env_ext.rb +6 -0
  46. data/examples/rails_root/features/support/paths.rb +33 -0
  47. data/examples/rails_root/lib/tasks/cucumber.rake +47 -0
  48. data/examples/rails_root/public/404.html +30 -0
  49. data/examples/rails_root/public/422.html +30 -0
  50. data/examples/rails_root/public/500.html +33 -0
  51. data/examples/rails_root/public/dispatch.rb +10 -0
  52. data/examples/rails_root/public/favicon.ico +0 -0
  53. data/examples/rails_root/public/images/rails.png +0 -0
  54. data/examples/rails_root/public/javascripts/application.js +2 -0
  55. data/examples/rails_root/public/javascripts/controls.js +963 -0
  56. data/examples/rails_root/public/javascripts/dragdrop.js +973 -0
  57. data/examples/rails_root/public/javascripts/effects.js +1128 -0
  58. data/examples/rails_root/public/javascripts/prototype.js +4320 -0
  59. data/examples/rails_root/public/robots.txt +5 -0
  60. data/examples/rails_root/script/about +4 -0
  61. data/examples/rails_root/script/autospec +5 -0
  62. data/examples/rails_root/script/console +3 -0
  63. data/examples/rails_root/script/cucumber +10 -0
  64. data/examples/rails_root/script/dbconsole +3 -0
  65. data/examples/rails_root/script/delayed_job +5 -0
  66. data/examples/rails_root/script/destroy +3 -0
  67. data/examples/rails_root/script/generate +3 -0
  68. data/examples/rails_root/script/performance/benchmarker +3 -0
  69. data/examples/rails_root/script/performance/profiler +3 -0
  70. data/examples/rails_root/script/performance/request +3 -0
  71. data/examples/rails_root/script/plugin +3 -0
  72. data/examples/rails_root/script/process/inspector +3 -0
  73. data/examples/rails_root/script/process/reaper +3 -0
  74. data/examples/rails_root/script/process/spawner +3 -0
  75. data/examples/rails_root/script/runner +3 -0
  76. data/examples/rails_root/script/server +3 -0
  77. data/examples/rails_root/script/spec +5 -0
  78. data/examples/rails_root/script/spec_server +125 -0
  79. data/examples/rails_root/spec/controllers/welcome_controller_spec.rb +15 -0
  80. data/examples/rails_root/spec/model_factory.rb +6 -0
  81. data/examples/rails_root/spec/models/user_mailer_spec.rb +58 -0
  82. data/examples/rails_root/spec/models/user_spec.rb +5 -0
  83. data/examples/rails_root/spec/rcov.opts +2 -0
  84. data/examples/rails_root/spec/spec.opts +4 -0
  85. data/examples/rails_root/spec/spec_helper.rb +51 -0
  86. data/examples/sinatra/app.rb +33 -0
  87. data/examples/sinatra/features/errors.feature +32 -0
  88. data/examples/sinatra/features/example.feature +62 -0
  89. data/examples/sinatra/features/step_definitions/user_steps.rb +26 -0
  90. data/examples/sinatra/features/step_definitions/web_steps.rb +273 -0
  91. data/examples/sinatra/features/support/env.rb +39 -0
  92. data/examples/sinatra/features/support/paths.rb +24 -0
  93. data/install.rb +0 -0
  94. data/lib/email-spec.rb +1 -0
  95. data/lib/email_spec.rb +15 -0
  96. data/lib/email_spec/address_converter.rb +29 -0
  97. data/lib/email_spec/background_processes.rb +45 -0
  98. data/lib/email_spec/cucumber.rb +26 -0
  99. data/lib/email_spec/deliveries.rb +87 -0
  100. data/lib/email_spec/email_viewer.rb +87 -0
  101. data/lib/email_spec/helpers.rb +160 -0
  102. data/lib/email_spec/matchers.rb +217 -0
  103. data/rails_generators/email_spec/email_spec_generator.rb +17 -0
  104. data/rails_generators/email_spec/templates/email_steps.rb +182 -0
  105. data/spec/email_spec/helpers_spec.rb +23 -0
  106. data/spec/email_spec/matchers_spec.rb +240 -0
  107. data/spec/spec.opts +4 -0
  108. data/spec/spec_helper.rb +4 -0
  109. metadata +268 -0
@@ -0,0 +1,29 @@
1
+ require 'singleton'
2
+
3
+ module EmailSpec
4
+ class AddressConverter
5
+ include Singleton
6
+
7
+ attr_accessor :converter
8
+
9
+ # The block provided to conversion should convert to an email
10
+ # address string or return the input untouched. For example:
11
+ #
12
+ # EmailSpec::AddressConverter.instance.conversion do |input|
13
+ # if input.is_a?(User)
14
+ # input.email
15
+ # else
16
+ # input
17
+ # end
18
+ # end
19
+ #
20
+ def conversion(&block)
21
+ self.converter = block
22
+ end
23
+
24
+ def convert(input)
25
+ return input unless converter
26
+ converter.call(input)
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,45 @@
1
+ module EmailSpec
2
+ module BackgroundProcesses
3
+ module DelayedJob
4
+ def all_emails
5
+ work_off_queue
6
+ super
7
+ end
8
+
9
+ def last_email_sent
10
+ work_off_queue
11
+ super
12
+ end
13
+
14
+ def reset_mailer
15
+ work_off_queue
16
+ super
17
+ end
18
+
19
+ def mailbox_for(address)
20
+ work_off_queue
21
+ super
22
+ end
23
+
24
+ private
25
+
26
+ # Later versions of DelayedJob switch from using Delayed::Job to Delayed::Worker
27
+ # Support both versions for those who haven't upgraded yet
28
+ def work_off_queue
29
+ if defined?(Delayed::Worker)
30
+ Delayed::Worker.send :public, :work_off
31
+ worker = Delayed::Worker.new(:max_priority => nil, :min_priority => nil, :quiet => true)
32
+ worker.work_off
33
+ else
34
+ Delayed::Job.work_off
35
+ end
36
+ end
37
+ end
38
+
39
+ module Compatibility
40
+ if defined?(Delayed) && (defined?(Delayed::Job) || defined?(Delayed::Worker))
41
+ include EmailSpec::BackgroundProcesses::DelayedJob
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,26 @@
1
+ # require this in your env.rb file after you require cucumber/rails/world
2
+
3
+ # Global Setup
4
+ if defined?(ActionMailer)
5
+ unless [:activerecord, :cache].include?(ActionMailer::Base.delivery_method)
6
+ ActionMailer::Base.delivery_method = :test
7
+ end
8
+ ActionMailer::Base.perform_deliveries = true
9
+
10
+ Before do
11
+ # Scenario setup
12
+ case ActionMailer::Base.delivery_method
13
+ when :test then ActionMailer::Base.deliveries.clear
14
+ when :cache then ActionMailer::Base.clear_cache
15
+ end
16
+ end
17
+ end
18
+
19
+ After do
20
+ EmailSpec::EmailViewer.save_and_open_all_raw_emails if ENV['SHOW_EMAILS']
21
+ EmailSpec::EmailViewer.save_and_open_all_html_emails if ENV['SHOW_HTML_EMAILS']
22
+ EmailSpec::EmailViewer.save_and_open_all_text_emails if ENV['SHOW_TEXT_EMAILS']
23
+ end
24
+
25
+ World(EmailSpec::Helpers)
26
+ World(EmailSpec::Matchers)
@@ -0,0 +1,87 @@
1
+ module EmailSpec
2
+ module MailerDeliveries
3
+ def all_emails
4
+ deliveries
5
+ end
6
+
7
+ def last_email_sent
8
+ deliveries.last || raise("No email has been sent!")
9
+ end
10
+
11
+ def reset_mailer
12
+ deliveries.clear
13
+ end
14
+
15
+ def mailbox_for(address)
16
+ deliveries.select { |email|
17
+ (email.to && email.to.include?(address)) ||
18
+ (email.bcc && email.bcc.include?(address)) ||
19
+ (email.cc && email.cc.include?(address)) }
20
+ end
21
+
22
+ protected
23
+
24
+ def deliveries
25
+ if ActionMailer::Base.delivery_method == :cache
26
+ mailer.cached_deliveries
27
+ else
28
+ mailer.deliveries
29
+ end
30
+ end
31
+ end
32
+
33
+ module ARMailerDeliveries
34
+ def all_emails
35
+ Email.all.map{ |email| parse_to_tmail(email) }
36
+ end
37
+
38
+ def last_email_sent
39
+ if email = Email.last
40
+ TMail::Mail.parse(email.mail)
41
+ else
42
+ raise("No email has been sent!")
43
+ end
44
+ end
45
+
46
+ def reset_mailer
47
+ Email.delete_all
48
+ end
49
+
50
+ def mailbox_for(address)
51
+ Email.all.select { |email|
52
+ (email.to && email.to.include?(address)) ||
53
+ (email.bcc && email.bcc.include?(address)) ||
54
+ (email.cc && email.cc.include?(address)) }.map{ |email| parse_to_tmail(email) }
55
+ end
56
+
57
+ def parse_to_tmail(email)
58
+ TMail::Mail.parse(email.mail)
59
+ end
60
+ end
61
+
62
+ if defined?(Pony)
63
+ module ::Pony
64
+ def self.deliveries
65
+ @deliveries ||= []
66
+ end
67
+
68
+ def self.mail(options)
69
+ deliveries << build_tmail(options)
70
+ end
71
+ end
72
+ end
73
+
74
+ module Deliveries
75
+ if defined?(Pony)
76
+ def deliveries; Pony::deliveries ; end
77
+ include EmailSpec::MailerDeliveries
78
+ elsif ActionMailer::Base.delivery_method == :activerecord
79
+ include EmailSpec::ARMailerDeliveries
80
+ else
81
+ def mailer; ActionMailer::Base; end
82
+ include EmailSpec::MailerDeliveries
83
+ end
84
+ include EmailSpec::BackgroundProcesses::Compatibility
85
+ end
86
+ end
87
+
@@ -0,0 +1,87 @@
1
+ module EmailSpec
2
+ class EmailViewer
3
+ extend Deliveries
4
+
5
+ def self.save_and_open_all_raw_emails
6
+ filename = tmp_email_filename
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 = tmp_email_filename
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.save_and_open_email_attachments_list(mail)
59
+ filename = tmp_email_filename
60
+
61
+ File.open(filename, "w") do |f|
62
+ mail.attachments.each_with_index do |attachment, index|
63
+ info = "#{index + 1}:"
64
+ info += "\n\tfilename: #{attachment.original_filename}"
65
+ info += "\n\tcontent type: #{attachment.content_type}"
66
+ info += "\n\tsize: #{attachment.size}"
67
+ f.write info + "\n"
68
+ end
69
+ end
70
+
71
+ open_in_text_editor(filename)
72
+ end
73
+
74
+ # TODO: use the launchy gem for this stuff...
75
+ def self.open_in_text_editor(filename)
76
+ `open #{filename}`
77
+ end
78
+
79
+ def self.open_in_browser(filename)
80
+ `open #{filename}`
81
+ end
82
+
83
+ def self.tmp_email_filename(extension = '.txt')
84
+ "#{RAILS_ROOT}/tmp/email-#{Time.now.to_i}#{extension}"
85
+ end
86
+ end
87
+ end
@@ -0,0 +1,160 @@
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 current_email_attachments(address=nil)
48
+ current_email(address).attachments || Array.new
49
+ end
50
+
51
+ def unread_emails_for(address)
52
+ mailbox_for(address) - read_emails_for(address)
53
+ end
54
+
55
+ def read_emails_for(address)
56
+ email_spec_hash[:read_emails][convert_address(address)] ||= []
57
+ end
58
+
59
+ def find_email(address, opts={})
60
+ address = convert_address(address)
61
+ if opts[:with_subject]
62
+ mailbox_for(address).find { |m| m.subject =~ Regexp.new(opts[:with_subject]) }
63
+ elsif opts[:with_text]
64
+ mailbox_for(address).find { |m| m.body =~ Regexp.new(opts[:with_text]) }
65
+ else
66
+ mailbox_for(address).first
67
+ end
68
+ end
69
+
70
+ def links_in_email(email)
71
+ URI.extract(email.body, ['http', 'https'])
72
+ end
73
+
74
+ private
75
+
76
+ def email_spec_hash
77
+ @email_spec_hash ||= {:read_emails => {}, :unread_emails => {}, :current_emails => {}, :current_email => nil}
78
+ end
79
+
80
+ def find_email!(address, opts={})
81
+ email = find_email(address, opts)
82
+ if email.nil?
83
+ error = "#{opts.keys.first.to_s.humanize unless opts.empty?} #{('"' + opts.values.first.to_s.humanize + '"') unless opts.empty?}"
84
+ raise Spec::Expectations::ExpectationNotMetError, "Could not find email #{error}. \n Found the following emails:\n\n #{all_emails.to_s}"
85
+ end
86
+ email
87
+ end
88
+
89
+ def set_current_email(email)
90
+ return unless email
91
+ email.to.each do |to|
92
+ read_emails_for(to) << email
93
+ email_spec_hash[:current_emails][to] = email
94
+ end
95
+ email_spec_hash[:current_email] = email
96
+ end
97
+
98
+ def parse_email_for_link(email, text_or_regex)
99
+ email.should have_body_text(text_or_regex)
100
+
101
+ url = parse_email_for_explicit_link(email, text_or_regex)
102
+ url ||= parse_email_for_anchor_text_link(email, text_or_regex)
103
+
104
+ raise "No link found matching #{text_or_regex.inspect} in #{email}" unless url
105
+ url
106
+ end
107
+
108
+ # e.g. confirm in http://confirm
109
+ def parse_email_for_explicit_link(email, regex)
110
+ regex = /#{Regexp.escape(regex)}/ unless regex.is_a?(Regexp)
111
+ url = links_in_email(email).detect { |link| link =~ regex }
112
+ URI::parse(url).request_uri if url
113
+ end
114
+
115
+ # e.g. Click here in <a href="http://confirm">Click here</a>
116
+ def parse_email_for_anchor_text_link(email, link_text)
117
+ if textify_images(email.body) =~ %r{<a[^>]*href=['"]?([^'"]*)['"]?[^>]*?>[^<]*?#{link_text}[^<]*?</a>}
118
+ URI.split($1)[5..-1].compact!.join("?").gsub("&amp;", "&")
119
+ # sub correct ampersand after rails switches it (http://dev.rubyonrails.org/ticket/4002)
120
+ else
121
+ return nil
122
+ end
123
+ end
124
+
125
+ def textify_images(email_body)
126
+ email_body.gsub(%r{<img[^>]*alt=['"]?([^'"]*)['"]?[^>]*?/>}) { $1 }
127
+ end
128
+
129
+ def parse_email_count(amount)
130
+ case amount
131
+ when "no"
132
+ 0
133
+ when "an"
134
+ 1
135
+ else
136
+ amount.to_i
137
+ end
138
+ end
139
+
140
+ attr_reader :last_email_address
141
+
142
+ def convert_address(address)
143
+ @last_email_address = (address || current_email_address)
144
+ AddressConverter.instance.convert(@last_email_address)
145
+ end
146
+
147
+
148
+ def mailbox_for(address)
149
+ super(convert_address(address)) # super resides in Deliveries
150
+ end
151
+
152
+ def email_spec_deprecate(text)
153
+ puts ""
154
+ puts "DEPRECATION: #{text.split.join(' ')}"
155
+ puts ""
156
+ end
157
+
158
+ end
159
+ end
160
+
@@ -0,0 +1,217 @@
1
+ module EmailSpec
2
+ module Matchers
3
+ class ReplyTo
4
+ def initialize(email)
5
+ @expected_reply_to = TMail::Address.parse(email)
6
+ end
7
+
8
+ def description
9
+ "have reply to as #{@expected_reply_to.address.to_s}"
10
+ end
11
+
12
+ def matches?(email)
13
+ @email = email
14
+ @actual_reply_to = (email.reply_to || []).first
15
+ !@actual_reply_to.nil? &&
16
+ @actual_reply_to == @expected_reply_to.address
17
+ end
18
+
19
+ def failure_message
20
+ "expected #{@email.inspect} to reply to #{@expected_reply_to.address.inspect}, but it replied to #{@actual_reply_to.inspect}"
21
+ end
22
+
23
+ def negative_failure_message
24
+ "expected #{@email.inspect} not to deliver to #{@expected_reply_to.inspect}, but it did"
25
+ end
26
+ end
27
+
28
+ def reply_to(email)
29
+ ReplyTo.new(email)
30
+ end
31
+
32
+ alias :have_reply_to :reply_to
33
+
34
+ class DeliverTo
35
+ def initialize(expected_email_addresses_or_objects_that_respond_to_email)
36
+ emails = expected_email_addresses_or_objects_that_respond_to_email.map do |email_or_object|
37
+ email_or_object.kind_of?(String) ? email_or_object : email_or_object.email
38
+ end
39
+
40
+ @expected_email_addresses = emails.sort
41
+ end
42
+
43
+ def description
44
+ "be delivered to #{@expected_email_addresses.inspect}"
45
+ end
46
+
47
+ def matches?(email)
48
+ @email = email
49
+ @actual_recipients = (Array(email.to) || []).sort
50
+ @actual_recipients == @expected_email_addresses
51
+ end
52
+
53
+ def failure_message
54
+ "expected #{@email.inspect} to deliver to #{@expected_email_addresses.inspect}, but it delivered to #{@actual_recipients.inspect}"
55
+ end
56
+
57
+ def negative_failure_message
58
+ "expected #{@email.inspect} not to deliver to #{@expected_email_addresses.inspect}, but it did"
59
+ end
60
+ end
61
+
62
+ def deliver_to(*expected_email_addresses_or_objects_that_respond_to_email)
63
+ DeliverTo.new(expected_email_addresses_or_objects_that_respond_to_email.flatten)
64
+ end
65
+
66
+ alias :be_delivered_to :deliver_to
67
+
68
+ class DeliverFrom
69
+
70
+ def initialize(email)
71
+ @expected_sender = TMail::Address.parse(email)
72
+ end
73
+
74
+ def description
75
+ "be delivered from #{@expected_sender.to_s}"
76
+ end
77
+
78
+ def matches?(email)
79
+ @email = email
80
+ @actual_sender = (email.from_addrs || []).first
81
+
82
+ !@actual_sender.nil? &&
83
+ @actual_sender.address == @expected_sender.address &&
84
+ @actual_sender.name == @expected_sender.name
85
+ end
86
+
87
+ def failure_message
88
+ %(expected #{@email.inspect} to deliver from "#{@expected_sender.to_s}", but it delivered from "#{@actual_sender.to_s}")
89
+ end
90
+
91
+ def negative_failure_message
92
+ %(expected #{@email.inspect} not to deliver from "#{@expected_sender.to_s}", but it did)
93
+ end
94
+ end
95
+
96
+ def deliver_from(email)
97
+ DeliverFrom.new(email)
98
+ end
99
+
100
+ alias :be_delivered_from :deliver_from
101
+
102
+ class BccTo
103
+
104
+ def initialize(expected_email_addresses_or_objects_that_respond_to_email)
105
+ emails = expected_email_addresses_or_objects_that_respond_to_email.map do |email_or_object|
106
+ email_or_object.kind_of?(String) ? email_or_object : email_or_object.email
107
+ end
108
+
109
+ @expected_email_addresses = emails.sort
110
+ end
111
+
112
+ def description
113
+ "be bcc'd to #{@expected_email_addresses.inspect}"
114
+ end
115
+
116
+ def matches?(email)
117
+ @email = email
118
+ @actual_recipients = (Array(email.bcc) || []).sort
119
+ @actual_recipients == @expected_email_addresses
120
+ end
121
+
122
+ def failure_message
123
+ "expected #{@email.inspect} to bcc to #{@expected_email_addresses.inspect}, but it was bcc'd to #{@actual_recipients.inspect}"
124
+ end
125
+
126
+ def negative_failure_message
127
+ "expected #{@email.inspect} not to bcc to #{@expected_email_addresses.inspect}, but it did"
128
+ end
129
+ end
130
+
131
+ def bcc_to(*expected_email_addresses_or_objects_that_respond_to_email)
132
+ BccTo.new(expected_email_addresses_or_objects_that_respond_to_email.flatten)
133
+ end
134
+
135
+ def have_subject(expected)
136
+ simple_matcher do |given, matcher|
137
+ given_subject = given.subject
138
+
139
+ if expected.is_a?(String)
140
+ matcher.description = "have subject of #{expected.inspect}"
141
+ matcher.failure_message = "expected the subject to be #{expected.inspect} but was #{given_subject.inspect}"
142
+ matcher.negative_failure_message = "expected the subject not to be #{expected.inspect} but was"
143
+
144
+ given_subject == expected
145
+ else
146
+ matcher.description = "have subject matching #{expected.inspect}"
147
+ matcher.failure_message = "expected the subject to match #{expected.inspect}, but did not. Actual subject was: #{given_subject.inspect}"
148
+ matcher.negative_failure_message = "expected the subject not to match #{expected.inspect} but #{given_subject.inspect} does match it."
149
+
150
+ !!(given_subject =~ expected)
151
+ end
152
+ end
153
+ end
154
+
155
+ def include_email_with_subject(expected)
156
+ simple_matcher do |given_emails, matcher|
157
+
158
+ if expected.is_a?(String)
159
+ matcher.description = "include email with subject of #{expected.inspect}"
160
+ matcher.failure_message = "expected at least one email to have the subject #{expected.inspect} but none did. Subjects were #{given_emails.map(&:subject).inspect}"
161
+ matcher.negative_failure_message = "expected no email with the subject #{expected.inspect} but found at least one. Subjects were #{given_emails.map(&:subject).inspect}"
162
+
163
+ given_emails.map(&:subject).include?(expected)
164
+ else
165
+ matcher.description = "include email with subject matching #{expected.inspect}"
166
+ 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}"
167
+ 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}"
168
+
169
+ !!(given_emails.any?{ |mail| mail.subject =~ expected })
170
+ end
171
+ end
172
+ end
173
+
174
+ def have_body_text(expected)
175
+ simple_matcher do |given, matcher|
176
+
177
+ if expected.is_a?(String)
178
+ normalized_body = given.body.gsub(/\s+/, " ")
179
+ normalized_expected = expected.gsub(/\s+/, " ")
180
+ matcher.description = "have body including #{normalized_expected.inspect}"
181
+ matcher.failure_message = "expected the body to contain #{normalized_expected.inspect} but was #{normalized_body.inspect}"
182
+ matcher.negative_failure_message = "expected the body not to contain #{normalized_expected.inspect} but was #{normalized_body.inspect}"
183
+
184
+ normalized_body.include?(normalized_expected)
185
+ else
186
+ given_body = given.body
187
+ matcher.description = "have body matching #{expected.inspect}"
188
+ matcher.failure_message = "expected the body to match #{expected.inspect}, but did not. Actual body was: #{given_body.inspect}"
189
+ matcher.negative_failure_message = "expected the body not to match #{expected.inspect} but #{given_body.inspect} does match it."
190
+
191
+ !!(given_body =~ expected)
192
+ end
193
+ end
194
+ end
195
+
196
+ def have_header(expected_name, expected_value)
197
+ simple_matcher do |given, matcher|
198
+ given_header = given.header
199
+
200
+ if expected_value.is_a?(String)
201
+ matcher.description = "have header #{expected_name}: #{expected_value}"
202
+ matcher.failure_message = "expected the headers to include '#{expected_name}: #{expected_value}' but they were #{given_header.inspect}"
203
+ matcher.negative_failure_message = "expected the headers not to include '#{expected_name}: #{expected_value}' but they were #{given_header.inspect}"
204
+
205
+ given_header[expected_name].to_s == expected_value
206
+ else
207
+ matcher.description = "have header #{expected_name} with value matching #{expected_value.inspect}"
208
+ matcher.failure_message = "expected the headers to include '#{expected_name}' with a value matching #{expected_value.inspect} but they were #{given_header.inspect}"
209
+ 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}"
210
+
211
+ given_header[expected_name].to_s =~ expected_value
212
+ end
213
+ end
214
+ end
215
+
216
+ end
217
+ end