Skip to content

Commit

Permalink
fix action mailer test method
Browse files Browse the repository at this point in the history
  • Loading branch information
Chad Pytel committed Jun 11, 2010
1 parent 6989256 commit 832f46b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 18 deletions.
14 changes: 8 additions & 6 deletions lib/shoulda/action_mailer/matchers/have_sent_email.rb
Expand Up @@ -32,13 +32,15 @@ def to(recipient)
end

def matches?(subject)
@mail = subject
@subject_failed = !regexp_or_string_match(@mail.subject, @email_subject) if @email_subject
@body_failed = !regexp_or_string_match(@mail.body, @body) if @body
@sender_failed = !regexp_or_string_match_in_array(@mail.from, @sender) if @sender
@recipient_failed = !regexp_or_string_match_in_array(@mail.to, @recipient) if @recipient
::ActionMailer::Base.deliveries.each do |mail|
@subject_failed = !regexp_or_string_match(mail.subject, @email_subject) if @email_subject
@body_failed = !regexp_or_string_match(mail.body, @body) if @body
@sender_failed = !regexp_or_string_match_in_array(mail.from, @sender) if @sender
@recipient_failed = !regexp_or_string_match_in_array(mail.to, @recipient) if @recipient
return true unless anything_failed?
end

!anything_failed?
false
end

def failure_message
Expand Down
24 changes: 12 additions & 12 deletions test/matchers/action_mailer/have_sent_email_test.rb
Expand Up @@ -19,35 +19,35 @@ def the_email
end
end
if defined?(AbstractController::Rendering)
@mail = Mailer.the_email
::ActionMailer::Base.deliveries << Mailer.the_email
else
@mail = Mailer.create_the_email
::ActionMailer::Base.deliveries << Mailer.create_the_email
end
end

should "accept based on the subject" do
assert_accepts have_sent_email.with_subject(/is spam$/), @mail
assert_rejects have_sent_email.with_subject(/totally safe/), @mail
assert_accepts have_sent_email.with_subject(/is spam$/), nil
assert_rejects have_sent_email.with_subject(/totally safe/), nil
end

should "accept based on the sender" do
assert_accepts have_sent_email.from('do-not-reply@example.com'), @mail
assert_rejects have_sent_email.from('you@example.com'), @mail
assert_accepts have_sent_email.from('do-not-reply@example.com'), nil
assert_rejects have_sent_email.from('you@example.com'), nil
end

should "accept based on the body" do
assert_accepts have_sent_email.with_body(/is spam\./), @mail
assert_rejects have_sent_email.with_body(/totally safe/), @mail
assert_accepts have_sent_email.with_body(/is spam\./), nil
assert_rejects have_sent_email.with_body(/totally safe/), nil
end

should "accept baed on the recipienct" do
assert_accepts have_sent_email.to('myself@me.com'), @mail
assert_rejects have_sent_email.to('you@example.com'), @mail
assert_accepts have_sent_email.to('myself@me.com'), nil
assert_rejects have_sent_email.to('you@example.com'), nil
end

should "chain" do
assert_accepts have_sent_email.with_subject(/spam/).from('do-not-reply@example.com').with_body(/spam/).to('myself@me.com'), @mail
assert_rejects have_sent_email.with_subject(/ham/).from('you@example.com').with_body(/ham/).to('them@example.com'), @mail
assert_accepts have_sent_email.with_subject(/spam/).from('do-not-reply@example.com').with_body(/spam/).to('myself@me.com'), nil
assert_rejects have_sent_email.with_subject(/ham/).from('you@example.com').with_body(/ham/).to('them@example.com'), nil
end
end
end

0 comments on commit 832f46b

Please sign in to comment.