Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Allow Resque (queue target) to be overridden for testing

  • Loading branch information...
commit 1bc5b3deec62084b0f62239344240c6e920ef4ea 1 parent a97dc66
@zapnap authored
Showing with 29 additions and 17 deletions.
  1. +8 −2 lib/resque_mailer.rb
  2. +21 −15 spec/resque_mailer_spec.rb
View
10 lib/resque_mailer.rb
@@ -1,7 +1,7 @@
module Resque
module Mailer
class << self
- attr_accessor :default_queue_name
+ attr_accessor :default_queue_name, :default_queue_target
attr_reader :excluded_environments
def excluded_environments=(envs)
@@ -13,6 +13,7 @@ def included(base)
end
end
+ self.default_queue_target = ::Resque
self.default_queue_name = "mailer"
self.excluded_environments = [:test]
@@ -25,11 +26,12 @@ def method_missing(method_name, *args)
return super if environment_excluded?
if action_methods.include?(method_name.to_s)
+ resque = self.resque
mailer_class = self
super.tap do |resque_mail|
resque_mail.class_eval do
define_method(:deliver) do
- ::Resque.enqueue(mailer_class, method_name, *args)
+ resque.enqueue(mailer_class, method_name, *args)
self
end
end
@@ -51,6 +53,10 @@ def queue
::Resque::Mailer.default_queue_name
end
+ def resque
+ ::Resque::Mailer.default_queue_target
+ end
+
def excluded_environment?(name)
::Resque::Mailer.excluded_environments && ::Resque::Mailer.excluded_environments.include?(name.to_sym)
end
View
36 spec/resque_mailer_spec.rb
@@ -1,5 +1,9 @@
require File.join(File.expand_path(File.dirname(__FILE__)), 'spec_helper')
+class FakeResque
+ def self.enqueue(*args); end
+end
+
class Rails3Mailer < ActionMailer::Base
include Resque::Mailer
default :from => "from@example.org", :subject => "Subject"
@@ -11,22 +15,28 @@ def test_mail(*params)
end
describe Resque::Mailer do
+ let(:resque) { FakeResque }
+
before do
+ Resque::Mailer.default_queue_target = resque
Rails3Mailer.stub(:current_env => :test)
end
+ describe "resque" do
+ it "allows overriding of the default queue target (for testing)" do
+ Resque::Mailer.default_queue_target = FakeResque
+ Rails3Mailer.resque.should == FakeResque
+ end
+ end
+
describe "queue" do
- context "when using the default" do
- it "should return 'mailer'" do
- Rails3Mailer.queue.should == "mailer"
- end
+ it "defaults to the 'mailer' queue" do
+ Rails3Mailer.queue.should == "mailer"
end
- context "when modified by user" do
- it "should return proper queue name" do
- Resque::Mailer.default_queue_name = "postal"
- Rails3Mailer.queue.should == "postal"
- end
+ it "allows overriding of the default queue name" do
+ Resque::Mailer.default_queue_name = "postal"
+ Rails3Mailer.queue.should == "postal"
end
end
@@ -37,16 +47,12 @@ def test_mail(*params)
}
end
- before(:each) do
- Resque.stub(:enqueue)
- end
-
it 'should not deliver the email synchronously' do
lambda { @delivery.call }.should_not change(ActionMailer::Base.deliveries, :size)
end
it 'should place the deliver action on the Resque "mailer" queue' do
- Resque.should_receive(:enqueue).with(Rails3Mailer, :test_mail, Rails3Mailer::MAIL_PARAMS)
+ resque.should_receive(:enqueue).with(Rails3Mailer, :test_mail, Rails3Mailer::MAIL_PARAMS)
@delivery.call
end
@@ -54,7 +60,7 @@ def test_mail(*params)
it 'should not deliver through Resque for excluded environments' do
Resque::Mailer.stub(:excluded_environments => [:custom])
Rails3Mailer.should_receive(:current_env).and_return(:custom)
- Resque.should_not_receive(:enqueue)
+ resque.should_not_receive(:enqueue)
@delivery.call
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.