Skip to content

Commit

Permalink
Add load hooks to all tests classes
Browse files Browse the repository at this point in the history
Usually users extends tests classes doing something like:

    ActionView::TestCase.include MyCustomTestHelpers

This is bad because it will load the ActionView::TestCase right aways
and this will load ActionController::Base making its on_load hooks to
execute early than it should.

One way to fix this is using the on_load hooks of the components like:

    ActiveSupport.on_load(:action_view) do
      ActionView::TestCase.include MyCustomTestHelpers
    end

The problem with this approach is that the test extension will be only
load when ActionView::Base is loaded and this may happen too late in the
test.

To fix this we are adding hooks to people extend the test classes that
will be loaded exactly when the test classes are needed.
  • Loading branch information
rafaelfranca committed Aug 25, 2016
1 parent 804f5b3 commit 0510208
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 0 deletions.
1 change: 1 addition & 0 deletions actionmailer/lib/action_mailer/test_case.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ module Behavior
setup :initialize_test_deliveries
setup :set_expected_mail
teardown :restore_test_deliveries
ActiveSupport.run_load_hooks(:action_mailer_test_case, self)
end

module ClassMethods
Expand Down
1 change: 1 addition & 0 deletions actionpack/lib/action_controller/test_case.rb
Original file line number Diff line number Diff line change
Expand Up @@ -627,6 +627,7 @@ def build_response(klass)
include ActionDispatch::Assertions
class_attribute :_controller_class
setup :setup_controller_request_and_response
ActiveSupport.run_load_hooks(:action_view_test_case, self)
end

private
Expand Down
1 change: 1 addition & 0 deletions actionview/lib/action_view/test_case.rb
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ def view_rendered?(view, expected_locals)

included do
setup :setup_with_controller
ActiveSupport.run_load_hooks(:action_view_test_case, self)
end

private
Expand Down
2 changes: 2 additions & 0 deletions activejob/lib/active_job/test_case.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,7 @@
module ActiveJob
class TestCase < ActiveSupport::TestCase
include ActiveJob::TestHelper

ActiveSupport.run_load_hooks(:active_job_test_case, self)
end
end

0 comments on commit 0510208

Please sign in to comment.