lib/email_spec/matchers.rb in email_spec-1.1.1 vs lib/email_spec/matchers.rb in email_spec-1.2.0

- old
+ new

@@ -35,29 +35,29 @@ def initialize(expected_email_addresses_or_objects_that_respond_to_email) emails = expected_email_addresses_or_objects_that_respond_to_email.map do |email_or_object| email_or_object.kind_of?(String) ? email_or_object : email_or_object.email end - @expected_email_addresses = emails.sort + @expected_recipients = Mail::ToField.new(emails).addrs.map(&:to_s).sort end def description - "be delivered to #{@expected_email_addresses.inspect}" + "be delivered to #{@expected_recipients.inspect}" end def matches?(email) @email = email - @actual_recipients = (Array(email.to) || []).sort - @actual_recipients == @expected_email_addresses + @actual_recipients = (email.header[:to].addrs || []).map(&:to_s).sort + @actual_recipients == @expected_recipients end def failure_message - "expected #{@email.inspect} to deliver to #{@expected_email_addresses.inspect}, but it delivered to #{@actual_recipients.inspect}" + "expected #{@email.inspect} to deliver to #{@expected_recipients.inspect}, but it delivered to #{@actual_recipients.inspect}" end def negative_failure_message - "expected #{@email.inspect} not to deliver to #{@expected_email_addresses.inspect}, but it did" + "expected #{@email.inspect} not to deliver to #{@expected_recipients.inspect}, but it did" end end def deliver_to(*expected_email_addresses_or_objects_that_respond_to_email) DeliverTo.new(expected_email_addresses_or_objects_that_respond_to_email.flatten) @@ -78,12 +78,11 @@ def matches?(email) @email = email @actual_sender = (email.header[:from].addrs || []).first !@actual_sender.nil? && - @actual_sender.address == @expected_sender.address && - @actual_sender.display_name == @expected_sender.display_name + @actual_sender.to_s == @expected_sender.to_s end def failure_message %(expected #{@email.inspect} to deliver from "#{@expected_sender.to_s}", but it delivered from "#{@actual_sender.to_s}") end @@ -128,9 +127,42 @@ end end def bcc_to(*expected_email_addresses_or_objects_that_respond_to_email) BccTo.new(expected_email_addresses_or_objects_that_respond_to_email.flatten) + end + + class CcTo + + def initialize(expected_email_addresses_or_objects_that_respond_to_email) + emails = expected_email_addresses_or_objects_that_respond_to_email.map do |email_or_object| + email_or_object.kind_of?(String) ? email_or_object : email_or_object.email + end + + @expected_email_addresses = emails.sort + end + + def description + "be cc'd to #{@expected_email_addresses.inspect}" + end + + def matches?(email) + @email = email + @actual_recipients = (Array(email.cc) || []).sort + @actual_recipients == @expected_email_addresses + end + + def failure_message + "expected #{@email.inspect} to cc to #{@expected_email_addresses.inspect}, but it was cc'd to #{@actual_recipients.inspect}" + end + + def negative_failure_message + "expected #{@email.inspect} not to cc to #{@expected_email_addresses.inspect}, but it did" + end + end + + def cc_to(*expected_email_addresses_or_objects_that_respond_to_email) + CcTo.new(expected_email_addresses_or_objects_that_respond_to_email.flatten) end RSpec::Matchers.define :have_subject do match do |given| given_subject = given.subject