Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

added html_part and text_part matcher #664

Open
wants to merge 1 commit into from

1 participant

Akihiro YAMAZAKI
Akihiro YAMAZAKI

add convenient method for testing html-mail

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Feb 6, 2014
  1. added html_part and text_part matcher

    Akihiro Yamazaki authored
This page is out of date. Refresh to see the latest.
Showing with 70 additions and 1 deletion.
  1. +19 −1 lib/mail/matchers/has_sent_mail.rb
  2. +51 −0 spec/matchers_spec.rb
20 lib/mail/matchers/has_sent_mail.rb
View
@@ -72,6 +72,16 @@ def matching_body(body_matcher)
self
end
+ def with_html_part_body(body)
+ @html_part_body = body
+ self
+ end
+
+ def with_text_part_body(body)
+ @text_part_body = body
+ self
+ end
+
def description
result = "send a matching email"
result
@@ -96,7 +106,7 @@ def negative_failure_message
def filter_matched_deliveries(deliveries)
candidate_deliveries = deliveries
- %w(sender recipients copy_recipients blind_copy_recipients subject subject_matcher body body_matcher).each do |modifier_name|
+ %w(sender recipients copy_recipients blind_copy_recipients subject subject_matcher body body_matcher html_part_body text_part_body).each do |modifier_name|
next unless instance_variable_defined?("@#{modifier_name}")
candidate_deliveries = candidate_deliveries.select{|matching_delivery| self.send("matches_on_#{modifier_name}?", matching_delivery)}
end
@@ -136,6 +146,14 @@ def matches_on_body_matcher?(delivery)
@body_matcher.match delivery.body.raw_source
end
+ def matches_on_html_part_body?(delivery)
+ delivery.html_part.body == @html_part_body
+ end
+
+ def matches_on_text_part_body?(delivery)
+ delivery.text_part.body == @text_part_body
+ end
+
def explain_expectations
result = ''
result += "from #{@sender} " if instance_variable_defined?('@sender')
51 spec/matchers_spec.rb
View
@@ -14,6 +14,23 @@ def send_test_email
end
end
+ def send_test_html_mail
+ Mail.deliver do
+ from 'phil@example.com'
+ to ['bob@example.com', 'fred@example.com']
+ cc ['dad@example.com', 'mom@example.com']
+ bcc ['alice@example.com', 'sue@example.com']
+ subject 'The facts you requested'
+ text_part do
+ body 'Here is the text-part'
+ end
+ html_part do
+ content_type 'text/html; charset=UTF-8'
+ body 'Here is the html-part'
+ end
+ end
+ end
+
before(:all) do
$old_delivery_method = Mail.delivery_method
@@ -155,4 +172,38 @@ def send_test_email
matching_body(/(I|you) request/)
end
end
+
+ context "with html-mail" do
+ before(:each) do
+ Mail::TestMailer.deliveries.clear
+ Mail::TestMailer.deliveries.should be_empty
+ end
+
+ context "when html-mail has been sent" do
+ before(:each) do
+ send_test_html_mail
+ Mail::TestMailer.deliveries.should_not be_empty
+ end
+
+ context "with #html_part" do
+ context "and a matching html_part.body" do
+ it { should have_sent_email.with_html_part_body('Here is the html-part') }
+ end
+
+ context "and a non-matching html_part.body" do
+ it { should_not have_sent_email.with_html_part_body('Here is the body') }
+ end
+ end
+
+ context "with #text_part" do
+ context "and a matching text_part.body" do
+ it { should have_sent_email.with_text_part_body('Here is the text-part') }
+ end
+
+ context "and a non-matching text_part.body" do
+ it { should_not have_sent_email.with_text_part_body('Here is the subject') }
+ end
+ end
+ end
+ end
end
Something went wrong with that request. Please try again.