diff options
author | Jeremy Kemper <jeremy@bitsweat.net> | 2007-09-23 02:11:44 +0000 |
---|---|---|
committer | Jeremy Kemper <jeremy@bitsweat.net> | 2007-09-23 02:11:44 +0000 |
commit | 3dea8b580b9c67bb27c01290fb3b17f446544b78 (patch) | |
tree | 6d0bdda03df500643b4d3f086866855a04e8a93c /railties | |
parent | ea456801182195b113e695ec9019757ce523be0f (diff) | |
download | rails-3dea8b580b9c67bb27c01290fb3b17f446544b78.tar.gz rails-3dea8b580b9c67bb27c01290fb3b17f446544b78.tar.bz2 rails-3dea8b580b9c67bb27c01290fb3b17f446544b78.zip |
Dispatcher tests. References #9630.
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@7591 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'railties')
-rw-r--r-- | railties/test/abstract_unit.rb | 10 | ||||
-rw-r--r-- | railties/test/dispatcher_test.rb | 101 | ||||
-rw-r--r-- | railties/test/mocks/dispatcher.rb | 13 |
3 files changed, 48 insertions, 76 deletions
diff --git a/railties/test/abstract_unit.rb b/railties/test/abstract_unit.rb index 68ddb0c24f..7b773531c6 100644 --- a/railties/test/abstract_unit.rb +++ b/railties/test/abstract_unit.rb @@ -15,3 +15,13 @@ end class Test::Unit::TestCase # Add stuff here if you need it end + +# Wrap tests that use Mocha and skip if unavailable. +def uses_mocha(test_name) + require 'rubygems' + gem 'mocha', '>= 0.5.5' + require 'mocha' + yield +rescue LoadError + $stderr.puts "Skipping #{test_name} tests. `gem install mocha` and try again." +end diff --git a/railties/test/dispatcher_test.rb b/railties/test/dispatcher_test.rb index 3267314b03..081d21f8d8 100644 --- a/railties/test/dispatcher_test.rb +++ b/railties/test/dispatcher_test.rb @@ -1,4 +1,7 @@ require "#{File.dirname(__FILE__)}/abstract_unit" + +uses_mocha 'dispatcher tests' do + $:.unshift File.dirname(__FILE__) + "/../../actionmailer/lib" require 'stringio' @@ -8,15 +11,6 @@ require 'dispatcher' require 'action_controller' require 'action_mailer' -ACTION_MAILER_DEF = <<AM - class DispatcherTestMailer < ActionMailer::Base - end -AM - -ACTION_CONTROLLER_DEF = <<AM - class DispatcherControllerTest < ActionController::Base - end -AM class DispatcherTest < Test::Unit::TestCase def setup @@ -26,75 +20,46 @@ class DispatcherTest < Test::Unit::TestCase Dispatcher.send(:preparation_callbacks).clear Dispatcher.send(:preparation_callbacks_run=, false) - Object.const_set :ApplicationController, nil - class << ActionController::Routing::Routes - alias_method :old_reload, :reload - def reload() end - end + Object.const_set 'ApplicationController', nil end def teardown - Object.send :remove_const, :ApplicationController ENV['REQUEST_METHOD'] = nil - class << ActionController::Routing::Routes - alias_method :reload, :old_reload - end + Object.send :remove_const, 'ApplicationController' end - def test_ac_subclasses_cleared_on_reset - Object.class_eval(ACTION_CONTROLLER_DEF) - assert_subclasses 1, ActionController::Base - dispatch + def test_clears_dependencies_after_dispatch_if_in_loading_mode + Dependencies.stubs(:load?).returns(true) - GC.start # force the subclass to be collected - assert_subclasses 0, ActionController::Base - end + ActionController::Routing::Routes.expects(:reload).once + Dependencies.expects(:clear).once - def test_am_subclasses_cleared_on_reset - Object.class_eval(ACTION_MAILER_DEF) - assert_subclasses 1, ActionMailer::Base dispatch + end + + def test_clears_dependencies_after_dispatch_if_not_in_loading_mode + Dependencies.stubs(:load?).returns(false) + + ActionController::Routing::Routes.expects(:reload).never + Dependencies.expects(:clear).never - GC.start # force the subclass to be collected - assert_subclasses 0, ActionMailer::Base + dispatch end + def test_failsafe_response + CGI.expects(:new).raises('some multipart parsing failure') - INVALID_MULTIPART = [ - 'POST /foo HTTP/1.0', - 'Host: example.com', - 'Content-Type: multipart/form-data;boundary=foo' - ] - - EMPTY_CONTENT = (INVALID_MULTIPART + [ - 'Content-Length: 100', - nil, nil - ]).join("\r\n") - - CONTENT_LENGTH_MISMATCH = (INVALID_MULTIPART + [ - 'Content-Length: 100', - nil, nil, - 'foobar' - ]).join("\r\n") - - NONINTEGER_CONTENT_LENGTH = (INVALID_MULTIPART + [ - 'Content-Length: abc', - nil, nil - ]).join("\r\n") - - def test_bad_multipart_request - old_stdin = $stdin - [EMPTY_CONTENT, CONTENT_LENGTH_MISMATCH, NONINTEGER_CONTENT_LENGTH].each do |bad_request| - $stdin = StringIO.new(bad_request) - output = StringIO.new - assert_nothing_raised { dispatch output } - assert_equal "Status: 400 Bad Request\r\n", output.string - end - ensure - $stdin = old_stdin + ActionController::Routing::Routes.stubs(:reload) + Dispatcher.stubs(:log_failsafe_exception) + + assert_nothing_raised { dispatch } + + assert_equal "Status: 400 Bad Request\r\nContent-Type: text/html\r\n\r\n<html><body><h1>400 Bad Request</h1></body></html>", @output.string end - + def test_preparation_callbacks + ActionController::Routing::Routes.stubs(:reload) + old_mechanism = Dependencies.mechanism a = b = c = nil @@ -126,6 +91,8 @@ class DispatcherTest < Test::Unit::TestCase end def test_to_prepare_with_identifier_replaces + ActionController::Routing::Routes.stubs(:reload) + a = b = nil Dispatcher.to_prepare(:unique_id) { a = b = 1 } Dispatcher.to_prepare(:unique_id) { a = 2 } @@ -137,6 +104,12 @@ class DispatcherTest < Test::Unit::TestCase private def dispatch(output = @output) + controller = mock + controller.stubs(:process).returns(controller) + controller.stubs(:out).with(output).returns('response') + + ActionController::Routing::Routes.stubs(:recognize).returns(controller) + Dispatcher.dispatch(nil, {}, output) end @@ -144,3 +117,5 @@ class DispatcherTest < Test::Unit::TestCase assert_equal howmany, klass.subclasses.size, message end end + +end # uses_mocha diff --git a/railties/test/mocks/dispatcher.rb b/railties/test/mocks/dispatcher.rb deleted file mode 100644 index 3391f398bf..0000000000 --- a/railties/test/mocks/dispatcher.rb +++ /dev/null @@ -1,13 +0,0 @@ -class Dispatcher - class <<self - attr_accessor :time_to_sleep - attr_accessor :raise_exception - attr_accessor :dispatch_hook - - def dispatch(cgi, session_options = nil, output = $stdout) - dispatch_hook.call(cgi) if dispatch_hook - sleep(time_to_sleep || 0) - raise raise_exception, "Something died" if raise_exception - end - end -end |