Skip to content

Commit

Permalink
be_delivered_from matcher now compares both sender name and email add…
Browse files Browse the repository at this point in the history
…ress
  • Loading branch information
archfear committed Jan 5, 2010
1 parent d742a02 commit 6c50c68
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 11 deletions.
15 changes: 9 additions & 6 deletions lib/email_spec/matchers.rb
Expand Up @@ -40,25 +40,28 @@ def deliver_to(*expected_email_addresses_or_objects_that_respond_to_email)
class DeliverFrom

def initialize(email)
@expected_email_addresses = email
@expected_sender = TMail::Address.parse(email)
end

def description
"be delivered from #{@expected_email_addresses.inspect}"
"be delivered from #{@expected_sender.to_s}"
end

def matches?(email)
@email = email
@actual_sender = (email.from || []).first
@actual_sender.eql? @expected_email_addresses
@actual_sender = (email.from_addrs || []).first

!@actual_sender.nil? &&
@actual_sender.address == @expected_sender.address &&
@actual_sender.name == @expected_sender.name
end

def failure_message
"expected #{@email.inspect} to deliver from #{@expected_email_addresses.inspect}, but it delivered from #{@actual_sender.inspect}"
%(expected #{@email.inspect} to deliver from "#{@expected_sender.to_s}", but it delivered from "#{@actual_sender.to_s}")
end

def negative_failure_message
"expected #{@email.inspect} not to deliver from #{@expected_email_addresses.inspect}, but it did"
%(expected #{@email.inspect} not to deliver from "#{@expected_sender.to_s}", but it did)
end
end

Expand Down
30 changes: 25 additions & 5 deletions spec/email_spec/matchers_spec.rb
Expand Up @@ -80,19 +80,39 @@ def mock_email(stubs)
end

describe "#deliver_from" do
it "should match when the email is set to deliver from the specidied address" do
email = mock_email(:from => ["jimmy_bean@yahoo.com"])
it "should match when the email is set to deliver from the specified address" do
email = mock_email(:from_addrs => [TMail::Address.parse("jimmy_bean@yahoo.com")])
deliver_from("jimmy_bean@yahoo.com").should match(email)
end

it "should match when the email is set to deliver from the specified name and address" do
email = mock_email(:from_addrs => [TMail::Address.parse("Jimmy Bean <jimmy_bean@yahoo.com>")])
deliver_from("Jimmy Bean <jimmy_bean@yahoo.com>").should match(email)
end

it "should not match when the email is not set to deliver from the specified address" do
email = mock_email(:from => nil)
it "should not match when the email does not have a sender" do
email = mock_email(:from_addrs => nil)
deliver_from("jimmy_bean@yahoo.com").should_not match(email)
end

it "should not match when the email addresses match but the names do not" do
email = mock_email(:from_addrs => [TMail::Address.parse("Jimmy Bean <jimmy_bean@yahoo.com>")])
deliver_from("Freddy Noe <jimmy_bean@yahoo.com>").should_not match(email)
end

it "should not match when the names match but the email addresses do not" do
email = mock_email(:from_addrs => [TMail::Address.parse("Jimmy Bean <jimmy_bean@yahoo.com>")])
deliver_from("Jimmy Bean <freddy_noe@yahoo.com>").should_not match(email)
end

it "should not match when the email is not set to deliver from the specified address" do
email = mock_email(:from_addrs => [TMail::Address.parse("freddy_noe@yahoo.com")])
deliver_from("jimmy_bean@yahoo.com").should_not match(email)
end

it "should give correct failure message when the email is not set to deliver from the specified address" do
matcher = deliver_from("jimmy_bean@yahoo.com")
matcher.matches?(mock_email(:inspect => 'email', :from => ['freddy_noe@yahoo.com']))
matcher.matches?(mock_email(:inspect => 'email', :from_addrs => [TMail::Address.parse("freddy_noe@yahoo.com")]))
matcher.failure_message.should == %{expected email to deliver from "jimmy_bean@yahoo.com", but it delivered from "freddy_noe@yahoo.com"}
end

Expand Down

0 comments on commit 6c50c68

Please sign in to comment.