diff options
45 files changed, 591 insertions, 577 deletions
diff --git a/actionpack/lib/action_controller/assertions.rb b/actionpack/lib/action_controller/assertions.rb deleted file mode 100644 index 5b9a2b71f2..0000000000 --- a/actionpack/lib/action_controller/assertions.rb +++ /dev/null @@ -1,69 +0,0 @@ -require 'test/unit/assertions' - -module ActionController #:nodoc: - # In addition to these specific assertions, you also have easy access to various collections that the regular test/unit assertions - # can be used against. These collections are: - # - # * assigns: Instance variables assigned in the action that are available for the view. - # * session: Objects being saved in the session. - # * flash: The flash objects currently in the session. - # * cookies: Cookies being sent to the user on this request. - # - # These collections can be used just like any other hash: - # - # assert_not_nil assigns(:person) # makes sure that a @person instance variable was set - # assert_equal "Dave", cookies[:name] # makes sure that a cookie called :name was set as "Dave" - # assert flash.empty? # makes sure that there's nothing in the flash - # - # For historic reasons, the assigns hash uses string-based keys. So assigns[:person] won't work, but assigns["person"] will. To - # appease our yearning for symbols, though, an alternative accessor has been devised using a method call instead of index referencing. - # So assigns(:person) will work just like assigns["person"], but again, assigns[:person] will not work. - # - # On top of the collections, you have the complete url that a given action redirected to available in redirect_to_url. - # - # For redirects within the same controller, you can even call follow_redirect and the redirect will be followed, triggering another - # action call which can then be asserted against. - # - # == Manipulating the request collections - # - # The collections described above link to the response, so you can test if what the actions were expected to do happened. But - # sometimes you also want to manipulate these collections in the incoming request. This is really only relevant for sessions - # and cookies, though. For sessions, you just do: - # - # @request.session[:key] = "value" - # - # For cookies, you need to manually create the cookie, like this: - # - # @request.cookies["key"] = CGI::Cookie.new("key", "value") - # - # == Testing named routes - # - # If you're using named routes, they can be easily tested using the original named routes' methods straight in the test case. - # Example: - # - # assert_redirected_to page_url(:title => 'foo') - module Assertions - def self.included(klass) - %w(response selector tag dom routing model).each do |kind| - require "action_controller/assertions/#{kind}_assertions" - klass.module_eval { include const_get("#{kind.camelize}Assertions") } - end - end - - def clean_backtrace(&block) - yield - rescue Test::Unit::AssertionFailedError => error - framework_path = Regexp.new(File.expand_path("#{File.dirname(__FILE__)}/assertions")) - error.backtrace.reject! { |line| File.expand_path(line) =~ framework_path } - raise - end - end -end - -module Test #:nodoc: - module Unit #:nodoc: - class TestCase #:nodoc: - include ActionController::Assertions - end - end -end diff --git a/actionpack/lib/action_controller/integration.rb b/actionpack/lib/action_controller/integration.rb index fc473c269c..b3771c1ebc 100644 --- a/actionpack/lib/action_controller/integration.rb +++ b/actionpack/lib/action_controller/integration.rb @@ -1,4 +1,4 @@ -require 'active_support/test_case' +require 'action_controller/test_case' require 'action_controller/dispatcher' require 'action_controller/test_process' @@ -16,7 +16,7 @@ module ActionController # rather than instantiating Integration::Session directly. class Session include Test::Unit::Assertions - include ActionController::Assertions + include ActionController::TestCase::Assertions include ActionController::TestProcess # The integer HTTP status code of the last request. diff --git a/actionpack/lib/action_controller/test_case.rb b/actionpack/lib/action_controller/test_case.rb index 4fc60f0697..b925230118 100644 --- a/actionpack/lib/action_controller/test_case.rb +++ b/actionpack/lib/action_controller/test_case.rb @@ -1,20 +1,6 @@ require 'active_support/test_case' module ActionController - class NonInferrableControllerError < ActionControllerError - def initialize(name) - @name = name - super "Unable to determine the controller to test from #{name}. " + - "You'll need to specify it using 'tests YourController' in your " + - "test case definition. This could mean that #{inferred_controller_name} does not exist " + - "or it contains syntax errors" - end - - def inferred_controller_name - @name.sub(/Test$/, '') - end - end - # Superclass for ActionController functional tests. Functional tests allow you to # test a single controller action per test method. This should not be confused with # integration tests (see ActionController::IntegrationTest), which are more like @@ -74,7 +60,67 @@ module ActionController # class SpecialEdgeCaseWidgetsControllerTest < ActionController::TestCase # tests WidgetController # end + # + # == Testing controller internals + # + # In addition to these specific assertions, you also have easy access to various collections that the regular test/unit assertions + # can be used against. These collections are: + # + # * assigns: Instance variables assigned in the action that are available for the view. + # * session: Objects being saved in the session. + # * flash: The flash objects currently in the session. + # * cookies: Cookies being sent to the user on this request. + # + # These collections can be used just like any other hash: + # + # assert_not_nil assigns(:person) # makes sure that a @person instance variable was set + # assert_equal "Dave", cookies[:name] # makes sure that a cookie called :name was set as "Dave" + # assert flash.empty? # makes sure that there's nothing in the flash + # + # For historic reasons, the assigns hash uses string-based keys. So assigns[:person] won't work, but assigns["person"] will. To + # appease our yearning for symbols, though, an alternative accessor has been devised using a method call instead of index referencing. + # So assigns(:person) will work just like assigns["person"], but again, assigns[:person] will not work. + # + # On top of the collections, you have the complete url that a given action redirected to available in redirect_to_url. + # + # For redirects within the same controller, you can even call follow_redirect and the redirect will be followed, triggering another + # action call which can then be asserted against. + # + # == Manipulating the request collections + # + # The collections described above link to the response, so you can test if what the actions were expected to do happened. But + # sometimes you also want to manipulate these collections in the incoming request. This is really only relevant for sessions + # and cookies, though. For sessions, you just do: + # + # @request.session[:key] = "value" + # + # For cookies, you need to manually create the cookie, like this: + # + # @request.cookies["key"] = CGI::Cookie.new("key", "value") + # + # == Testing named routes + # + # If you're using named routes, they can be easily tested using the original named routes' methods straight in the test case. + # Example: + # + # assert_redirected_to page_url(:title => 'foo') class TestCase < ActiveSupport::TestCase + module Assertions + %w(response selector tag dom routing model).each do |kind| + require "action_controller/assertions/#{kind}_assertions" + include ActionController::Assertions.const_get("#{kind.camelize}Assertions") + end + + def clean_backtrace(&block) + yield + rescue ActiveSupport::TestCase::Assertion => error + framework_path = Regexp.new(File.expand_path("#{File.dirname(__FILE__)}/assertions")) + error.backtrace.reject! { |line| File.expand_path(line) =~ framework_path } + raise + end + end + include Assertions + # When the request.remote_addr remains the default for testing, which is 0.0.0.0, the exception is simply raised inline # (bystepping the regular exception handling from rescue_action). If the request.remote_addr is anything else, the regular # rescue_action process takes place. This means you can test your rescue_action code by setting remote_addr to something else @@ -107,7 +153,7 @@ module ActionController end def controller_class=(new_class) - prepare_controller_class(new_class) + prepare_controller_class(new_class) if new_class write_inheritable_attribute(:controller_class, new_class) end @@ -122,7 +168,7 @@ module ActionController def determine_default_controller_class(name) name.sub(/Test$/, '').constantize rescue NameError - raise NonInferrableControllerError.new(name) + nil end def prepare_controller_class(new_class) @@ -131,17 +177,23 @@ module ActionController end def setup_controller_request_and_response - @controller = self.class.controller_class.new - @controller.request = @request = TestRequest.new + @request = TestRequest.new @response = TestResponse.new - @controller.params = {} - @controller.send(:initialize_current_url) + if klass = self.class.controller_class + @controller ||= klass.new rescue nil + end + + if @controller + @controller.request = @request + @controller.params = {} + @controller.send(:initialize_current_url) + end end # Cause the action to be rescued according to the regular rules for rescue_action when the visitor is not local def rescue_action_in_public! @request.remote_addr = '208.77.188.166' # example.com end - end + end end diff --git a/actionpack/lib/action_controller/test_process.rb b/actionpack/lib/action_controller/test_process.rb index 7a31f0e8d5..38e15baac2 100644 --- a/actionpack/lib/action_controller/test_process.rb +++ b/actionpack/lib/action_controller/test_process.rb @@ -1,4 +1,3 @@ -require 'action_controller/assertions' require 'action_controller/test_case' module ActionController #:nodoc: @@ -463,9 +462,9 @@ module ActionController #:nodoc: html_document.find_all(conditions) end - def method_missing(selector, *args) - if ActionController::Routing::Routes.named_routes.helpers.include?(selector) - @controller.send(selector, *args) + def method_missing(selector, *args, &block) + if @controller && ActionController::Routing::Routes.named_routes.helpers.include?(selector) + @controller.send(selector, *args, &block) else super end diff --git a/actionpack/lib/action_view/test_case.rb b/actionpack/lib/action_view/test_case.rb index c69f9455b2..cdea1def92 100644 --- a/actionpack/lib/action_view/test_case.rb +++ b/actionpack/lib/action_view/test_case.rb @@ -1,7 +1,9 @@ -require 'active_support/test_case' +require 'action_controller/test_case' module ActionView class TestCase < ActiveSupport::TestCase + include ActionController::TestCase::Assertions + class_inheritable_accessor :helper_class @@helper_class = nil diff --git a/actionpack/test/active_record_unit.rb b/actionpack/test/active_record_unit.rb index a377ccad24..d8d2e00dc2 100644 --- a/actionpack/test/active_record_unit.rb +++ b/actionpack/test/active_record_unit.rb @@ -82,7 +82,9 @@ class ActiveRecordTestConnector end end -class ActiveRecordTestCase < ActiveSupport::TestCase +class ActiveRecordTestCase < ActionController::TestCase + include ActiveRecord::TestFixtures + # Set our fixture path if ActiveRecordTestConnector.able_to_connect self.fixture_path = [FIXTURE_LOAD_PATH] @@ -96,8 +98,6 @@ class ActiveRecordTestCase < ActiveSupport::TestCase def run(*args) super if ActiveRecordTestConnector.connected end - - def default_test; end end ActiveRecordTestConnector.setup diff --git a/actionpack/test/activerecord/render_partial_with_record_identification_test.rb b/actionpack/test/activerecord/render_partial_with_record_identification_test.rb index d75cb2b53a..147b270808 100644 --- a/actionpack/test/activerecord/render_partial_with_record_identification_test.rb +++ b/actionpack/test/activerecord/render_partial_with_record_identification_test.rb @@ -47,15 +47,9 @@ class RenderPartialWithRecordIdentificationController < ActionController::Base end class RenderPartialWithRecordIdentificationTest < ActiveRecordTestCase + tests RenderPartialWithRecordIdentificationController fixtures :developers, :projects, :developers_projects, :topics, :replies, :companies, :mascots - def setup - @controller = RenderPartialWithRecordIdentificationController.new - @request = ActionController::TestRequest.new - @response = ActionController::TestResponse.new - super - end - def test_rendering_partial_with_has_many_and_belongs_to_association get :render_with_has_many_and_belongs_to_association assert_template 'projects/_project' @@ -162,12 +156,7 @@ module Fun end class RenderPartialWithRecordIdentificationAndNestedControllersTest < ActiveRecordTestCase - def setup - @controller = Fun::NestedController.new - @request = ActionController::TestRequest.new - @response = ActionController::TestResponse.new - super - end + tests Fun::NestedController def test_render_with_record_in_nested_controller get :render_with_record_in_nested_controller @@ -183,12 +172,7 @@ class RenderPartialWithRecordIdentificationAndNestedControllersTest < ActiveReco end class RenderPartialWithRecordIdentificationAndNestedDeeperControllersTest < ActiveRecordTestCase - def setup - @controller = Fun::Serious::NestedDeeperController.new - @request = ActionController::TestRequest.new - @response = ActionController::TestResponse.new - super - end + tests Fun::Serious::NestedDeeperController def test_render_with_record_in_deeper_nested_controller get :render_with_record_in_deeper_nested_controller diff --git a/actionpack/test/controller/action_pack_assertions_test.rb b/actionpack/test/controller/action_pack_assertions_test.rb index 56ba36cee5..7050000dd8 100644 --- a/actionpack/test/controller/action_pack_assertions_test.rb +++ b/actionpack/test/controller/action_pack_assertions_test.rb @@ -165,13 +165,11 @@ module Admin end # a test case to exercise the new capabilities TestRequest & TestResponse -class ActionPackAssertionsControllerTest < Test::Unit::TestCase +class ActionPackAssertionsControllerTest < ActionController::TestCase # let's get this party started def setup ActionController::Routing::Routes.reload ActionController::Routing.use_controllers!(%w(action_pack_assertions admin/inner_module user content admin/user)) - @controller = ActionPackAssertionsController.new - @request, @response = ActionController::TestRequest.new, ActionController::TestResponse.new end def teardown @@ -235,13 +233,13 @@ class ActionPackAssertionsControllerTest < Test::Unit::TestCase map.connect ':controller/:action/:id' end process :redirect_to_named_route - assert_raise(Test::Unit::AssertionFailedError) do + assert_raise(ActiveSupport::TestCase::Assertion) do assert_redirected_to 'http://test.host/route_two' end - assert_raise(Test::Unit::AssertionFailedError) do + assert_raise(ActiveSupport::TestCase::Assertion) do assert_redirected_to :controller => 'action_pack_assertions', :action => 'nothing', :id => 'two' end - assert_raise(Test::Unit::AssertionFailedError) do + assert_raise(ActiveSupport::TestCase::Assertion) do assert_redirected_to route_two_url end end @@ -410,7 +408,7 @@ class ActionPackAssertionsControllerTest < Test::Unit::TestCase def test_assert_redirection_fails_with_incorrect_controller process :redirect_to_controller - assert_raise(Test::Unit::AssertionFailedError) do + assert_raise(ActiveSupport::TestCase::Assertion) do assert_redirected_to :controller => "action_pack_assertions", :action => "flash_me" end end @@ -466,7 +464,7 @@ class ActionPackAssertionsControllerTest < Test::Unit::TestCase begin assert_valid assigns('record') assert false - rescue Test::Unit::AssertionFailedError => e + rescue ActiveSupport::TestCase::Assertion => e end end @@ -475,7 +473,7 @@ class ActionPackAssertionsControllerTest < Test::Unit::TestCase get :index assert_response :success flunk 'Expected non-success response' - rescue Test::Unit::AssertionFailedError => e + rescue ActiveSupport::TestCase::Assertion => e assert e.message.include?('FAIL') end @@ -484,17 +482,15 @@ class ActionPackAssertionsControllerTest < Test::Unit::TestCase get :show assert_response :success flunk 'Expected non-success response' - rescue Test::Unit::AssertionFailedError + rescue ActiveSupport::TestCase::Assertion + # success rescue flunk "assert_response failed to handle failure response with missing, but optional, exception." end end -class ActionPackHeaderTest < Test::Unit::TestCase - def setup - @controller = ActionPackAssertionsController.new - @request, @response = ActionController::TestRequest.new, ActionController::TestResponse.new - end +class ActionPackHeaderTest < ActionController::TestCase + tests ActionPackAssertionsController def test_rendering_xml_sets_content_type process :hello_xml_world diff --git a/actionpack/test/controller/assert_select_test.rb b/actionpack/test/controller/assert_select_test.rb index 08cbcbf302..a79278159d 100644 --- a/actionpack/test/controller/assert_select_test.rb +++ b/actionpack/test/controller/assert_select_test.rb @@ -19,7 +19,18 @@ end ActionMailer::Base.template_root = FIXTURE_LOAD_PATH -class AssertSelectTest < Test::Unit::TestCase +class AssertSelectTest < ActionController::TestCase + Assertion = ActiveSupport::TestCase::Assertion + + class AssertSelectMailer < ActionMailer::Base + def test(html) + recipients "test <test@test.host>" + from "test@test.host" + subject "Test e-mail" + part :content_type=>"text/html", :body=>html + end + end + class AssertSelectController < ActionController::Base def response_with=(content) @content = content @@ -51,21 +62,9 @@ class AssertSelectTest < Test::Unit::TestCase end end - class AssertSelectMailer < ActionMailer::Base - def test(html) - recipients "test <test@test.host>" - from "test@test.host" - subject "Test e-mail" - part :content_type=>"text/html", :body=>html - end - end - - AssertionFailedError = Test::Unit::AssertionFailedError + tests AssertSelectController def setup - @controller = AssertSelectController.new - @request = ActionController::TestRequest.new - @response = ActionController::TestResponse.new ActionMailer::Base.delivery_method = :test ActionMailer::Base.perform_deliveries = true ActionMailer::Base.deliveries = [] @@ -76,7 +75,7 @@ class AssertSelectTest < Test::Unit::TestCase end def assert_failure(message, &block) - e = assert_raises(AssertionFailedError, &block) + e = assert_raises(Assertion, &block) assert_match(message, e.message) if Regexp === message assert_equal(message, e.message) if String === message end @@ -94,43 +93,43 @@ class AssertSelectTest < Test::Unit::TestCase def test_equality_true_false render_html %Q{<div id="1"></div><div id="2"></div>} - assert_nothing_raised { assert_select "div" } - assert_raises(AssertionFailedError) { assert_select "p" } - assert_nothing_raised { assert_select "div", true } - assert_raises(AssertionFailedError) { assert_select "p", true } - assert_raises(AssertionFailedError) { assert_select "div", false } - assert_nothing_raised { assert_select "p", false } + assert_nothing_raised { assert_select "div" } + assert_raises(Assertion) { assert_select "p" } + assert_nothing_raised { assert_select "div", true } + assert_raises(Assertion) { assert_select "p", true } + assert_raises(Assertion) { assert_select "div", false } + assert_nothing_raised { assert_select "p", false } end def test_equality_string_and_regexp render_html %Q{<div id="1">foo</div><div id="2">foo</div>} - assert_nothing_raised { assert_select "div", "foo" } - assert_raises(AssertionFailedError) { assert_select "div", "bar" } - assert_nothing_raised { assert_select "div", :text=>"foo" } - assert_raises(AssertionFailedError) { assert_select "div", :text=>"bar" } - assert_nothing_raised { assert_select "div", /(foo|bar)/ } - assert_raises(AssertionFailedError) { assert_select "div", /foobar/ } - assert_nothing_raised { assert_select "div", :text=>/(foo|bar)/ } - assert_raises(AssertionFailedError) { assert_select "div", :text=>/foobar/ } - assert_raises(AssertionFailedError) { assert_select "p", :text=>/foobar/ } + assert_nothing_raised { assert_select "div", "foo" } + assert_raises(Assertion) { assert_select "div", "bar" } + assert_nothing_raised { assert_select "div", :text=>"foo" } + assert_raises(Assertion) { assert_select "div", :text=>"bar" } + assert_nothing_raised { assert_select "div", /(foo|bar)/ } + assert_raises(Assertion) { assert_select "div", /foobar/ } + assert_nothing_raised { assert_select "div", :text=>/(foo|bar)/ } + assert_raises(Assertion) { assert_select "div", :text=>/foobar/ } + assert_raises(Assertion) { assert_select "p", :text=>/foobar/ } end def test_equality_of_html render_html %Q{<p>\n<em>"This is <strong>not</strong> a big problem,"</em> he said.\n</p>} text = "\"This is not a big problem,\" he said." html = "<em>\"This is <strong>not</strong> a big problem,\"</em> he said." - assert_nothing_raised { assert_select "p", text } - assert_raises(AssertionFailedError) { assert_select "p", html } - assert_nothing_raised { assert_select "p", :html=>html } - assert_raises(AssertionFailedError) { assert_select "p", :html=>text } + assert_nothing_raised { assert_select "p", text } + assert_raises(Assertion) { assert_select "p", html } + assert_nothing_raised { assert_select "p", :html=>html } + assert_raises(Assertion) { assert_select "p", :html=>text } # No stripping for pre. render_html %Q{<pre>\n<em>"This is <strong>not</strong> a big problem,"</em> he said.\n</pre>} text = "\n\"This is not a big problem,\" he said.\n" html = "\n<em>\"This is <strong>not</strong> a big problem,\"</em> he said.\n" - assert_nothing_raised { assert_select "pre", text } - assert_raises(AssertionFailedError) { assert_select "pre", html } - assert_nothing_raised { assert_select "pre", :html=>html } - assert_raises(AssertionFailedError) { assert_select "pre", :html=>text } + assert_nothing_raised { assert_select "pre", text } + assert_raises(Assertion) { assert_select "pre", html } + assert_nothing_raised { assert_select "pre", :html=>html } + assert_raises(Assertion) { assert_select "pre", :html=>text } end def test_counts @@ -206,16 +205,16 @@ class AssertSelectTest < Test::Unit::TestCase def test_assert_select_text_match render_html %Q{<div id="1"><span>foo</span></div><div id="2"><span>bar</span></div>} assert_select "div" do - assert_nothing_raised { assert_select "div", "foo" } - assert_nothing_raised { assert_select "div", "bar" } - assert_nothing_raised { assert_select "div", /\w*/ } - assert_nothing_raised { assert_select "div", /\w*/, :count=>2 } - assert_raises(AssertionFailedError) { assert_select "div", :text=>"foo", :count=>2 } - assert_nothing_raised { assert_select "div", :html=>"<span>bar</span>" } - assert_nothing_raised { assert_select "div", :html=>"<span>bar</span>" } - assert_nothing_raised { assert_select "div", :html=>/\w*/ } - assert_nothing_raised { assert_select "div", :html=>/\w*/, :count=>2 } - assert_raises(AssertionFailedError) { assert_select "div", :html=>"<span>foo</span>", :count=>2 } + assert_nothing_raised { assert_select "div", "foo" } + assert_nothing_raised { assert_select "div", "bar" } + assert_nothing_raised { assert_select "div", /\w*/ } + assert_nothing_raised { assert_select "div", /\w*/, :count=>2 } + assert_raises(Assertion) { assert_select "div", :text=>"foo", :count=>2 } + assert_nothing_raised { assert_select "div", :html=>"<span>bar</span>" } + assert_nothing_raised { assert_select "div", :html=>"<span>bar</span>" } + assert_nothing_raised { assert_select "div", :html=>/\w*/ } + assert_nothing_raised { assert_select "div", :html=>/\w*/, :count=>2 } + assert_raises(Assertion) { assert_select "div", :html=>"<span>foo</span>", :count=>2 } end end @@ -323,7 +322,7 @@ class AssertSelectTest < Test::Unit::TestCase # Test that we fail if there is nothing to pick. def test_assert_select_rjs_fails_if_nothing_to_pick render_rjs { } - assert_raises(AssertionFailedError) { assert_select_rjs } + assert_raises(Assertion) { assert_select_rjs } end def test_assert_select_rjs_with_unicode @@ -338,10 +337,10 @@ class AssertSelectTest < Test::Unit::TestCase if str.respond_to?(:force_encoding) str.force_encoding(Encoding::UTF_8) assert_select str, /\343\203\201..\343\203\210/u - assert_raises(AssertionFailedError) { assert_select str, /\343\203\201.\343\203\210/u } + assert_raises(Assertion) { assert_select str, /\343\203\201.\343\203\210/u } else assert_select str, Regexp.new("\343\203\201..\343\203\210",0,'U') - assert_raises(AssertionFailedError) { assert_select str, Regexp.new("\343\203\201.\343\203\210",0,'U') } + assert_raises(Assertion) { assert_select str, Regexp.new("\343\203\201.\343\203\210",0,'U') } end end end @@ -365,7 +364,7 @@ class AssertSelectTest < Test::Unit::TestCase assert_select "div", 1 assert_select "#3" end - assert_raises(AssertionFailedError) { assert_select_rjs "test4" } + assert_raises(Assertion) { assert_select_rjs "test4" } end def test_assert_select_rjs_for_replace @@ -383,7 +382,7 @@ class AssertSelectTest < Test::Unit::TestCase assert_select "div", 1 assert_select "#1" end - assert_raises(AssertionFailedError) { assert_select_rjs :replace, "test2" } + assert_raises(Assertion) { assert_select_rjs :replace, "test2" } # Replace HTML. assert_select_rjs :replace_html do assert_select "div", 1 @@ -393,7 +392,7 @@ class AssertSelectTest < Test::Unit::TestCase assert_select "div", 1 assert_select "#2" end - assert_raises(AssertionFailedError) { assert_select_rjs :replace_html, "test1" } + assert_raises(Assertion) { assert_select_rjs :replace_html, "test1" } end def test_assert_select_rjs_for_chained_replace @@ -411,7 +410,7 @@ class AssertSelectTest < Test::Unit::TestCase assert_select "div", 1 assert_select "#1" end - assert_raises(AssertionFailedError) { assert_select_rjs :chained_replace, "test2" } + assert_raises(Assertion) { assert_select_rjs :chained_replace, "test2" } # Replace HTML. assert_select_rjs :chained_replace_html do assert_select "div", 1 @@ -421,7 +420,7 @@ class AssertSelectTest < Test::Unit::TestCase assert_select "div", 1 assert_select "#2" end - assert_raises(AssertionFailedError) { assert_select_rjs :replace_html, "test1" } + assert_raises(Assertion) { assert_select_rjs :replace_html, "test1" } end # Simple remove @@ -440,8 +439,8 @@ class AssertSelectTest < Test::Unit::TestCase assert_select_rjs :remove, "test1" - rescue Test::Unit::AssertionFailedError - assert_equal "No RJS statement that removes 'test1' was rendered.", $!.message + rescue Assertion + assert_equal "No RJS statement that removes 'test1' was rendered.", $!.message end def test_assert_select_rjs_for_remove_ignores_block @@ -472,8 +471,8 @@ class AssertSelectTest < Test::Unit::TestCase assert_select_rjs :show, "test1" - rescue Test::Unit::AssertionFailedError - assert_equal "No RJS statement that shows 'test1' was rendered.", $!.message + rescue Assertion + assert_equal "No RJS statement that shows 'test1' was rendered.", $!.message end def test_assert_select_rjs_for_show_ignores_block @@ -504,8 +503,8 @@ class AssertSelectTest < Test::Unit::TestCase assert_select_rjs :hide, "test1" - rescue Test::Unit::AssertionFailedError - assert_equal "No RJS statement that hides 'test1' was rendered.", $!.message + rescue Assertion + assert_equal "No RJS statement that hides 'test1' was rendered.", $!.message end def test_assert_select_rjs_for_hide_ignores_block @@ -536,8 +535,8 @@ class AssertSelectTest < Test::Unit::TestCase assert_select_rjs :toggle, "test1" - rescue Test::Unit::AssertionFailedError - assert_equal "No RJS statement that toggles 'test1' was rendered.", $!.message + rescue Assertion + assert_equal "No RJS statement that toggles 'test1' was rendered.", $!.message end def test_assert_select_rjs_for_toggle_ignores_block @@ -567,7 +566,7 @@ class AssertSelectTest < Test::Unit::TestCase assert_select "div", 1 assert_select "#3" end - assert_raises(AssertionFailedError) { assert_select_rjs :insert_html, "test1" } + assert_raises(Assertion) { assert_select_rjs :insert_html, "test1" } end # Positioned insert. @@ -693,7 +692,7 @@ EOF # def test_assert_select_email - assert_raises(AssertionFailedError) { assert_select_email {} } + assert_raises(Assertion) { assert_select_email {} } AssertSelectMailer.deliver_test "<div><p>foo</p><p>bar</p></div>" assert_select_email do assert_select "div:root" do diff --git a/actionpack/test/controller/base_test.rb b/actionpack/test/controller/base_test.rb index 738c016c6e..18d185b264 100644 --- a/actionpack/test/controller/base_test.rb +++ b/actionpack/test/controller/base_test.rb @@ -105,7 +105,7 @@ class ControllerInstanceTests < Test::Unit::TestCase end -class PerformActionTest < Test::Unit::TestCase +class PerformActionTest < ActionController::TestCase class MockLogger attr_reader :logged diff --git a/actionpack/test/controller/caching_test.rb b/actionpack/test/controller/caching_test.rb index b6cdd116e5..ad160970cc 100644 --- a/actionpack/test/controller/caching_test.rb +++ b/actionpack/test/controller/caching_test.rb @@ -42,7 +42,7 @@ class PageCachingTestController < ActionController::Base end end -class PageCachingTest < Test::Unit::TestCase +class PageCachingTest < ActionController::TestCase def setup ActionController::Base.perform_caching = true @@ -222,7 +222,7 @@ class ActionCachingMockController end end -class ActionCacheTest < Test::Unit::TestCase +class ActionCacheTest < ActionController::TestCase def setup reset! FileUtils.mkdir_p(FILE_STORE_PATH) @@ -469,7 +469,7 @@ class FragmentCachingTestController < ActionController::Base def some_action; end; end -class FragmentCachingTest < Test::Unit::TestCase +class FragmentCachingTest < ActionController::TestCase def setup ActionController::Base.perform_caching = true @store = ActiveSupport::Cache::MemoryStore.new @@ -601,7 +601,7 @@ class FunctionalCachingController < ActionController::Base end end -class FunctionalFragmentCachingTest < Test::Unit::TestCase +class FunctionalFragmentCachingTest < ActionController::TestCase def setup ActionController::Base.perform_caching = true @store = ActiveSupport::Cache::MemoryStore.new diff --git a/actionpack/test/controller/components_test.rb b/actionpack/test/controller/components_test.rb index 4d36fc411d..e7b17aa34e 100644 --- a/actionpack/test/controller/components_test.rb +++ b/actionpack/test/controller/components_test.rb @@ -69,12 +69,8 @@ class CalleeController < ActionController::Base def rescue_action(e) raise end end -class ComponentsTest < Test::Unit::TestCase - def setup - @controller = CallerController.new - @request = ActionController::TestRequest.new - @response = ActionController::TestResponse.new - end +class ComponentsTest < ActionController::TestCase + tests CallerController def test_calling_from_controller assert_deprecated do diff --git a/actionpack/test/controller/deprecation/deprecated_base_methods_test.rb b/actionpack/test/controller/deprecation/deprecated_base_methods_test.rb index 86555a77df..dd69a63020 100644 --- a/actionpack/test/controller/deprecation/deprecated_base_methods_test.rb +++ b/actionpack/test/controller/deprecation/deprecated_base_methods_test.rb @@ -1,6 +1,6 @@ require 'abstract_unit' -class DeprecatedBaseMethodsTest < Test::Unit::TestCase +class DeprecatedBaseMethodsTest < ActionController::TestCase class Target < ActionController::Base def home_url(greeting) "http://example.com/#{greeting}" @@ -13,11 +13,7 @@ class DeprecatedBaseMethodsTest < Test::Unit::TestCase def rescue_action(e) raise e end end - def setup - @request = ActionController::TestRequest.new - @response = ActionController::TestResponse.new - @controller = Target.new - end + tests Target def test_log_error_silences_deprecation_warnings get :raises_name_error @@ -25,10 +21,12 @@ class DeprecatedBaseMethodsTest < Test::Unit::TestCase assert_not_deprecated { @controller.send :log_error, e } end - def test_assertion_failed_error_silences_deprecation_warnings - get :raises_name_error - rescue => e - error = Test::Unit::Error.new('testing ur doodz', e) - assert_not_deprecated { error.message } + if defined? Test::Unit::Error + def test_assertion_failed_error_silences_deprecation_warnings + get :raises_name_error + rescue => e + error = Test::Unit::Error.new('testing ur doodz', e) + assert_not_deprecated { error.message } + end end end diff --git a/actionpack/test/controller/html-scanner/sanitizer_test.rb b/actionpack/test/controller/html-scanner/sanitizer_test.rb index bae0f5c9fd..e85a5c7abf 100644 --- a/actionpack/test/controller/html-scanner/sanitizer_test.rb +++ b/actionpack/test/controller/html-scanner/sanitizer_test.rb @@ -1,6 +1,6 @@ require 'abstract_unit' -class SanitizerTest < Test::Unit::TestCase +class SanitizerTest < ActionController::TestCase def setup @sanitizer = nil # used by assert_sanitizer end diff --git a/actionpack/test/controller/layout_test.rb b/actionpack/test/controller/layout_test.rb index 1120fdbff5..61c20f8299 100644 --- a/actionpack/test/controller/layout_test.rb +++ b/actionpack/test/controller/layout_test.rb @@ -34,11 +34,8 @@ end ActionView::Template::register_template_handler :mab, lambda { |template| template.source.inspect } -class LayoutAutoDiscoveryTest < Test::Unit::TestCase +class LayoutAutoDiscoveryTest < ActionController::TestCase def setup - @request = ActionController::TestRequest.new - @response = ActionController::TestResponse.new - @request.host = "www.nextangle.com" end @@ -98,12 +95,7 @@ class RendersNoLayoutController < LayoutTest end end -class LayoutSetInResponseTest < Test::Unit::TestCase - def setup - @request = ActionController::TestRequest.new - @response = ActionController::TestResponse.new - end - +class LayoutSetInResponseTest < ActionController::TestCase def test_layout_set_when_using_default_layout @controller = DefaultLayoutController.new get :hello @@ -150,12 +142,7 @@ class SetsNonExistentLayoutFile < LayoutTest layout "nofile.rhtml" end -class LayoutExceptionRaised < Test::Unit::TestCase - def setup - @request = ActionController::TestRequest.new - @response = ActionController::TestResponse.new - end - +class LayoutExceptionRaised < ActionController::TestCase def test_exception_raised_when_layout_file_not_found @controller = SetsNonExistentLayoutFile.new get :hello @@ -170,12 +157,7 @@ class LayoutStatusIsRendered < LayoutTest end end -class LayoutStatusIsRenderedTest < Test::Unit::TestCase - def setup - @request = ActionController::TestRequest.new - @response = ActionController::TestResponse.new - end - +class LayoutStatusIsRenderedTest < ActionController::TestCase def test_layout_status_is_rendered @controller = LayoutStatusIsRendered.new get :hello @@ -187,12 +169,7 @@ class LayoutSymlinkedTest < LayoutTest layout "symlinked/symlinked_layout" end -class LayoutSymlinkedIsRenderedTest < Test::Unit::TestCase - def setup - @request = ActionController::TestRequest.new - @response = ActionController::TestResponse.new - end - +class LayoutSymlinkedIsRenderedTest < ActionController::TestCase def test_symlinked_layout_is_rendered @controller = LayoutSymlinkedTest.new get :hello diff --git a/actionpack/test/controller/mime_responds_test.rb b/actionpack/test/controller/mime_responds_test.rb index 0d508eb8df..dc59180a68 100644 --- a/actionpack/test/controller/mime_responds_test.rb +++ b/actionpack/test/controller/mime_responds_test.rb @@ -162,13 +162,11 @@ class RespondToController < ActionController::Base end end -class MimeControllerTest < Test::Unit::TestCase +class MimeControllerTest < ActionController::TestCase + tests RespondToController + def setup ActionController::Base.use_accept_header = true - @request = ActionController::TestRequest.new - @response = ActionController::TestResponse.new - - @controller = RespondToController.new @request.host = "www.example.com" end @@ -509,12 +507,10 @@ class SuperPostController < PostController end end -class MimeControllerLayoutsTest < Test::Unit::TestCase - def setup - @request = ActionController::TestRequest.new - @response = ActionController::TestResponse.new +class MimeControllerLayoutsTest < ActionController::TestCase + tests PostController - @controller = PostController.new + def setup @request.host = "www.example.com" end diff --git a/actionpack/test/controller/polymorphic_routes_test.rb b/actionpack/test/controller/polymorphic_routes_test.rb index 6ddf2826cd..df49a37f89 100644 --- a/actionpack/test/controller/polymorphic_routes_test.rb +++ b/actionpack/test/controller/polymorphic_routes_test.rb @@ -22,8 +22,7 @@ end class Response::Nested < Response; end uses_mocha 'polymorphic URL helpers' do - class PolymorphicRoutesTest < Test::Unit::TestCase - + class PolymorphicRoutesTest < ActiveSupport::TestCase include ActionController::PolymorphicRoutes def setup diff --git a/actionpack/test/controller/redirect_test.rb b/actionpack/test/controller/redirect_test.rb index c55307d645..27cedc91d2 100644 --- a/actionpack/test/controller/redirect_test.rb +++ b/actionpack/test/controller/redirect_test.rb @@ -103,12 +103,8 @@ class RedirectController < ActionController::Base end end -class RedirectTest < Test::Unit::TestCase - def setup - @controller = RedirectController.new - @request = ActionController::TestRequest.new - @response = ActionController::TestResponse.new - end +class RedirectTest < ActionController::TestCase + tests RedirectController def test_simple_redirect get :simple_redirect @@ -256,12 +252,8 @@ module ModuleTest end end - class ModuleRedirectTest < Test::Unit::TestCase - def setup - @controller = ModuleRedirectController.new - @request = ActionController::TestRequest.new - @response = ActionController::TestResponse.new - end + class ModuleRedirectTest < ActionController::TestCase + tests ModuleRedirectController def test_simple_redirect get :simple_redirect diff --git a/actionpack/test/controller/render_test.rb b/actionpack/test/controller/render_test.rb index df9376727f..6a03466db1 100644 --- a/actionpack/test/controller/render_test.rb +++ b/actionpack/test/controller/render_test.rb @@ -641,12 +641,10 @@ class TestController < ActionController::Base end end -class RenderTest < Test::Unit::TestCase - def setup - @request = ActionController::TestRequest.new - @response = ActionController::TestResponse.new - @controller = TestController.new +class RenderTest < ActionController::TestCase + tests TestController + def setup # enable a logger so that (e.g.) the benchmarking stuff runs, so we can get # a more accurate simulation of what happens in "real life". @controller.logger = Logger.new(nil) @@ -1333,12 +1331,10 @@ class RenderTest < Test::Unit::TestCase end end -class EtagRenderTest < Test::Unit::TestCase - def setup - @request = ActionController::TestRequest.new - @response = ActionController::TestResponse.new - @controller = TestController.new +class EtagRenderTest < ActionController::TestCase + tests TestController + def setup @request.host = "www.nextangle.com" @expected_bang_etag = etag_for(expand_key([:foo, 123])) end @@ -1430,12 +1426,10 @@ class EtagRenderTest < Test::Unit::TestCase end end -class LastModifiedRenderTest < Test::Unit::TestCase - def setup - @request = ActionController::TestRequest.new - @response = ActionController::TestResponse.new - @controller = TestController.new +class LastModifiedRenderTest < ActionController::TestCase + tests TestController + def setup @request.host = "www.nextangle.com" @last_modified = Time.now.utc.beginning_of_day.httpdate end @@ -1487,12 +1481,10 @@ class LastModifiedRenderTest < Test::Unit::TestCase end end -class RenderingLoggingTest < Test::Unit::TestCase - def setup - @request = ActionController::TestRequest.new - @response = ActionController::TestResponse.new - @controller = TestController.new +class RenderingLoggingTest < ActionController::TestCase + tests TestController + def setup @request.host = "www.nextangle.com" end diff --git a/actionpack/test/controller/request_forgery_protection_test.rb b/actionpack/test/controller/request_forgery_protection_test.rb index f7adaa7d4e..9dbfd120f2 100644 --- a/actionpack/test/controller/request_forgery_protection_test.rb +++ b/actionpack/test/controller/request_forgery_protection_test.rb @@ -222,7 +222,7 @@ end # OK let's get our test on -class RequestForgeryProtectionControllerTest < Test::Unit::TestCase +class RequestForgeryProtectionControllerTest < ActionController::TestCase include RequestForgeryProtectionTests def setup @controller = RequestForgeryProtectionController.new @@ -236,7 +236,7 @@ class RequestForgeryProtectionControllerTest < Test::Unit::TestCase end end -class RequestForgeryProtectionWithoutSecretControllerTest < Test::Unit::TestCase +class RequestForgeryProtectionWithoutSecretControllerTest < ActionController::TestCase def setup @controller = RequestForgeryProtectionWithoutSecretController.new @request = ActionController::TestRequest.new @@ -255,7 +255,7 @@ class RequestForgeryProtectionWithoutSecretControllerTest < Test::Unit::TestCase end end -class CsrfCookieMonsterControllerTest < Test::Unit::TestCase +class CsrfCookieMonsterControllerTest < ActionController::TestCase include RequestForgeryProtectionTests def setup @controller = CsrfCookieMonsterController.new @@ -271,7 +271,7 @@ class CsrfCookieMonsterControllerTest < Test::Unit::TestCase end end -class FreeCookieControllerTest < Test::Unit::TestCase +class FreeCookieControllerTest < ActionController::TestCase def setup @controller = FreeCookieController.new @request = ActionController::TestRequest.new @@ -296,7 +296,7 @@ class FreeCookieControllerTest < Test::Unit::TestCase end end -class SessionOffControllerTest < Test::Unit::TestCase +class SessionOffControllerTest < ActionController::TestCase def setup @controller = SessionOffController.new @request = ActionController::TestRequest.new diff --git a/actionpack/test/controller/request_test.rb b/actionpack/test/controller/request_test.rb index e79a0ea76b..2dc2ed9965 100644 --- a/actionpack/test/controller/request_test.rb +++ b/actionpack/test/controller/request_test.rb @@ -1,7 +1,7 @@ require 'abstract_unit' require 'action_controller/integration' -class RequestTest < Test::Unit::TestCase +class RequestTest < ActiveSupport::TestCase def setup ActionController::Base.relative_url_root = nil @request = ActionController::TestRequest.new @@ -400,7 +400,7 @@ class RequestTest < Test::Unit::TestCase end end -class UrlEncodedRequestParameterParsingTest < Test::Unit::TestCase +class UrlEncodedRequestParameterParsingTest < ActiveSupport::TestCase def setup @query_string = "action=create_customer&full_name=David%20Heinemeier%20Hansson&customerId=1" @query_string_with_empty = "action=create_customer&full_name=" @@ -704,20 +704,20 @@ class UrlEncodedRequestParameterParsingTest < Test::Unit::TestCase end end -class MultipartRequestParameterParsingTest < Test::Unit::TestCase +class MultipartRequestParameterParsingTest < ActiveSupport::TestCase FIXTURE_PATH = File.dirname(__FILE__) + '/../fixtures/multipart' def test_single_parameter - params = process('single_parameter') + params = parse_multipart('single_parameter') assert_equal({ 'foo' => 'bar' }, params) end def test_bracketed_param - assert_equal({ 'foo' => { 'baz' => 'bar'}}, process('bracketed_param')) + assert_equal({ 'foo' => { 'baz' => 'bar'}}, parse_multipart('bracketed_param')) end def test_text_file - params = process('text_file') + params = parse_multipart('text_file') assert_equal %w(file foo), params.keys.sort assert_equal 'bar', params['foo'] @@ -729,7 +729,7 @@ class MultipartRequestParameterParsingTest < Test::Unit::TestCase end def test_boundary_problem_file - params = process('boundary_problem_file') + params = parse_multipart('boundary_problem_file') assert_equal %w(file foo), params.keys.sort file = params['file'] @@ -748,7 +748,7 @@ class MultipartRequestParameterParsingTest < Test::Unit::TestCase end def test_large_text_file - params = process('large_text_file') + params = parse_multipart('large_text_file') assert_equal %w(file foo), params.keys.sort assert_equal 'bar', params['foo'] @@ -774,7 +774,7 @@ class MultipartRequestParameterParsingTest < Test::Unit::TestCase end def test_binary_file - params = process('binary_file') + params = parse_multipart('binary_file') assert_equal %w(file flowers foo), params.keys.sort assert_equal 'bar', params['foo'] @@ -793,7 +793,7 @@ class MultipartRequestParameterParsingTest < Test::Unit::TestCase end def test_mixed_files - params = process('mixed_files') + params = parse_multipart('mixed_files') assert_equal %w(files foo), params.keys.sort assert_equal 'bar', params['foo'] @@ -805,7 +805,7 @@ class MultipartRequestParameterParsingTest < Test::Unit::TestCase end private - def process(name) + def parse_multipart(name) File.open(File.join(FIXTURE_PATH, name), 'rb') do |file| params = ActionController::AbstractRequest.parse_multipart_form_parameters(file, 'AaB03x', file.stat.size, {}) assert_equal 0, file.pos # file was rewound after reading @@ -814,7 +814,7 @@ class MultipartRequestParameterParsingTest < Test::Unit::TestCase end end -class XmlParamsParsingTest < Test::Unit::TestCase +class XmlParamsParsingTest < ActiveSupport::TestCase def test_hash_params person = parse_body("<person><name>David</name></person>")[:person] assert_kind_of Hash, person @@ -868,7 +868,7 @@ class LegacyXmlParamsParsingTest < XmlParamsParsingTest end end -class JsonParamsParsingTest < Test::Unit::TestCase +class JsonParamsParsingTest < ActiveSupport::TestCase def test_hash_params_for_application_json person = parse_body({:person => {:name => "David"}}.to_json,'application/json')[:person] assert_kind_of Hash, person diff --git a/actionpack/test/controller/resources_test.rb b/actionpack/test/controller/resources_test.rb index 1fea82e564..3e656fa51b 100644 --- a/actionpack/test/controller/resources_test.rb +++ b/actionpack/test/controller/resources_test.rb @@ -27,7 +27,7 @@ module Backoffice end end -class ResourcesTest < Test::Unit::TestCase +class ResourcesTest < ActionController::TestCase # The assertions in these tests are incompatible with the hash method # optimisation. This could indicate user level problems def setup diff --git a/actionpack/test/controller/test_test.rb b/actionpack/test/controller/test_test.rb index a23428804a..02eb447f31 100644 --- a/actionpack/test/controller/test_test.rb +++ b/actionpack/test/controller/test_test.rb @@ -1,7 +1,7 @@ require 'abstract_unit' require 'controller/fake_controllers' -class TestTest < Test::Unit::TestCase +class TestTest < ActionController::TestCase class TestController < ActionController::Base def no_op render :text => 'dummy' @@ -24,7 +24,7 @@ class TestTest < Test::Unit::TestCase end def render_raw_post - raise Test::Unit::AssertionFailedError, "#raw_post is blank" if request.raw_post.blank? + raise ActiveSupport::TestCase::Assertion, "#raw_post is blank" if request.raw_post.blank? render :text => request.raw_post end @@ -580,7 +580,7 @@ XML assert_equal @response.redirect_url, redirect_to_url # Must be a :redirect response. - assert_raise(Test::Unit::AssertionFailedError) do + assert_raise(ActiveSupport::TestCase::Assertion) do assert_redirected_to 'created resource' end end @@ -602,9 +602,9 @@ XML end end -class CleanBacktraceTest < Test::Unit::TestCase +class CleanBacktraceTest < ActionController::TestCase def test_should_reraise_the_same_object - exception = Test::Unit::AssertionFailedError.new('message') + exception = ActiveSupport::TestCase::Assertion.new('message') clean_backtrace { raise exception } rescue => caught assert_equal exception.object_id, caught.object_id @@ -613,7 +613,7 @@ class CleanBacktraceTest < Test::Unit::TestCase def test_should_clean_assertion_lines_from_backtrace path = File.expand_path("#{File.dirname(__FILE__)}/../../lib/action_controller") - exception = Test::Unit::AssertionFailedError.new('message') + exception = ActiveSupport::TestCase::Assertion.new('message') exception.set_backtrace ["#{path}/abc", "#{path}/assertions/def"] clean_backtrace { raise exception } rescue => caught @@ -629,21 +629,17 @@ class CleanBacktraceTest < Test::Unit::TestCase end end -class InferringClassNameTest < Test::Unit::TestCase +class InferringClassNameTest < ActionController::TestCase def test_determine_controller_class assert_equal ContentController, determine_class("ContentControllerTest") end def test_determine_controller_class_with_nonsense_name - assert_raises ActionController::NonInferrableControllerError do - determine_class("HelloGoodBye") - end + assert_nil determine_class("HelloGoodBye") end def test_determine_controller_class_with_sensible_name_where_no_controller_exists - assert_raises ActionController::NonInferrableControllerError do - determine_class("NoControllerWithThisNameTest") - end + assert_nil determine_class("NoControllerWithThisNameTest") end private diff --git a/actionpack/test/controller/url_rewriter_test.rb b/actionpack/test/controller/url_rewriter_test.rb index 64e9a085ca..8bc343e2ea 100644 --- a/actionpack/test/controller/url_rewriter_test.rb +++ b/actionpack/test/controller/url_rewriter_test.rb @@ -2,7 +2,7 @@ require 'abstract_unit' ActionController::UrlRewriter -class UrlRewriterTests < Test::Unit::TestCase +class UrlRewriterTests < ActionController::TestCase def setup @request = ActionController::TestRequest.new @params = {} @@ -85,8 +85,7 @@ class UrlRewriterTests < Test::Unit::TestCase end end -class UrlWriterTests < Test::Unit::TestCase - +class UrlWriterTests < ActionController::TestCase class W include ActionController::UrlWriter end diff --git a/actionpack/test/controller/verification_test.rb b/actionpack/test/controller/verification_test.rb index b289443129..418a81baa8 100644 --- a/actionpack/test/controller/verification_test.rb +++ b/actionpack/test/controller/verification_test.rb @@ -1,6 +1,6 @@ require 'abstract_unit' -class VerificationTest < Test::Unit::TestCase +class VerificationTest < ActionController::TestCase class TestController < ActionController::Base verify :only => :guarded_one, :params => "one", :add_flash => { :error => 'unguarded' }, diff --git a/actionpack/test/controller/view_paths_test.rb b/actionpack/test/controller/view_paths_test.rb index b859a92cbd..04e14d8eac 100644 --- a/actionpack/test/controller/view_paths_test.rb +++ b/actionpack/test/controller/view_paths_test.rb @@ -1,6 +1,6 @@ require 'abstract_unit' -class ViewLoadPathsTest < Test::Unit::TestCase +class ViewLoadPathsTest < ActionController::TestCase class TestController < ActionController::Base def self.controller_path() "test" end def rescue_action(e) raise end diff --git a/actionpack/test/template/atom_feed_helper_test.rb b/actionpack/test/template/atom_feed_helper_test.rb index 06af8d1d6a..8a00a397ca 100644 --- a/actionpack/test/template/atom_feed_helper_test.rb +++ b/actionpack/test/template/atom_feed_helper_test.rb @@ -166,12 +166,10 @@ class ScrollsController < ActionController::Base end end -class AtomFeedTest < Test::Unit::TestCase - def setup - @request = ActionController::TestRequest.new - @response = ActionController::TestResponse.new - @controller = ScrollsController.new +class AtomFeedTest < ActionController::TestCase + tests ScrollsController + def setup @request.host = "www.nextangle.com" end diff --git a/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb b/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb index 97c6cd4331..189c6c7b5a 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb @@ -31,13 +31,13 @@ module ActiveRecord # Returns an array of arrays containing the field values. # Order is the same as that returned by +columns+. def select_rows(sql, name = nil) - raise NotImplementedError, "select_rows is an abstract method" end + undef_method :select_rows # Executes the SQL statement in the context of this connection. - def execute(sql, name = nil) - raise NotImplementedError, "execute is an abstract method" + def execute(sql, name = nil, skip_logging = false) end + undef_method :execute # Returns the last auto-generated ID from the affected table. def insert(sql, name = nil, pk = nil, id_value = nil, sequence_name = nil) @@ -163,8 +163,8 @@ module ActiveRecord # Returns an array of record hashes with the column names as keys and # column values as values. def select(sql, name = nil) - raise NotImplementedError, "select is an abstract method" end + undef_method :select # Returns the last auto-generated ID from the affected table. def insert_sql(sql, name = nil, pk = nil, id_value = nil, sequence_name = nil) diff --git a/activerecord/lib/active_record/fixtures.rb b/activerecord/lib/active_record/fixtures.rb index 114141a646..24aabf0359 100644 --- a/activerecord/lib/active_record/fixtures.rb +++ b/activerecord/lib/active_record/fixtures.rb @@ -813,186 +813,190 @@ class Fixture #:nodoc: end end -module Test #:nodoc: - module Unit #:nodoc: - class TestCase #:nodoc: - setup :setup_fixtures - teardown :teardown_fixtures - - superclass_delegating_accessor :fixture_path - superclass_delegating_accessor :fixture_table_names - superclass_delegating_accessor :fixture_class_names - superclass_delegating_accessor :use_transactional_fixtures - superclass_delegating_accessor :use_instantiated_fixtures # true, false, or :no_instances - superclass_delegating_accessor :pre_loaded_fixtures - - self.fixture_table_names = [] - self.use_transactional_fixtures = false - self.use_instantiated_fixtures = true - self.pre_loaded_fixtures = false - - @@already_loaded_fixtures = {} - self.fixture_class_names = {} - - class << self - def set_fixture_class(class_names = {}) - self.fixture_class_names = self.fixture_class_names.merge(class_names) - end +module ActiveRecord + module TestFixtures + def self.included(base) + base.class_eval do + setup :setup_fixtures + teardown :teardown_fixtures + + superclass_delegating_accessor :fixture_path + superclass_delegating_accessor :fixture_table_names + superclass_delegating_accessor :fixture_class_names + superclass_delegating_accessor :use_transactional_fixtures + superclass_delegating_accessor :use_instantiated_fixtures # true, false, or :no_instances + superclass_delegating_accessor :pre_loaded_fixtures + + self.fixture_table_names = [] + self.use_transactional_fixtures = false + self.use_instantiated_fixtures = true + self.pre_loaded_fixtures = false + + @@already_loaded_fixtures = {} + self.fixture_class_names = {} + end - def fixtures(*table_names) - if table_names.first == :all - table_names = Dir["#{fixture_path}/*.yml"] + Dir["#{fixture_path}/*.csv"] - table_names.map! { |f| File.basename(f).split('.')[0..-2].join('.') } - else - table_names = table_names.flatten.map { |n| n.to_s } - end + base.extend ClassMethods + end + + module ClassMethods + def set_fixture_class(class_names = {}) + self.fixture_class_names = self.fixture_class_names.merge(class_names) + end - self.fixture_table_names |= table_names - require_fixture_classes(table_names) - setup_fixture_accessors(table_names) + def fixtures(*table_names) + if table_names.first == :all + table_names = Dir["#{fixture_path}/*.yml"] + Dir["#{fixture_path}/*.csv"] + table_names.map! { |f| File.basename(f).split('.')[0..-2].join('.') } + else + table_names = table_names.flatten.map { |n| n.to_s } end - def try_to_load_dependency(file_name) - require_dependency file_name - rescue LoadError => e - # Let's hope the developer has included it himself + self.fixture_table_names |= table_names + require_fixture_classes(table_names) + setup_fixture_accessors(table_names) + end + + def try_to_load_dependency(file_name) + require_dependency file_name + rescue LoadError => e + # Let's hope the developer has included it himself - # Let's warn in case this is a subdependency, otherwise - # subdependency error messages are totally cryptic - if ActiveRecord::Base.logger - ActiveRecord::Base.logger.warn("Unable to load #{file_name}, underlying cause #{e.message} \n\n #{e.backtrace.join("\n")}") - end + # Let's warn in case this is a subdependency, otherwise + # subdependency error messages are totally cryptic + if ActiveRecord::Base.logger + ActiveRecord::Base.logger.warn("Unable to load #{file_name}, underlying cause #{e.message} \n\n #{e.backtrace.join("\n")}") end + end - def require_fixture_classes(table_names = nil) - (table_names || fixture_table_names).each do |table_name| - file_name = table_name.to_s - file_name = file_name.singularize if ActiveRecord::Base.pluralize_table_names - try_to_load_dependency(file_name) - end + def require_fixture_classes(table_names = nil) + (table_names || fixture_table_names).each do |table_name| + file_name = table_name.to_s + file_name = file_name.singularize if ActiveRecord::Base.pluralize_table_names + try_to_load_dependency(file_name) end + end - def setup_fixture_accessors(table_names = nil) - table_names = [table_names] if table_names && !table_names.respond_to?(:each) - (table_names || fixture_table_names).each do |table_name| - table_name = table_name.to_s.tr('.', '_') + def setup_fixture_accessors(table_names = nil) + table_names = [table_names] if table_names && !table_names.respond_to?(:each) + (table_names || fixture_table_names).each do |table_name| + table_name = table_name.to_s.tr('.', '_') - define_method(table_name) do |*fixtures| - force_reload = fixtures.pop if fixtures.last == true || fixtures.last == :reload + define_method(table_name) do |*fixtures| + force_reload = fixtures.pop if fixtures.last == true || fixtures.last == :reload - @fixture_cache[table_name] ||= {} + @fixture_cache[table_name] ||= {} - instances = fixtures.map do |fixture| - @fixture_cache[table_name].delete(fixture) if force_reload + instances = fixtures.map do |fixture| + @fixture_cache[table_name].delete(fixture) if force_reload - if @loaded_fixtures[table_name][fixture.to_s] - @fixture_cache[table_name][fixture] ||= @loaded_fixtures[table_name][fixture.to_s].find - else - raise StandardError, "No fixture with name '#{fixture}' found for table '#{table_name}'" - end + if @loaded_fixtures[table_name][fixture.to_s] + @fixture_cache[table_name][fixture] ||= @loaded_fixtures[table_name][fixture.to_s].find + else + raise StandardError, "No fixture with name '#{fixture}' found for table '#{table_name}'" end - - instances.size == 1 ? instances.first : instances end - end - end - def uses_transaction(*methods) - @uses_transaction = [] unless defined?(@uses_transaction) - @uses_transaction.concat methods.map(&:to_s) + instances.size == 1 ? instances.first : instances + end end + end - def uses_transaction?(method) - @uses_transaction = [] unless defined?(@uses_transaction) - @uses_transaction.include?(method.to_s) - end + def uses_transaction(*methods) + @uses_transaction = [] unless defined?(@uses_transaction) + @uses_transaction.concat methods.map(&:to_s) end - def use_transactional_fixtures? - use_transactional_fixtures && - !self.class.uses_transaction?(method_name) + def uses_transaction?(method) + @uses_transaction = [] unless defined?(@uses_transaction) + @uses_transaction.include?(method.to_s) end + end - def setup_fixtures - return unless defined?(ActiveRecord) && !ActiveRecord::Base.configurations.blank? + def use_transactional_fixtures? + use_transactional_fixtures && + !self.class.uses_transaction?(method_name) + end - if pre_loaded_fixtures && !use_transactional_fixtures - raise RuntimeError, 'pre_loaded_fixtures requires use_transactional_fixtures' - end + def setup_fixtures + return unless defined?(ActiveRecord) && !ActiveRecord::Base.configurations.blank? - @fixture_cache = {} + if pre_loaded_fixtures && !use_transactional_fixtures + raise RuntimeError, 'pre_loaded_fixtures requires use_transactional_fixtures' + end - # Load fixtures once and begin transaction. - if use_transactional_fixtures? - if @@already_loaded_fixtures[self.class] - @loaded_fixtures = @@already_loaded_fixtures[self.class] - else - load_fixtures - @@already_loaded_fixtures[self.class] = @loaded_fixtures - end - ActiveRecord::Base.connection.increment_open_transactions - ActiveRecord::Base.connection.begin_db_transaction - # Load fixtures for every test. + @fixture_cache = {} + + # Load fixtures once and begin transaction. + if use_transactional_fixtures? + if @@already_loaded_fixtures[self.class] + @loaded_fixtures = @@already_loaded_fixtures[self.class] else - Fixtures.reset_cache - @@already_loaded_fixtures[self.class] = nil load_fixtures + @@already_loaded_fixtures[self.class] = @loaded_fixtures end - - # Instantiate fixtures for every test if requested. - instantiate_fixtures if use_instantiated_fixtures + ActiveRecord::Base.connection.increment_open_transactions + ActiveRecord::Base.connection.begin_db_transaction + # Load fixtures for every test. + else + Fixtures.reset_cache + @@already_loaded_fixtures[self.class] = nil + load_fixtures end - def teardown_fixtures - return unless defined?(ActiveRecord) && !ActiveRecord::Base.configurations.blank? + # Instantiate fixtures for every test if requested. + instantiate_fixtures if use_instantiated_fixtures + end + + def teardown_fixtures + return unless defined?(ActiveRecord) && !ActiveRecord::Base.configurations.blank? - unless use_transactional_fixtures? - Fixtures.reset_cache - end + unless use_transactional_fixtures? + Fixtures.reset_cache + end - # Rollback changes if a transaction is active. - if use_transactional_fixtures? && ActiveRecord::Base.connection.open_transactions != 0 - ActiveRecord::Base.connection.rollback_db_transaction - ActiveRecord::Base.connection.decrement_open_transactions - end - ActiveRecord::Base.clear_active_connections! + # Rollback changes if a transaction is active. + if use_transactional_fixtures? && ActiveRecord::Base.connection.open_transactions != 0 + ActiveRecord::Base.connection.rollback_db_transaction + ActiveRecord::Base.connection.decrement_open_transactions end + ActiveRecord::Base.clear_active_connections! + end - private - def load_fixtures - @loaded_fixtures = {} - fixtures = Fixtures.create_fixtures(fixture_path, fixture_table_names, fixture_class_names) - unless fixtures.nil? - if fixtures.instance_of?(Fixtures) - @loaded_fixtures[fixtures.name] = fixtures - else - fixtures.each { |f| @loaded_fixtures[f.name] = f } - end + private + def load_fixtures + @loaded_fixtures = {} + fixtures = Fixtures.create_fixtures(fixture_path, fixture_table_names, fixture_class_names) + unless fixtures.nil? + if fixtures.instance_of?(Fixtures) + @loaded_fixtures[fixtures.name] = fixtures + else + fixtures.each { |f| @loaded_fixtures[f.name] = f } end end + end - # for pre_loaded_fixtures, only require the classes once. huge speed improvement - @@required_fixture_classes = false + # for pre_loaded_fixtures, only require the classes once. huge speed improvement + @@required_fixture_classes = false - def instantiate_fixtures - if pre_loaded_fixtures - raise RuntimeError, 'Load fixtures before instantiating them.' if Fixtures.all_loaded_fixtures.empty? - unless @@required_fixture_classes - self.class.require_fixture_classes Fixtures.all_loaded_fixtures.keys - @@required_fixture_classes = true - end - Fixtures.instantiate_all_loaded_fixtures(self, load_instances?) - else - raise RuntimeError, 'Load fixtures before instantiating them.' if @loaded_fixtures.nil? - @loaded_fixtures.each do |table_name, fixtures| - Fixtures.instantiate_fixtures(self, table_name, fixtures, load_instances?) - end + def instantiate_fixtures + if pre_loaded_fixtures + raise RuntimeError, 'Load fixtures before instantiating them.' if Fixtures.all_loaded_fixtures.empty? + unless @@required_fixture_classes + self.class.require_fixture_classes Fixtures.all_loaded_fixtures.keys + @@required_fixture_classes = true + end + Fixtures.instantiate_all_loaded_fixtures(self, load_instances?) + else + raise RuntimeError, 'Load fixtures before instantiating them.' if @loaded_fixtures.nil? + @loaded_fixtures.each do |table_name, fixtures| + Fixtures.instantiate_fixtures(self, table_name, fixtures, load_instances?) end end + end - def load_instances? - use_instantiated_fixtures != :no_instances - end - end + def load_instances? + use_instantiated_fixtures != :no_instances + end end end diff --git a/activerecord/lib/active_record/test_case.rb b/activerecord/lib/active_record/test_case.rb index eabf06fc3b..588cf65156 100644 --- a/activerecord/lib/active_record/test_case.rb +++ b/activerecord/lib/active_record/test_case.rb @@ -1,7 +1,10 @@ require "active_support/test_case" +require "active_record/fixtures" module ActiveRecord class TestCase < ActiveSupport::TestCase #:nodoc: + include TestFixtures + self.fixture_path = FIXTURES_ROOT self.use_instantiated_fixtures = false self.use_transactional_fixtures = true diff --git a/activerecord/test/cases/fixtures_test.rb b/activerecord/test/cases/fixtures_test.rb index 6ba7597f56..ed2915b023 100644 --- a/activerecord/test/cases/fixtures_test.rb +++ b/activerecord/test/cases/fixtures_test.rb @@ -641,15 +641,15 @@ end class FixtureLoadingTest < ActiveRecord::TestCase uses_mocha 'reloading_fixtures_through_accessor_methods' do def test_logs_message_for_failed_dependency_load - Test::Unit::TestCase.expects(:require_dependency).with(:does_not_exist).raises(LoadError) + ActiveRecord::TestCase.expects(:require_dependency).with(:does_not_exist).raises(LoadError) ActiveRecord::Base.logger.expects(:warn) - Test::Unit::TestCase.try_to_load_dependency(:does_not_exist) + ActiveRecord::TestCase.try_to_load_dependency(:does_not_exist) end def test_does_not_logs_message_for_successful_dependency_load - Test::Unit::TestCase.expects(:require_dependency).with(:works_out_fine) + ActiveRecord::TestCase.expects(:require_dependency).with(:works_out_fine) ActiveRecord::Base.logger.expects(:warn).never - Test::Unit::TestCase.try_to_load_dependency(:works_out_fine) + ActiveRecord::TestCase.try_to_load_dependency(:works_out_fine) end end end diff --git a/activerecord/test/cases/helper.rb b/activerecord/test/cases/helper.rb index f7bdac8013..13988d5392 100644 --- a/activerecord/test/cases/helper.rb +++ b/activerecord/test/cases/helper.rb @@ -5,7 +5,6 @@ require 'config' require 'test/unit' require 'active_record' -require 'active_record/fixtures' require 'active_record/test_case' require 'connection' @@ -48,15 +47,11 @@ class << ActiveRecord::Base end unless ENV['FIXTURE_DEBUG'] - module Test #:nodoc: - module Unit #:nodoc: - class << TestCase #:nodoc: - def try_to_load_dependency_with_silence(*args) - ActiveRecord::Base.logger.silence { try_to_load_dependency_without_silence(*args)} - end - - alias_method_chain :try_to_load_dependency, :silence - end + module ActiveRecord::TestFixtures::ClassMethods + def try_to_load_dependency_with_silence(*args) + ActiveRecord::Base.logger.silence { try_to_load_dependency_without_silence(*args)} end + + alias_method_chain :try_to_load_dependency, :silence end -end
\ No newline at end of file +end diff --git a/activerecord/test/cases/validations_i18n_test.rb b/activerecord/test/cases/validations_i18n_test.rb index 42246f18b6..b2df98ca0a 100644 --- a/activerecord/test/cases/validations_i18n_test.rb +++ b/activerecord/test/cases/validations_i18n_test.rb @@ -2,7 +2,7 @@ require "cases/helper" require 'models/topic' require 'models/reply' -class ActiveRecordValidationsI18nTests < Test::Unit::TestCase +class ActiveRecordValidationsI18nTests < ActiveSupport::TestCase def setup reset_callbacks Topic @topic = Topic.new diff --git a/activeresource/test/format_test.rb b/activeresource/test/format_test.rb index a564ee01b5..01cc162075 100644 --- a/activeresource/test/format_test.rb +++ b/activeresource/test/format_test.rb @@ -90,7 +90,7 @@ class FormatTest < Test::Unit::TestCase [:json, :xml].each do |format| encoded_person = ActiveResource::Formats[format].encode(person) - assert_match /12345 Street/, encoded_person + assert_match(/12345 Street/, encoded_person) remote_person = Person.new(person.update({:address => StreetAddress.new(address)})) assert_kind_of StreetAddress, remote_person.address using_format(Person, format) do diff --git a/activesupport/lib/active_support/deprecation.rb b/activesupport/lib/active_support/deprecation.rb index b4d8f61b8c..b3ad599371 100644 --- a/activesupport/lib/active_support/deprecation.rb +++ b/activesupport/lib/active_support/deprecation.rb @@ -221,23 +221,30 @@ class Module include ActiveSupport::Deprecation::ClassMethods end -require 'test/unit/error' -module Test - module Unit - class TestCase - include ActiveSupport::Deprecation::Assertions - end +require 'active_support/test_case' + +class ActiveSupport::TestCase + include ActiveSupport::Deprecation::Assertions +end + +begin + require 'test/unit/error' - class Error # :nodoc: - # Silence warnings when reporting test errors. - def message_with_silenced_deprecation - ActiveSupport::Deprecation.silence do - message_without_silenced_deprecation + module Test + module Unit + class Error # :nodoc: + # Silence warnings when reporting test errors. + def message_with_silenced_deprecation + ActiveSupport::Deprecation.silence do + message_without_silenced_deprecation + end end - end - alias_method_chain :message, :silenced_deprecation + alias_method_chain :message, :silenced_deprecation + end end end +rescue LoadError + # Using miniunit, ignore. end diff --git a/activesupport/lib/active_support/test_case.rb b/activesupport/lib/active_support/test_case.rb index 197e73b3e8..f47329d026 100644 --- a/activesupport/lib/active_support/test_case.rb +++ b/activesupport/lib/active_support/test_case.rb @@ -1,24 +1,36 @@ -require 'test/unit/testcase' -require 'active_support/testing/default' -require 'active_support/testing/core_ext/test' - +require 'active_support/testing/setup_and_teardown' +require 'active_support/testing/assertions' +require 'active_support/testing/declarative' module ActiveSupport - class TestCase < Test::Unit::TestCase - # test "verify something" do - # ... - # end - def self.test(name, &block) - test_name = "test_#{name.gsub(/\s+/,'_')}".to_sym - defined = instance_method(test_name) rescue false - raise "#{test_name} is already defined in #{self}" if defined - if block_given? - define_method(test_name, &block) - else - define_method(test_name) do - flunk "No implementation provided for #{name}" - end - end + # Prefer MiniTest with Test::Unit compatibility. + begin + require 'minitest/unit' + + # Hack around the test/unit autorun. + autorun_enabled = MiniTest::Unit.class_variable_get('@@installed_at_exit') + MiniTest::Unit.disable_autorun + require 'test/unit' + MiniTest::Unit.class_variable_set('@@installed_at_exit', autorun_enabled) + + class TestCase < ::Test::Unit::TestCase + Assertion = MiniTest::Assertion + end + + # Test::Unit compatibility. + rescue LoadError + require 'test/unit/testcase' + require 'active_support/testing/default' + + class TestCase < ::Test::Unit::TestCase + Assertion = Test::Unit::AssertionFailedError + include ActiveSupport::Testing::Default end end + + class TestCase + include ActiveSupport::Testing::SetupAndTeardown + include ActiveSupport::Testing::Assertions + extend ActiveSupport::Testing::Declarative + end end diff --git a/activesupport/lib/active_support/testing/assertions.rb b/activesupport/lib/active_support/testing/assertions.rb new file mode 100644 index 0000000000..ce2f44efd6 --- /dev/null +++ b/activesupport/lib/active_support/testing/assertions.rb @@ -0,0 +1,61 @@ +module ActiveSupport + module Testing + module Assertions + # Test numeric difference between the return value of an expression as a result of what is evaluated + # in the yielded block. + # + # assert_difference 'Article.count' do + # post :create, :article => {...} + # end + # + # An arbitrary expression is passed in and evaluated. + # + # assert_difference 'assigns(:article).comments(:reload).size' do + # post :create, :comment => {...} + # end + # + # An arbitrary positive or negative difference can be specified. The default is +1. + # + # assert_difference 'Article.count', -1 do + # post :delete, :id => ... + # end + # + # An array of expressions can also be passed in and evaluated. + # + # assert_difference [ 'Article.count', 'Post.count' ], +2 do + # post :create, :article => {...} + # end + # + # A error message can be specified. + # + # assert_difference 'Article.count', -1, "An Article should be destroyed" do + # post :delete, :id => ... + # end + def assert_difference(expressions, difference = 1, message = nil, &block) + expression_evaluations = Array(expressions).collect{ |expression| lambda { eval(expression, block.send(:binding)) } } + + original_values = expression_evaluations.inject([]) { |memo, expression| memo << expression.call } + yield + expression_evaluations.each_with_index do |expression, i| + assert_equal original_values[i] + difference, expression.call, message + end + end + + # Assertion that the numeric result of evaluating an expression is not changed before and after + # invoking the passed in block. + # + # assert_no_difference 'Article.count' do + # post :create, :article => invalid_attributes + # end + # + # A error message can be specified. + # + # assert_no_difference 'Article.count', "An Article should not be destroyed" do + # post :create, :article => invalid_attributes + # end + def assert_no_difference(expressions, message = nil, &block) + assert_difference expressions, 0, message, &block + end + end + end +end diff --git a/activesupport/lib/active_support/testing/declarative.rb b/activesupport/lib/active_support/testing/declarative.rb new file mode 100644 index 0000000000..cb6a5844eb --- /dev/null +++ b/activesupport/lib/active_support/testing/declarative.rb @@ -0,0 +1,21 @@ +module ActiveSupport + module Testing + module Declarative + # test "verify something" do + # ... + # end + def test(name, &block) + test_name = "test_#{name.gsub(/\s+/,'_')}".to_sym + defined = instance_method(test_name) rescue false + raise "#{test_name} is already defined in #{self}" if defined + if block_given? + define_method(test_name, &block) + else + define_method(test_name) do + flunk "No implementation provided for #{name}" + end + end + end + end + end +end diff --git a/activesupport/lib/active_support/testing/setup_and_teardown.rb b/activesupport/lib/active_support/testing/setup_and_teardown.rb index a514b61fea..c70e149c16 100644 --- a/activesupport/lib/active_support/testing/setup_and_teardown.rb +++ b/activesupport/lib/active_support/testing/setup_and_teardown.rb @@ -14,9 +14,8 @@ module ActiveSupport include ActiveSupport::Callbacks define_callbacks :setup, :teardown - if defined?(::Mini) - undef_method :run - alias_method :run, :run_with_callbacks_and_miniunit + if defined?(::MiniTest) + include ForMiniTest else begin require 'mocha' @@ -30,22 +29,23 @@ module ActiveSupport end end - def run_with_callbacks_and_miniunit(runner) - result = '.' - begin - run_callbacks :setup - result = super - rescue Exception => e - result = runner.puke(self.class, self.name, e) - ensure + module ForMiniTest + def run(runner) + result = '.' begin - teardown - run_callbacks :teardown, :enumerator => :reverse_each + run_callbacks :setup + result = super rescue Exception => e result = runner.puke(self.class, self.name, e) + ensure + begin + run_callbacks :teardown, :enumerator => :reverse_each + rescue Exception => e + result = runner.puke(self.class, self.name, e) + end end + result end - result end # This redefinition is unfortunate but test/unit shows us no alternative. diff --git a/activesupport/test/abstract_unit.rb b/activesupport/test/abstract_unit.rb index 9d8c252f86..a11ff25e41 100644 --- a/activesupport/test/abstract_unit.rb +++ b/activesupport/test/abstract_unit.rb @@ -5,6 +5,7 @@ require 'test/unit' $:.unshift "#{File.dirname(__FILE__)}/../lib" $:.unshift File.dirname(__FILE__) require 'active_support' +require 'active_support/test_case' if RUBY_VERSION < '1.9' $KCODE = 'UTF8' diff --git a/activesupport/test/core_ext/duration_test.rb b/activesupport/test/core_ext/duration_test.rb index 5cd52d8d79..6fe0a98d39 100644 --- a/activesupport/test/core_ext/duration_test.rb +++ b/activesupport/test/core_ext/duration_test.rb @@ -1,6 +1,6 @@ require 'abstract_unit' -class DurationTest < Test::Unit::TestCase +class DurationTest < ActiveSupport::TestCase def test_inspect assert_equal '1 month', 1.month.inspect assert_equal '1 month and 1 day', (1.month + 1.day).inspect diff --git a/activesupport/test/core_ext/module/model_naming_test.rb b/activesupport/test/core_ext/module/model_naming_test.rb index fc73fa5c36..d08349dd97 100644 --- a/activesupport/test/core_ext/module/model_naming_test.rb +++ b/activesupport/test/core_ext/module/model_naming_test.rb @@ -2,18 +2,18 @@ require 'abstract_unit' class ModelNamingTest < Test::Unit::TestCase def setup - @name = ActiveSupport::ModelName.new('Post::TrackBack') + @model_name = ActiveSupport::ModelName.new('Post::TrackBack') end def test_singular - assert_equal 'post_track_back', @name.singular + assert_equal 'post_track_back', @model_name.singular end def test_plural - assert_equal 'post_track_backs', @name.plural + assert_equal 'post_track_backs', @model_name.plural end def test_partial_path - assert_equal 'post/track_backs/track_back', @name.partial_path + assert_equal 'post/track_backs/track_back', @model_name.partial_path end end diff --git a/activesupport/test/deprecation_test.rb b/activesupport/test/deprecation_test.rb index 27e9573ce2..73a1f9959c 100644 --- a/activesupport/test/deprecation_test.rb +++ b/activesupport/test/deprecation_test.rb @@ -32,7 +32,7 @@ class Deprecatee end -class DeprecationTest < Test::Unit::TestCase +class DeprecationTest < ActiveSupport::TestCase def setup # Track the last warning. @old_behavior = ActiveSupport::Deprecation.behavior @@ -143,19 +143,21 @@ class DeprecationTest < Test::Unit::TestCase assert_deprecated(/you now need to do something extra for this one/) { @dtc.d } end - def test_assertion_failed_error_doesnt_spout_deprecation_warnings - error_class = Class.new(StandardError) do - def message - ActiveSupport::Deprecation.warn 'warning in error message' - super + unless defined?(::MiniTest) + def test_assertion_failed_error_doesnt_spout_deprecation_warnings + error_class = Class.new(StandardError) do + def message + ActiveSupport::Deprecation.warn 'warning in error message' + super + end end - end - raise error_class.new('hmm') + raise error_class.new('hmm') - rescue => e - error = Test::Unit::Error.new('testing ur doodz', e) - assert_not_deprecated { error.message } - assert_nil @last_message + rescue => e + error = Test::Unit::Error.new('testing ur doodz', e) + assert_not_deprecated { error.message } + assert_nil @last_message + end end end diff --git a/activesupport/test/test_test.rb b/activesupport/test/test_test.rb index 4e253848f6..89fae1a9e4 100644 --- a/activesupport/test/test_test.rb +++ b/activesupport/test/test_test.rb @@ -1,7 +1,6 @@ require 'abstract_unit' -require 'active_support/test_case' -class AssertDifferenceTest < Test::Unit::TestCase +class AssertDifferenceTest < ActiveSupport::TestCase def setup @object = Class.new do attr_accessor :num @@ -66,8 +65,8 @@ class AssertDifferenceTest < Test::Unit::TestCase @object.increment end fail 'should not get to here' - rescue Test::Unit::AssertionFailedError => e - assert_equal "<1 + 1> was the expression that failed.\n<3> expected but was\n<2>.", e.message + rescue Exception => e + assert_equal "<3> expected but was\n<2>.", e.message end def test_array_of_expressions_identify_failure_when_message_provided @@ -75,8 +74,8 @@ class AssertDifferenceTest < Test::Unit::TestCase @object.increment end fail 'should not get to here' - rescue Test::Unit::AssertionFailedError => e - assert_equal "something went wrong.\n<1 + 1> was the expression that failed.\n<3> expected but was\n<2>.", e.message + rescue Exception => e + assert_equal "something went wrong.\n<3> expected but was\n<2>.", e.message end else def default_test; end @@ -84,15 +83,17 @@ class AssertDifferenceTest < Test::Unit::TestCase end # These should always pass -class NotTestingThingsTest < Test::Unit::TestCase - include ActiveSupport::Testing::Default +if defined? ActiveSupport::Testing::Default + class NotTestingThingsTest < Test::Unit::TestCase + include ActiveSupport::Testing::Default + end end class AlsoDoingNothingTest < ActiveSupport::TestCase end # Setup and teardown callbacks. -class SetupAndTeardownTest < Test::Unit::TestCase +class SetupAndTeardownTest < ActiveSupport::TestCase setup :reset_callback_record, :foo teardown :foo, :sentinel, :foo diff --git a/railties/lib/test_help.rb b/railties/lib/test_help.rb index 3cc61d7932..442ce3fadc 100644 --- a/railties/lib/test_help.rb +++ b/railties/lib/test_help.rb @@ -4,18 +4,19 @@ require_dependency 'application' # so fixtures are loaded to the right database silence_warnings { RAILS_ENV = "test" } -require 'test/unit' -require 'active_support/test_case' -require 'active_record/fixtures' -require 'action_controller/test_case' require 'action_controller/integration' require 'action_mailer/test_case' if defined?(ActionMailer) -Test::Unit::TestCase.fixture_path = RAILS_ROOT + "/test/fixtures/" -ActionController::IntegrationTest.fixture_path = Test::Unit::TestCase.fixture_path +require 'active_record/fixtures' +class ActiveSupport::TestCase + include ActiveRecord::TestFixtures +end + +ActiveSupport::TestCase.fixture_path = "#{RAILS_ROOT}/test/fixtures/" +ActionController::IntegrationTest.fixture_path = ActiveSupport::TestCase.fixture_path def create_fixtures(*table_names) - Fixtures.create_fixtures(Test::Unit::TestCase.fixture_path, table_names) + Fixtures.create_fixtures(ActiveSupport::TestCase.fixture_path, table_names) end begin |