Skip to content

Commit

Permalink
Only prepend_before the Rails setup methods
Browse files Browse the repository at this point in the history
- Fixes rspec#534
- Unfortunately depends on names of methods that are internal to Rails
  and subject to change. Fingers crossed.
  • Loading branch information
dchelimsky committed May 5, 2012
1 parent 93e4eec commit 71006ce
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
10 changes: 8 additions & 2 deletions lib/rspec/rails/adapters.rb
Expand Up @@ -12,15 +12,21 @@ module ClassMethods
# Wraps `setup` calls from within Rails' testing framework in `before`
# hooks.
def setup(*methods)
methods.each {|method| prepend_before { send method } }
methods.each do |method|
if method.to_s =~ /^setup_(fixtures|controller_request_and_response)$/
prepend_before { send method }
else
before { send method }
end
end
end

# @api private
#
# Wraps `teardown` calls from within Rails' testing framework in
# `after` hooks.
def teardown(*methods)
methods.each {|method| after { send method } }
methods.each { |method| after { send method } }
end
end

Expand Down
32 changes: 32 additions & 0 deletions spec/rspec/rails/setup_and_teardown_adapter_spec.rb
@@ -0,0 +1,32 @@
require 'spec_helper'

describe RSpec::Rails::SetupAndTeardownAdapter do
describe "::setup" do
it "registers before hooks in the order setup is received" do
klass = Class.new do
include RSpec::Rails::SetupAndTeardownAdapter
def self.foo; "foo"; end
def self.bar; "bar"; end
end
klass.should_receive(:before).ordered { |&block| block.call.should eq "foo" }
klass.should_receive(:before).ordered { |&block| block.call.should eq "bar" }

klass.setup :foo
klass.setup :bar
end

it "registers prepend_before hooks for the Rails' setup methods" do
klass = Class.new do
include RSpec::Rails::SetupAndTeardownAdapter
def self.setup_fixtures; "setup fixtures" end
def self.setup_controller_request_and_response; "setup controller" end
end

klass.should_receive(:prepend_before) { |&block| block.call.should eq "setup fixtures" }
klass.should_receive(:prepend_before) { |&block| block.call.should eq "setup controller" }

klass.setup :setup_fixtures
klass.setup :setup_controller_request_and_response
end
end
end

0 comments on commit 71006ce

Please sign in to comment.