I previously posted about global params hashes for nested shoulda contexts in functional tests. This post is a followup that shows how my solution has since crystallized.
Here’s the special sauce to add to your test_helper.rb:
ActionController::TestCase.class_eval do
# special overload methods for "global"/nested params
[ :get, :post ].each do |overloaded_method|
define_method overloaded_method do |*args|
action,params,extras = *args
super action, @params.merge( params || {} ), *extras
end
end
def setup
super
@params = {}
end
end
This creates a @params variable that gets merged with all HTTP requests.
Now you can use global params in your nested contexts as such:
class FooControllerTest < ActionController::TestCase
def setup
super
@params[:security_token] = 'abc123' # add any global params you need here
@event = create_event
end
context "A POST to :action" do
setup do
@action = lambda{ post :action, :id => @event.id }
end
%w[ attending not_attending maybe_attending ].each do |status|
context "with :status = '#{status}'" do
setup do
@params[:status] = status
@action.call
end
should_respond_with :success
should_change "Rsvp.count", :by => 1
should "create the proper Rsvp object" do
assert Rsvp.find_by_user_id_and_event_id_and_status( @user.id, @event.id, status )
end
end
end
end
end


November 2nd, 2008 at 01:54 PM
def setup super @params = {} end
I'm on rails 2.0.3 and this is not work... super is not called... @request is null