Permalink
Browse files

added assert_mail

  • Loading branch information...
alto committed Jun 16, 2012
1 parent df0d2f9 commit 445fa013fd3f7f918f464056f5947ebce440a870
View
@@ -9,7 +9,7 @@ require 'bundler/gem_tasks'
require 'rake/testtask'
desc 'Test the assertions.'
Rake::TestTask.new(:test) do |t|
- t.libs << 'lib'
+ t.libs << 'lib' << 'test'
t.pattern = 'test/**/*_test.rb'
t.verbose = true
end
View
@@ -1,5 +1,6 @@
require "seatbelt/version"
require 'seatbelt/assert_json'
+require 'seatbelt/assert_mail'
# module Seatbelt
# # Your code goes here...
@@ -90,3 +90,7 @@ def raise_error(message)
end
end
end
+
+class Test::Unit::TestCase
+ include Seatbelt::AssertJson
+end
@@ -0,0 +1,48 @@
+module Seatbelt
+ module AssertMail
+
+ def assert_mail(options={}, &block)
+ assert !find_email(options, &block).nil?, "couldn't find the expected mail (#{options.inspect}) in #{ActionMailer::Base.deliveries.inspect}"
+ end
+
+ def assert_no_mail(options={}, &block)
+ assert find_email(options, &block).nil?, "didn't expect mail (#{options.inspect}) in #{ActionMailer::Base.deliveries.inspect}"
+ end
+
+ private
+
+ def find_email(options, &block)
+ if block_given?
+ ActionMailer::Base.deliveries.clear
+ yield
+ end
+ ActionMailer::Base.deliveries.detect do |mail|
+ got_mail?(mail, options)
+ end
+ end
+
+ def got_mail?(mail, options={})
+ return false if options[:to] && !mail.to.include?(options[:to])
+ return false if options[:from] && !mail.from.include?(options[:from])
+ return false if options[:subject] && (mail.subject !~ /#{options[:subject]}/)
+ return false if options[:cc] && !mail.cc.include?(options[:cc])
+ return false if options[:bcc] && !mail.bcc.include?(options[:bcc])
+ if options[:body]
+ Array(options[:body]).each do |element|
+ if !mail.body.encoded.match(element)
+ # puts "#{element} not found in body: #{mail.body.encoded}"
+ return false
+ end
+ end
+ end
+ true
+ end
+
+ end
+end
+
+if Module.const_defined?(:ActionMailer)
+ class Test::Unit::TestCase
+ include Seatbelt::AssertMail
+ end
+end
View
@@ -18,5 +18,5 @@ Gem::Specification.new do |gem|
gem.add_dependency 'multi_json'
gem.add_development_dependency 'activesupport'
- # gem.add_development_dependency 'minitest'
+ gem.add_development_dependency 'actionmailer'
end
View
@@ -1,4 +1,4 @@
-require File.expand_path(File.dirname(__FILE__) + '/test_helper')
+require 'test_helper'
class Seatbelt::AssertJsonNewDslTest < Test::Unit::TestCase
include Seatbelt::AssertJson
View
@@ -0,0 +1,78 @@
+require 'test_helper'
+
+class Seatbelt::AssertMailTest < Test::Unit::TestCase
+
+ def test_assert_mail
+ Mailer.test.deliver
+
+ assert_mail :to => 'test@seatbelt.co.nz'
+ assert_mail :cc => 'cc@seatbelt.co.nz'
+ assert_mail :bcc => 'bcc@seatbelt.co.nz'
+ assert_mail :from => 'from@seatbelt.co.nz'
+ assert_mail :subject => 'mail subject'
+ assert_mail :subject => 'subject' # uses regular expressions
+ assert_mail :body => 'mail body text'
+ assert_mail :body => 'body text' # uses regular expressions
+ assert_mail :body => ['mail body', 'body text']
+
+ assert_mail :to => 'test@seatbelt.co.nz', :subject => 'subject', :body => 'body'
+ end
+ def test_assert_mail_crosscheck
+ Mailer.test.deliver
+
+ assert_raises(MiniTest::Assertion) { assert_mail :to => 'unknown@seatbelt.co.nz' }
+ assert_raises(MiniTest::Assertion) { assert_mail :cc => 'unknown@seatbelt.co.nz' }
+ assert_raises(MiniTest::Assertion) { assert_mail :bcc => 'unknown@seatbelt.co.nz' }
+ assert_raises(MiniTest::Assertion) { assert_mail :from => 'unknown@seatbelt.co.nz' }
+ assert_raises(MiniTest::Assertion) { assert_mail :subject => 'unknown' }
+ assert_raises(MiniTest::Assertion) { assert_mail :body => 'unknown' }
+ assert_raises(MiniTest::Assertion) { assert_mail :to => 'unknown@seatbelt.co.nz', :subject => 'unknown', :body => 'unknown' }
+ end
+
+ def test_assert_mail_with_multiple_mails
+ Mailer.test.deliver
+ Mailer.test(:to => 'another@seatbelt.co.nz').deliver
+
+ assert_mail :to => 'test@seatbelt.co.nz'
+ assert_mail :to => 'another@seatbelt.co.nz'
+ end
+
+ def test_assert_mail_with_block
+ assert_mail :to => 'test@seatbelt.co.nz', :subject => 'subject', :body => 'body' do
+ Mailer.test.deliver
+ end
+ end
+ def test_assert_mail_with_block_crosscheck
+ assert_raises(MiniTest::Assertion) do
+ assert_mail :to => 'unknown@seatbelt.co.nz' do
+ Mailer.test.deliver
+ end
+ end
+ end
+
+ def test_assert_no_mail
+ assert_no_mail do
+ # do something
+ abc = 1 + 1
+ end
+
+ assert_no_mail :to => 'another@seatbelt.co.nz' do
+ Mailer.test.deliver
+ end
+ assert_no_mail :subject => 'my other subject' do
+ Mailer.test.deliver
+ end
+ assert_no_mail :body => 'my other body text' do
+ Mailer.test.deliver
+ end
+ end
+
+ def test_assert_no_mail_crosscheck
+ assert_raises(MiniTest::Assertion) do
+ assert_no_mail :to => 'test@seatbelt.co.nz' do
+ Mailer.test.deliver
+ end
+ end
+ end
+
+end
View
@@ -0,0 +1,14 @@
+ActionMailer::Base.delivery_method = :test
+class Mailer < ActionMailer::Base
+
+ def test(options={})
+ mail({:from => 'from@seatbelt.co.nz',
+ :to => 'test@seatbelt.co.nz',
+ :cc => 'cc@seatbelt.co.nz',
+ :bcc => 'bcc@seatbelt.co.nz',
+ :subject => 'mail subject'}.merge(options)) do |format|
+ format.text { render :text => options[:body] || 'mail body text' }
+ end
+ end
+
+end
View
@@ -1,6 +1,10 @@
require 'rubygems'
require 'test/unit'
require 'active_support'
+require 'action_mailer'
$:.push File.expand_path("../../lib", __FILE__)
require File.dirname(__FILE__) + '/../lib/seatbelt.rb'
+
+# Requiring custom spec helpers
+Dir[File.dirname(__FILE__) + "/helpers/**/*.rb"].sort.each { |f| require File.expand_path(f) }

0 comments on commit 445fa01

Please sign in to comment.