From cbc3afb8786a9e6caa486fa2c97b17348c9eff51 Mon Sep 17 00:00:00 2001 From: Nicholas Seckar Date: Sun, 6 Aug 2006 02:51:53 +0000 Subject: Add Dispatcher.to_prepare and config.to_prepare to provide a pre-request hook. git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@4686 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- railties/test/dispatcher_test.rb | 49 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) (limited to 'railties/test/dispatcher_test.rb') diff --git a/railties/test/dispatcher_test.rb b/railties/test/dispatcher_test.rb index 41d08e224e..8fb19b173a 100644 --- a/railties/test/dispatcher_test.rb +++ b/railties/test/dispatcher_test.rb @@ -24,6 +24,8 @@ class DispatcherTest < Test::Unit::TestCase def setup @output = StringIO.new ENV['REQUEST_METHOD'] = "GET" + Dispatcher.send(:preparation_callbacks).clear + Dispatcher.send(:preparation_callbacks_run=, false) end def teardown @@ -84,6 +86,53 @@ class DispatcherTest < Test::Unit::TestCase ensure $stdin = old_stdin end + + def test_preparation_callbacks + Object.const_set :ApplicationController, nil + old_mechanism = Dependencies.mechanism + + a = b = c = nil + Dispatcher.to_prepare { a = b = c = 1 } + Dispatcher.to_prepare { b = c = 2 } + Dispatcher.to_prepare { c = 3 } + + Dispatcher.send :prepare_application + + assert_equal 1, a + assert_equal 2, b + assert_equal 3, c + + # When mechanism is :load, perform the callbacks each request: + Dependencies.mechanism = :load + a = b = c = nil + Dispatcher.send :prepare_application + assert_equal 1, a + assert_equal 2, b + assert_equal 3, c + + # But when not :load, make sure they are only run once + a = b = c = nil + Dependencies.mechanism = :not_load + Dispatcher.send :prepare_application + assert_equal nil, a || b || c + ensure + Dependencies.mechanism = old_mechanism + Object.send :remove_const, :ApplicationController + end + + def test_to_prepare_with_identifier_replaces + Object.const_set :ApplicationController, nil + + a = b = nil + Dispatcher.to_prepare(:unique_id) { a = b = 1 } + Dispatcher.to_prepare(:unique_id) { a = 2 } + + Dispatcher.send :prepare_application + assert_equal 2, a + assert_equal nil, b + ensure + Object.send :remove_const, :ApplicationController + end private def dispatch -- cgit v1.2.3