Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Remove 'have_sent_email' matcher

* See issue #252
  • Loading branch information...
commit 6b105ba7cd867eed341259c672d50b0109697153 1 parent 7e7fb14
Melissa Xie mxie authored mxie committed
22 lib/shoulda/matchers/action_mailer.rb
View
@@ -1,22 +0,0 @@
-require 'shoulda/matchers/action_mailer/have_sent_email_matcher'
-
-module Shoulda
- module Matchers
- # = Matchers for your mailers
- #
- # This matcher will test that email is sent properly
- #
- # describe User do
- # it { should have_sent_email.with_subject(/is spam$/) }
- # it { should have_sent_email.from('do-not-reply@example.com') }
- # it { should have_sent_email.with_body(/is spam\./) }
- # it { should have_sent_email.to('myself@me.com') }
- # it { should have_sent_email.with_subject(/spam/).
- # from('do-not-reply@example.com').
- # with_body(/spam/).
- # to('myself@me.com') }
- # end
- module ActionMailer
- end
- end
-end
260 lib/shoulda/matchers/action_mailer/have_sent_email_matcher.rb
View
@@ -1,260 +0,0 @@
-require 'active_support/deprecation'
-
-module Shoulda # :nodoc:
- module Matchers
- module ActionMailer # :nodoc:
-
- # The right email is sent.
- #
- # it { should have_sent_email.with_subject(/is spam$/) }
- # it { should have_sent_email.from('do-not-reply@example.com') }
- # it { should have_sent_email.with_body(/is spam\./) }
- # it { should have_sent_email.to('myself@me.com') }
- # it { should have_sent_email.with_part('text/html', /HTML spam/) }
- # it { should have_sent_email.with_subject(/spam/).
- # from('do-not-reply@example.com').
- # reply_to('reply-to-me@example.com').
- # with_body(/spam/).
- # to('myself@me.com') }
- #
- # Use values of instance variables
- # it {should have_sent_email.to {@user.email} }
- def have_sent_email
- HaveSentEmailMatcher.new(self)
- end
-
- class HaveSentEmailMatcher # :nodoc:
-
- def initialize(context)
- ActiveSupport::Deprecation.warn 'The have_sent_email matcher is deprecated and will be removed in 2.0'
- @context = context
- end
-
- def in_context(context)
- @context = context
- self
- end
-
- def with_subject(email_subject = nil, &block)
- @email_subject = email_subject
- @email_subject_block = block
- self
- end
-
- def from(sender = nil, &block)
- @sender = sender
- @sender_block = block
- self
- end
-
- def reply_to(reply_to = nil, &block)
- @reply_to = reply_to
- @reply_to_block = block
- self
- end
-
- def with_body(body = nil, &block)
- @body = body
- @body_block = block
- self
- end
-
- def with_part(content_type, body = nil, &block)
- @parts ||= []
- @parts << [/#{Regexp.escape(content_type)}/, body, content_type]
- @parts_blocks ||= []
- @parts_blocks << block
- self
- end
-
- def to(recipient = nil, &block)
- @recipient = recipient
- @recipient_block = block
- self
- end
-
- def cc(recipient = nil, &block)
- @cc = recipient
- @cc_block = block
- self
- end
-
- def with_cc(recipients = nil, &block)
- @cc_recipients = recipients
- @cc_recipients_block = block
- self
- end
-
- def bcc(recipient = nil, &block)
- @bcc = recipient
- @bcc_block = block
- self
- end
-
- def with_bcc(recipients = nil, &block)
- @bcc_recipients = recipients
- @bcc_recipients_block = block
- self
- end
-
- def multipart(flag = true)
- @multipart = !!flag
- self
- end
-
- def matches?(subject)
- normalize_blocks
- ::ActionMailer::Base.deliveries.any? do |mail|
- mail_matches?(mail)
- end
- end
-
- def failure_message_for_should
- "Expected #{expectation}"
- end
-
- def failure_message_for_should_not
- "Did not expect #{expectation}"
- end
-
- def description
- description = 'send an email'
- description << " with a subject of #{@email_subject.inspect}" if @email_subject
- description << " containing #{@body.inspect}" if @body
- if @parts
- @parts.each do |_, body, content_type|
- description << " having a #{content_type} part containing #{body.inspect}"
- end
- end
- description << " from #{@sender.inspect}" if @sender
- description << " reply to #{@reply_to.inspect}" if @reply_to
- description << " to #{@recipient.inspect}" if @recipient
- description << " cc #{@cc.inspect}" if @cc
- description << " with cc #{@cc_recipients.inspect}" if @cc_recipients
- description << " bcc #{@bcc.inspect}" if @bcc
- description << " with bcc #{@bcc_recipients.inspect}" if @bcc_recipients
- description
- end
-
- private
-
- def expectation
- expectation = 'sent email'
- expectation << " with subject #{@email_subject.inspect}" if @subject_failed
- expectation << " with body #{@body.inspect}" if @body_failed
- @parts.each do |_, body, content_type|
- expectation << " with a #{content_type} part containing #{body}"
- end if @parts && @parts_failed
- expectation << " from #{@sender.inspect}" if @sender_failed
- expectation << " reply to #{@reply_to.inspect}" if @reply_to_failed
- expectation << " to #{@recipient.inspect}" if @recipient_failed
- expectation << " cc #{@cc.inspect}" if @cc_failed
- expectation << " with cc #{@cc_recipients.inspect}" if @cc_recipients_failed
- expectation << " bcc #{@bcc.inspect}" if @bcc_failed
- expectation << " with bcc #{@bcc_recipients.inspect}" if @bcc_recipients_failed
- expectation << " #{'not ' if !@multipart}being multipart" if @multipart_failed
- expectation << "\nDeliveries:\n#{inspect_deliveries}"
- end
-
- def inspect_deliveries
- ::ActionMailer::Base.deliveries.map do |delivery|
- "#{delivery.subject.inspect} to #{delivery.to.inspect}"
- end.join("\n")
- end
-
- def mail_matches?(mail)
- @subject_failed = !regexp_or_string_match(mail.subject, @email_subject) if @email_subject
- @parts_failed = !parts_match(mail, @parts) if @parts
- @body_failed = !body_match(mail, @body) if @body
- @sender_failed = !regexp_or_string_match_in_array(mail.from, @sender) if @sender
- @reply_to_failed = !regexp_or_string_match_in_array(mail.reply_to, @reply_to) if @reply_to
- @recipient_failed = !regexp_or_string_match_in_array(mail.to, @recipient) if @recipient
- @cc_failed = !regexp_or_string_match_in_array(mail.cc, @cc) if @cc
- @cc_recipients_failed = !match_array_in_array(mail.cc, @cc_recipients) if @cc_recipients
- @bcc_failed = !regexp_or_string_match_in_array(mail.bcc, @bcc) if @bcc
- @bcc_recipients_failed = !match_array_in_array(mail.bcc, @bcc_recipients) if @bcc_recipients
- @multipart_failed = (mail.multipart? != @multipart) if defined?(@multipart)
-
- ! anything_failed?
- end
-
- def anything_failed?
- @subject_failed || @body_failed || @sender_failed || @reply_to_failed ||
- @recipient_failed || @cc_failed || @cc_recipients_failed || @bcc_failed ||
- @bcc_recipients_failed || @parts_failed || @multipart_failed
- end
-
- def normalize_blocks
- @email_subject = @context.instance_eval(&@email_subject_block) if @email_subject_block
- @sender = @context.instance_eval(&@sender_block) if @sender_block
- @reply_to = @context.instance_eval(&@reply_to_block) if @reply_to_block
- @body = @context.instance_eval(&@body_block) if @body_block
- @recipient = @context.instance_eval(&@recipient_block) if @recipient_block
- @cc = @context.instance_eval(&@cc_block) if @cc_block
- @cc_recipients = @context.instance_eval(&@cc_recipients_block) if @cc_recipients_block
- @bcc = @context.instance_eval(&@bcc_block) if @bcc_block
- @bcc_recipients = @context.instance_eval(&@bcc_recipients_block) if @bcc_recipients_block
-
- if @parts
- @parts.each_with_index do |part, i|
- part[1] = @context.instance_eval(&@parts_blocks[i]) if @parts_blocks[i]
- end
- end
- end
-
- def regexp_or_string_match(a_string, a_regexp_or_string)
- case a_regexp_or_string
- when Regexp
- a_string =~ a_regexp_or_string
- when String
- a_string == a_regexp_or_string
- end
- end
-
- def regexp_or_string_match_in_array(an_array, a_regexp_or_string)
- case a_regexp_or_string
- when Regexp
- an_array.any? { |string| string =~ a_regexp_or_string }
- when String
- an_array.include?(a_regexp_or_string)
- end
- end
-
- def match_array_in_array(target_array, match_array)
- target_array.sort!
- match_array.sort!
-
- target_array.each_cons(match_array.size).include?(match_array)
- end
-
- def body_match(mail, a_regexp_or_string)
- # Mail objects instantiated by ActionMailer3 return a blank
- # body if the e-mail is multipart. TMail concatenates the
- # String representation of each part instead.
- if mail.body.blank? && mail.multipart?
- part_match(mail, /^text\//, a_regexp_or_string)
- else
- regexp_or_string_match(mail.body, a_regexp_or_string)
- end
- end
-
- def parts_match(mail, parts)
- return false if mail.parts.empty?
-
- parts.all? do |content_type, match, _|
- part_match(mail, content_type, match)
- end
- end
-
- def part_match(mail, content_type, a_regexp_or_string)
- matching = mail.parts.select {|p| p.content_type =~ content_type}
- return false if matching.empty?
-
- matching.all? do |part|
- regexp_or_string_match(part.body, a_regexp_or_string)
- end
- end
- end
- end
- end
-end
5 lib/shoulda/matchers/integrations/rspec.rb
View
@@ -17,9 +17,4 @@
require 'shoulda/matchers/action_controller'
config.include Shoulda::Matchers::ActionController
end
-
- if defined?(::ActionMailer)
- require 'shoulda/matchers/action_mailer'
- config.include Shoulda::Matchers::ActionMailer
- end
end
13 lib/shoulda/matchers/integrations/test_unit.rb
View
@@ -14,19 +14,6 @@ def subject
end
end
-if defined?(ActionMailer)
- require 'shoulda/matchers/action_mailer'
-
- module Test
- module Unit
- class TestCase
- include Shoulda::Matchers::ActionMailer
- extend Shoulda::Matchers::ActionMailer
- end
- end
- end
-end
-
if defined?(ActiveRecord)
require 'shoulda/matchers/active_record'
require 'shoulda/matchers/active_model'
324 spec/shoulda/matchers/action_mailer/have_sent_email_spec.rb
View
@@ -1,324 +0,0 @@
-require 'spec_helper'
-
-describe Shoulda::Matchers::ActionMailer::HaveSentEmailMatcher do
- subject { described_class.new(self) }
- after { ::ActionMailer::Base.deliveries.clear }
-
- def add_mail_to_deliveries(params = nil)
- ::ActionMailer::Base.deliveries << Mailer.the_email(params)
- end
-
- context 'testing with instance variables with no multipart' do
- let(:info) do
- {
- :from => 'do-not-reply@example.com',
- :reply_to => 'reply-to-me@example.com',
- :to => 'myself@me.com',
- :cc => ['you@you.com', 'joe@bob.com', 'hello@goodbye.com'],
- :bcc => ['test@example.com', 'sam@bob.com', 'goodbye@hello.com'],
- :subject => 'This is spam',
- :body => 'Every email is spam.'
- }
- end
-
- before do
- define_mailer(:mailer, [:the_email]) do
- def the_email(params)
- mail params
- end
- end
- add_mail_to_deliveries(info)
- end
-
- it 'sends an e-mail based on subject' do
- should have_sent_email.with_subject{ info[:subject] }
- end
-
- it 'sends an e-mail based on recipient' do
- should have_sent_email.to(nil) { info[:to] }
- end
-
- it 'sends an e-mail based on sender' do
- should have_sent_email.from{ info[:from] }
- end
-
- it 'sends an e-email based on reply_to' do
- should have_sent_email.reply_to { info[:reply_to] }
- end
-
- it 'sends an e-mail based on cc' do
- should have_sent_email.cc{ info[:cc][0] }
- end
-
- it 'sends an e-mail based on cc list' do
- should have_sent_email.with_cc{ info[:cc] }
- end
-
- it 'sends an e-mail based on bcc' do
- should have_sent_email.bcc{ info[:bcc][0] }
- end
-
- it 'sends an e-mail based on bcc list' do
- should have_sent_email.with_bcc{ info[:bcc] }
- end
-
- it 'sends an e-mail based on body' do
- should have_sent_email.with_body{ info[:body] }
- end
- end
-
- context 'testing with instance variables with multiple parts' do
- let(:info) do
- {
- :from => 'do-not-reply@example.com',
- :to => 'myself@me.com',
- :cc => ['you@you.com', 'joe@bob.com', 'hello@goodbye.com'],
- :bcc => ['test@example.com', 'sam@bob.com', 'goodbye@hello.com'],
- :subject => 'This is spam',
- :text => 'Every email is spam.',
- :html => '<h1>HTML is spam.</h1><p>Notably.</p>'
- }
- end
-
- before do
- define_mailer(:mailer, [:the_email]) do
- def the_email(params)
- mail params do |format|
- format.text { render :text => params[:text] }
- format.html { render :text => params[:html] }
- end
- end
- end
- add_mail_to_deliveries(info)
- end
-
- it 'sends emails with text and html parts' do
- should have_sent_email.with_part('text/plain') { info[:text] }.with_part('text/html') { info[:html] }
- end
-
- it 'should have the block override the method argument' do
- should have_sent_email.with_part('text/plain', 'foo') { info[:text] }.with_part('text/html', /bar/) { info[:html] }
- end
- end
-
- context 'an email without multiple parts' do
- before do
- define_mailer :mailer, [:the_email] do
- def the_email(params)
- mail :from => 'do-not-reply@example.com',
- :to => 'myself@me.com',
- :subject => 'This is spam',
- :cc => ['you@you.com', 'joe@bob.com', 'hello@goodbye.com'],
- :bcc => ['test@example.com', 'sam@bob.com', 'goodbye@hello.com'],
- :body => 'Every email is spam.'
- end
- end
- add_mail_to_deliveries
- end
-
- it 'accepts sent-email when it is not multipart' do
- should_not have_sent_email.multipart
- matcher = have_sent_email.multipart(true)
- matcher.matches?(Mailer.the_email(nil))
- matcher.failure_message_for_should.should =~ /Expected sent email being multipart/
- end
-
- it 'matches the body with a regexp' do
- should have_sent_email.with_body(/email is spam/)
- end
-
- it 'matches the body with a string' do
- should have_sent_email.with_body('Every email is spam.')
- should_not have_sent_email.with_body('emails is')
- end
- end
-
- context 'an email with both a text/plain and text/html part' do
- before do
- define_mailer :mailer, [:the_email] do
- def the_email(params)
- mail :from => 'do-not-reply@example.com',
- :to => 'myself@me.com',
- :cc => ['you@you.com', 'joe@bob.com', 'hello@goodbye.com'],
- :bcc => ['test@example.com', 'sam@bob.com', 'goodbye@hello.com'],
- :subject => 'This is spam' do |format|
-
- format.text { render :text => 'Every email is spam.' }
- format.html { render :text => '<h1>HTML is spam.</h1><p>Notably.</p>' }
- end
- end
- end
- add_mail_to_deliveries
- end
-
- it 'accepts sent e-mail based on the subject' do
- should have_sent_email.with_subject(/is spam$/)
- matcher = have_sent_email.with_subject(/totally safe/)
- matcher.matches?(nil)
- matcher.failure_message_for_should.should =~ /Expected sent email with subject/
- end
-
- it 'accepts sent e-mail based on a string sender' do
- should have_sent_email.from('do-not-reply@example.com')
- matcher = have_sent_email.from('you@example.com')
- matcher.matches?(nil)
- matcher.failure_message_for_should.should =~ /Expected sent email from/
- end
-
- it 'accepts sent e-mail based on a regexp sender' do
- should have_sent_email.from(/@example\.com/)
- matcher = have_sent_email.from(/you@/)
- matcher.matches?(nil)
- matcher.failure_message_for_should.should =~ /Expected sent email from/
- end
-
- it 'accepts sent e-mail based on the body' do
- should have_sent_email.with_body(/is spam\./)
- matcher = have_sent_email.with_body(/totally safe/)
- matcher.matches?(nil)
- matcher.failure_message_for_should.should =~ /Expected sent email with body/
- end
-
- it 'accepts sent e-mail based on a text/plain part' do
- should have_sent_email.with_part('text/plain', /is spam\./)
- matcher = have_sent_email.with_part('text/plain', /HTML is spam/)
- matcher.matches?(nil)
- matcher.failure_message_for_should.should =~ /Expected sent email with a text\/plain part containing/
- end
-
- it 'accepts sent e-mail based on a text/html part' do
- should have_sent_email.with_part('text/html', /HTML is spam/)
- matcher = have_sent_email.with_part('text/html', /HTML is not spam\./)
- matcher.matches?(nil)
- matcher.failure_message_for_should.should =~ /Expected sent email with a text\/html part containing/
- end
-
- it 'accept sent e-mail based on the recipient' do
- should have_sent_email.to('myself@me.com')
- matcher = have_sent_email.to('you@example.com')
- matcher.matches?(nil)
- matcher.failure_message_for_should.should =~ /Expected sent email to/
- end
-
- it 'accepts sent e-mail based on cc string' do
- should have_sent_email.cc('joe@bob.com')
- matcher = have_sent_email.cc('you@example.com')
- matcher.matches?(nil)
- matcher.failure_message_for_should.should =~ /Expected sent email cc/
- end
-
- it 'accepts sent-email based on cc regex' do
- should have_sent_email.cc(/@bob\.com/)
- matcher = have_sent_email.cc(/us@/)
- matcher.matches?(nil)
- matcher.failure_message_for_should.should =~ /Expected sent email cc/
- end
-
- it 'accepts sent e-mail based on cc list' do
- should have_sent_email.with_cc(['you@you.com', 'joe@bob.com'])
- matcher = have_sent_email.with_cc(['you@example.com'])
- matcher.matches?(nil)
- matcher.failure_message_for_should.should =~ /Expected sent email with cc/
- end
-
- it 'accepts sent e-mail based on bcc string' do
- should have_sent_email.bcc('goodbye@hello.com')
- matcher = have_sent_email.bcc('test@hello.com')
- matcher.matches?(nil)
- matcher.failure_message_for_should.should =~ /Expected sent email bcc/
- end
-
- it 'accepts sent e-mail based on bcc regex' do
- should have_sent_email.bcc(/@example\.com/)
- matcher = have_sent_email.bcc(/you@/)
- matcher.matches?(nil)
- matcher.failure_message_for_should.should =~ /Expected sent email bcc/
- end
-
- it 'accepts sent e-mail based on bcc list' do
- should have_sent_email.with_bcc(['sam@bob.com', 'test@example.com'])
- matcher = have_sent_email.with_bcc(['you@you.com', 'joe@bob.com'])
- matcher.matches?(nil)
- matcher.failure_message_for_should.should =~ /Expected sent email with bcc/
- end
-
- it 'accepts sent-email when it is multipart' do
- should have_sent_email.multipart
- matcher = have_sent_email.multipart(false)
- matcher.matches?(nil)
- matcher.failure_message_for_should.should =~ /Expected sent email not being multipart/
- end
-
- it 'lists all the deliveries within failure message' do
- add_mail_to_deliveries
-
- matcher = have_sent_email.to('you@example.com')
- matcher.matches?(nil)
- matcher.failure_message_for_should.should =~ /Deliveries:\n"This is spam" to \["myself@me\.com"\]\n"This is spam" to \["myself@me\.com"\]/
- end
-
- it 'allows chaining' do
- should have_sent_email.with_subject(/spam/).from('do-not-reply@example.com').with_body(/spam/).
- with_part('text/plain', /is spam\./).with_part('text/html', /HTML is spam/).to('myself@me.com')
- should_not have_sent_email.with_subject(/ham/).from('you@example.com').with_body(/ham/).
- with_part('text/plain', /is ham/).with_part('text/html', /HTML is ham/).to('them@example.com')
- end
- end
-
- context 'testing multiple email deliveries at once' do
- let(:info1) do
- {
- :from => 'do-not-reply@example.com',
- :to => 'one@me.com',
- :subject => 'subject',
- :body => 'body'
- }
- end
-
- let(:info2) do
- {
- :from => 'do-not-reply@example.com',
- :to => 'two@me.com',
- :subject => 'subject',
- :body => 'body'
- }
- end
-
- before do
- define_mailer(:mailer, [:the_email]) do
- def the_email(params)
- mail params
- end
- end
- add_mail_to_deliveries(info1)
- add_mail_to_deliveries(info2)
- end
-
- it 'sends an e-mail based on recipient 1' do
- should have_sent_email.to('one@me.com')
- end
-
- it 'sends an e-mail based on recipient 2' do
- should have_sent_email.to('two@me.com')
- end
- end
-
- it 'provides a detailed description of the e-mail expected to be sent' do
- matcher = have_sent_email
- matcher.description.should == 'send an email'
- matcher = matcher.with_subject('Welcome!')
- matcher.description.should == %q(send an email with a subject of "Welcome!")
- matcher = matcher.with_body('Welcome, human!')
- matcher.description.should == %q(send an email with a subject of "Welcome!" containing "Welcome, human!")
- matcher = matcher.with_part("text/plain", "plain")
- matcher.description.should == %q(send an email with a subject of "Welcome!" containing "Welcome, human!" having a text/plain part containing "plain")
- matcher = matcher.with_part('text/html', 'html')
- matcher.description.should == %q(send an email with a subject of "Welcome!" containing "Welcome, human!" having a text/plain part containing "plain" having a text/html part containing "html")
- matcher = matcher.from('alien@example.com')
- matcher.description.should == %q(send an email with a subject of "Welcome!" containing "Welcome, human!" having a text/plain part containing "plain" having a text/html part containing "html" from "alien@example.com")
- matcher = matcher.to('human@example.com')
- matcher.description.should == %q(send an email with a subject of "Welcome!" containing "Welcome, human!" having a text/plain part containing "plain" having a text/html part containing "html" from "alien@example.com" to "human@example.com")
- matcher = matcher.reply_to('reply-to-me@example.com')
- matcher.description.should == %q(send an email with a subject of "Welcome!" containing "Welcome, human!" having a text/plain part containing "plain" having a text/html part containing "html" from "alien@example.com" reply to "reply-to-me@example.com" to "human@example.com")
- end
-end
2  spec/spec_helper.rb
View
@@ -25,6 +25,4 @@
config.mock_with :mocha
config.include Shoulda::Matchers::ActionController,
:example_group => { :file_path => /action_controller/ }
- config.include Shoulda::Matchers::ActionMailer,
- :example_group => { :file_path => /action_mailer/ }
end
Please sign in to comment.
Something went wrong with that request. Please try again.