提交 35fa0073 编写于 作者: J Jeremy Kemper

Include process methods in ActionController::TestCase only. No need to...

Include process methods in ActionController::TestCase only. No need to alias_method_chain :process either.
上级 0f9e65b7
require 'active_support/test_case' require 'active_support/test_case'
require 'action_controller/test_process'
module ActionController module ActionController
# Superclass for ActionController functional tests. Functional tests allow you to # Superclass for ActionController functional tests. Functional tests allow you to
...@@ -102,6 +103,8 @@ module ActionController ...@@ -102,6 +103,8 @@ module ActionController
# #
# assert_redirected_to page_url(:title => 'foo') # assert_redirected_to page_url(:title => 'foo')
class TestCase < ActiveSupport::TestCase class TestCase < ActiveSupport::TestCase
include TestProcess
module Assertions module Assertions
%w(response selector tag dom routing model).each do |kind| %w(response selector tag dom routing model).each do |kind|
include ActionController::Assertions.const_get("#{kind.camelize}Assertions") include ActionController::Assertions.const_get("#{kind.camelize}Assertions")
......
require 'action_controller/test_case'
module ActionController #:nodoc: module ActionController #:nodoc:
class Base
attr_reader :assigns
# Process a test request called with a TestRequest object.
def self.process_test(request)
new.process_test(request)
end
def process_test(request) #:nodoc:
process(request, TestResponse.new)
end
def process_with_test(*args)
returning process_without_test(*args) do
@assigns = {}
(instance_variable_names - @@protected_instance_variables).each do |var|
value = instance_variable_get(var)
@assigns[var[1..-1]] = value
response.template.assigns[var[1..-1]] = value if response
end
end
end
alias_method_chain :process, :test
end
class TestRequest < Request #:nodoc: class TestRequest < Request #:nodoc:
attr_accessor :cookies, :session_options attr_accessor :cookies, :session_options
attr_accessor :query_parameters, :path, :session attr_accessor :query_parameters, :path, :session
...@@ -433,7 +405,9 @@ def process(action, parameters = nil, session = nil, flash = nil, http_method = ...@@ -433,7 +405,9 @@ def process(action, parameters = nil, session = nil, flash = nil, http_method =
@request.session = ActionController::TestSession.new(session) unless session.nil? @request.session = ActionController::TestSession.new(session) unless session.nil?
@request.session["flash"] = ActionController::Flash::FlashHash.new.update(flash) if flash @request.session["flash"] = ActionController::Flash::FlashHash.new.update(flash) if flash
build_request_uri(action, parameters) build_request_uri(action, parameters)
@controller.process(@request, @response)
Base.class_eval { include ProcessWithTest } unless Base < ProcessWithTest
@controller.process_with_test(@request, @response)
end end
def xml_http_request(request_method, action, parameters = nil, session = nil, flash = nil) def xml_http_request(request_method, action, parameters = nil, session = nil, flash = nil)
...@@ -545,12 +519,24 @@ def with_routing ...@@ -545,12 +519,24 @@ def with_routing
ActionController::Routing.const_set(:Routes, real_routes) if real_routes ActionController::Routing.const_set(:Routes, real_routes) if real_routes
end end
end end
end
module Test module ProcessWithTest #:nodoc:
module Unit def self.included(base)
class TestCase #:nodoc: base.class_eval { attr_reader :assigns }
include ActionController::TestProcess end
def process_with_test(*args)
process(*args).tap { set_test_assigns }
end end
private
def set_test_assigns
@assigns = {}
(instance_variable_names - self.class.protected_instance_variables).each do |var|
name, value = var[1..-1], instance_variable_get(var)
@assigns[name] = value
response.template.assigns[name] = value if response
end
end
end end
end end
...@@ -23,6 +23,7 @@ def render(view, local_assigns = {}) ...@@ -23,6 +23,7 @@ def render(view, local_assigns = {})
class TestCase < ActiveSupport::TestCase class TestCase < ActiveSupport::TestCase
include ActionController::TestCase::Assertions include ActionController::TestCase::Assertions
include ActionController::TestProcess
class_inheritable_accessor :helper_class class_inheritable_accessor :helper_class
@@helper_class = nil @@helper_class = nil
......
...@@ -19,17 +19,14 @@ def self.controller_name; "addresses"; end ...@@ -19,17 +19,14 @@ def self.controller_name; "addresses"; end
def self.controller_path; "addresses"; end def self.controller_path; "addresses"; end
end end
class AddressesTest < Test::Unit::TestCase class AddressesTest < ActionController::TestCase
def setup tests AddressesTestController
@controller = AddressesTestController.new
def setup
# enable a logger so that (e.g.) the benchmarking stuff runs, so we can get # 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". # a more accurate simulation of what happens in "real life".
@controller.logger = Logger.new(nil) @controller.logger = Logger.new(nil)
@request = ActionController::TestRequest.new
@response = ActionController::TestResponse.new
@request.host = "www.nextangle.com" @request.host = "www.nextangle.com"
end end
......
...@@ -129,6 +129,8 @@ def use_controller(controller_class) ...@@ -129,6 +129,8 @@ def use_controller(controller_class)
@response = ActionController::TestResponse.new @response = ActionController::TestResponse.new
@request.host = "www.nextangle.com" @request.host = "www.nextangle.com"
rescue_action_in_public!
end end
def test_get_on_priv_should_show_selector def test_get_on_priv_should_show_selector
...@@ -164,14 +166,12 @@ def test_namespaced_action_should_log_module_name ...@@ -164,14 +166,12 @@ def test_namespaced_action_should_log_module_name
end end
end end
class DefaultUrlOptionsTest < Test::Unit::TestCase class DefaultUrlOptionsTest < ActionController::TestCase
def setup tests DefaultUrlOptionsController
@controller = DefaultUrlOptionsController.new
@request = ActionController::TestRequest.new
@response = ActionController::TestResponse.new
def setup
@request.host = 'www.example.com' @request.host = 'www.example.com'
rescue_action_in_public!
end end
def test_default_url_options_are_used_if_set def test_default_url_options_are_used_if_set
...@@ -189,14 +189,12 @@ def test_default_url_options_are_used_if_set ...@@ -189,14 +189,12 @@ def test_default_url_options_are_used_if_set
end end
end end
class EmptyUrlOptionsTest < Test::Unit::TestCase class EmptyUrlOptionsTest < ActionController::TestCase
def setup tests NonEmptyController
@controller = NonEmptyController.new
@request = ActionController::TestRequest.new
@response = ActionController::TestResponse.new
def setup
@request.host = 'www.example.com' @request.host = 'www.example.com'
rescue_action_in_public!
end end
def test_ensure_url_for_works_as_expected_when_called_with_no_options_if_default_url_options_is_not_set def test_ensure_url_for_works_as_expected_when_called_with_no_options_if_default_url_options_is_not_set
......
...@@ -11,17 +11,17 @@ def rescue_action(e) ...@@ -11,17 +11,17 @@ def rescue_action(e)
end end
end end
class BenchmarkTest < Test::Unit::TestCase class BenchmarkTest < ActionController::TestCase
tests BenchmarkedController
class MockLogger class MockLogger
def method_missing(*args) def method_missing(*args)
end end
end end
def setup def setup
@controller = BenchmarkedController.new
# benchmark doesn't do anything unless a logger is set # benchmark doesn't do anything unless a logger is set
@controller.logger = MockLogger.new @controller.logger = MockLogger.new
@request, @response = ActionController::TestRequest.new, ActionController::TestResponse.new
@request.host = "test.actioncontroller.i" @request.host = "test.actioncontroller.i"
end end
......
...@@ -23,17 +23,14 @@ def non_erb_block_content_for ...@@ -23,17 +23,14 @@ def non_erb_block_content_for
def rescue_action(e) raise end def rescue_action(e) raise end
end end
class CaptureTest < Test::Unit::TestCase class CaptureTest < ActionController::TestCase
def setup tests CaptureController
@controller = CaptureController.new
def setup
# enable a logger so that (e.g.) the benchmarking stuff runs, so we can get # 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". # a more accurate simulation of what happens in "real life".
@controller.logger = Logger.new(nil) @controller.logger = Logger.new(nil)
@request = ActionController::TestRequest.new
@response = ActionController::TestResponse.new
@request.host = "www.nextangle.com" @request.host = "www.nextangle.com"
end end
......
...@@ -50,16 +50,13 @@ def render_default_content_types_for_respond_to ...@@ -50,16 +50,13 @@ def render_default_content_types_for_respond_to
def rescue_action(e) raise end def rescue_action(e) raise end
end end
class ContentTypeTest < Test::Unit::TestCase class ContentTypeTest < ActionController::TestCase
def setup tests ContentTypeController
@controller = ContentTypeController.new
def setup
# enable a logger so that (e.g.) the benchmarking stuff runs, so we can get # 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". # a more accurate simulation of what happens in "real life".
@controller.logger = Logger.new(nil) @controller.logger = Logger.new(nil)
@request = ActionController::TestRequest.new
@response = ActionController::TestResponse.new
end end
def test_render_defaults def test_render_defaults
......
require 'abstract_unit' require 'abstract_unit'
class CookieTest < Test::Unit::TestCase class CookieTest < ActionController::TestCase
class TestController < ActionController::Base class TestController < ActionController::Base
def authenticate def authenticate
cookies["user_name"] = "david" cookies["user_name"] = "david"
...@@ -41,11 +41,9 @@ def rescue_action(e) ...@@ -41,11 +41,9 @@ def rescue_action(e)
end end
end end
def setup tests TestController
@request = ActionController::TestRequest.new
@response = ActionController::TestResponse.new
@controller = TestController.new def setup
@request.host = "www.nextangle.com" @request.host = "www.nextangle.com"
end end
......
...@@ -634,9 +634,11 @@ def test_a_rescuing_around_filter ...@@ -634,9 +634,11 @@ def test_a_rescuing_around_filter
private private
def test_process(controller, action = "show") def test_process(controller, action = "show")
ActionController::Base.class_eval { include ActionController::ProcessWithTest } unless ActionController::Base < ActionController::ProcessWithTest
request = ActionController::TestRequest.new request = ActionController::TestRequest.new
request.action = action request.action = action
controller.process(request, ActionController::TestResponse.new) controller = controller.new if controller.is_a?(Class)
controller.process_with_test(request, ActionController::TestResponse.new)
end end
end end
...@@ -874,8 +876,10 @@ def test_last_filter_in_multiple_before_filter_chain_halts ...@@ -874,8 +876,10 @@ def test_last_filter_in_multiple_before_filter_chain_halts
protected protected
def test_process(controller, action = "show") def test_process(controller, action = "show")
ActionController::Base.class_eval { include ActionController::ProcessWithTest } unless ActionController::Base < ActionController::ProcessWithTest
request = ActionController::TestRequest.new request = ActionController::TestRequest.new
request.action = action request.action = action
controller.process(request, ActionController::TestResponse.new) controller = controller.new if controller.is_a?(Class)
controller.process_with_test(request, ActionController::TestResponse.new)
end end
end end
require 'abstract_unit' require 'abstract_unit'
class FlashTest < Test::Unit::TestCase class FlashTest < ActionController::TestCase
class TestController < ActionController::Base class TestController < ActionController::Base
def set_flash def set_flash
flash["that"] = "hello" flash["that"] = "hello"
...@@ -73,11 +73,7 @@ def halt_and_redir ...@@ -73,11 +73,7 @@ def halt_and_redir
end end
end end
def setup tests TestController
@request = ActionController::TestRequest.new
@response = ActionController::TestResponse.new
@controller = TestController.new
end
def test_flash def test_flash
get :set_flash get :set_flash
......
...@@ -19,7 +19,8 @@ def data() send_data(file_data, options) end ...@@ -19,7 +19,8 @@ def data() send_data(file_data, options) end
def rescue_action(e) raise end def rescue_action(e) raise end
end end
class SendFileTest < Test::Unit::TestCase class SendFileTest < ActionController::TestCase
tests SendFileController
include TestFileUtils include TestFileUtils
Mime::Type.register "image/png", :png unless defined? Mime::PNG Mime::Type.register "image/png", :png unless defined? Mime::PNG
......
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
silence_warnings { RAILS_ENV = "test" } silence_warnings { RAILS_ENV = "test" }
require 'test/unit' require 'test/unit'
require 'action_controller/test_process' require 'action_controller/test_case'
require 'action_view/test_case' require 'action_view/test_case'
require 'action_controller/integration' require 'action_controller/integration'
require 'action_mailer/test_case' if defined?(ActionMailer) require 'action_mailer/test_case' if defined?(ActionMailer)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册