Merge pull request #40125 from jhawthorn/actionview_test_case_isolation

Use a unique controller class per ActionView::TestCase
This commit is contained in:
John Hawthorn 2020-09-08 14:08:13 -07:00 committed by GitHub
commit 10df6931de
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 25 additions and 25 deletions

View File

@ -126,7 +126,7 @@ module ActionController
# ==== Returns
# * <tt>string</tt>
def self.controller_name
@controller_name ||= name.demodulize.delete_suffix("Controller").underscore
@controller_name ||= (name.demodulize.delete_suffix("Controller").underscore unless anonymous?)
end
def self.make_response!(request)

View File

@ -16,11 +16,12 @@ module ActionView
attr_accessor :request, :response, :params
class << self
attr_writer :controller_path
# Overrides AbstractController::Base#controller_path
attr_accessor :controller_path
end
def controller_path=(path)
self.class.controller_path = (path)
self.class.controller_path = path
end
def initialize
@ -101,7 +102,8 @@ module ActionView
end
def setup_with_controller
@controller = ActionView::TestCase::TestController.new
controller_class = Class.new(ActionView::TestCase::TestController)
@controller = controller_class.new
@request = @controller.request
@view_flow = ActionView::OutputFlow.new
# empty string ensures buffer has UTF-8 encoding as
@ -109,8 +111,8 @@ module ActionView
@output_buffer = ActiveSupport::SafeBuffer.new ""
@rendered = +""
make_test_case_available_to_view!
say_no_to_protect_against_forgery!
test_case_instance = self
controller_class.define_method(:_test_case) { test_case_instance }
end
def config
@ -160,6 +162,16 @@ module ActionView
included do
setup :setup_with_controller
ActiveSupport.run_load_hooks(:action_view_test_case, self)
helper do
def protect_against_forgery?
false
end
def _test_case
controller._test_case
end
end
end
private
@ -168,25 +180,6 @@ module ActionView
Nokogiri::HTML::Document.parse(@rendered.blank? ? @output_buffer : @rendered).root
end
def say_no_to_protect_against_forgery!
_helpers.module_eval do
silence_redefinition_of_method :protect_against_forgery?
def protect_against_forgery?
false
end
end
end
def make_test_case_available_to_view!
test_case_instance = self
_helpers.module_eval do
unless private_method_defined?(:_test_case)
define_method(:_test_case) { test_case_instance }
private :_test_case
end
end
end
module Locals
attr_accessor :rendered_views

View File

@ -156,6 +156,13 @@ module ActionView
assert_equal "controller_helper_method", some_method
end
class AnotherTestClass < ActionView::TestCase
test "doesn't use controller helpers from other tests" do
assert_not_respond_to view, :render_from_helper
assert_not_includes @controller._helpers.instance_methods, :render_from_helper
end
end
end
class ViewAssignsTest < ActionView::TestCase