Module | ActionController::TestProcess |
In: |
vendor/rails/actionpack/lib/action_controller/test_process.rb
|
# File vendor/rails/actionpack/lib/action_controller/test_process.rb, line 360 360: def self.included(base) 361: # execute the request simulating a specific HTTP method and set/volley the response 362: %w( get post put delete head ).each do |method| 363: base.class_eval "def \#{method}(action, parameters = nil, session = nil, flash = nil)\n@request.env['REQUEST_METHOD'] = \"\#{method.upcase}\" if defined?(@request)\nprocess(action, parameters, session, flash)\nend\n", __FILE__, __LINE__ 364: end 365: end
# File vendor/rails/actionpack/lib/action_controller/test_process.rb, line 417 417: def assigns(key = nil) 418: if key.nil? 419: @response.template.assigns 420: else 421: @response.template.assigns[key.to_s] 422: end 423: end
# File vendor/rails/actionpack/lib/action_controller/test_process.rb, line 441 441: def build_request_uri(action, parameters) 442: unless @request.env['REQUEST_URI'] 443: options = @controller.send!(:rewrite_options, parameters) 444: options.update(:only_path => true, :action => action) 445: 446: url = ActionController::UrlRewriter.new(@request, parameters) 447: @request.set_REQUEST_URI(url.rewrite(options)) 448: end 449: end
# File vendor/rails/actionpack/lib/action_controller/test_process.rb, line 433 433: def cookies 434: @response.cookies 435: end
# File vendor/rails/actionpack/lib/action_controller/test_process.rb, line 460 460: def find_all_tag(conditions) 461: html_document.find_all(conditions) 462: end
# File vendor/rails/actionpack/lib/action_controller/test_process.rb, line 456 456: def find_tag(conditions) 457: html_document.find(conditions) 458: end
Shortcut for ActionController::TestUploadedFile.new(Test::Unit::TestCase.fixture_path + path, type):
post :change_avatar, :avatar => fixture_file_upload('/files/spongebob.png', 'image/png')
To upload binary files on Windows, pass :binary as the last parameter. This will not affect other platforms:
post :change_avatar, :avatar => fixture_file_upload('/files/spongebob.png', 'image/png', :binary)
# File vendor/rails/actionpack/lib/action_controller/test_process.rb, line 477 477: def fixture_file_upload(path, mime_type = nil, binary = false) 478: ActionController::TestUploadedFile.new( 479: Test::Unit::TestCase.respond_to?(:fixture_path) ? Test::Unit::TestCase.fixture_path + path : path, 480: mime_type, 481: binary 482: ) 483: end
# File vendor/rails/actionpack/lib/action_controller/test_process.rb, line 429 429: def flash 430: @response.flash 431: end
# File vendor/rails/actionpack/lib/action_controller/test_process.rb, line 408 408: def follow_redirect 409: redirected_controller = @response.redirected_to[:controller] 410: if redirected_controller && redirected_controller != @controller.controller_name 411: raise "Can't follow redirects outside of current controller (from #{@controller.controller_name} to #{redirected_controller})" 412: end 413: 414: get(@response.redirected_to.delete(:action), @response.redirected_to.stringify_keys) 415: end
# File vendor/rails/actionpack/lib/action_controller/test_process.rb, line 451 451: def html_document 452: xml = @response.content_type =~ /xml$/ 453: @html_document ||= HTML::Document.new(@response.body, false, xml) 454: end
# File vendor/rails/actionpack/lib/action_controller/test_process.rb, line 464 464: def method_missing(selector, *args) 465: return @controller.send!(selector, *args) if ActionController::Routing::Routes.named_routes.helpers.include?(selector) 466: return super 467: end
execute the request and set/volley the response
# File vendor/rails/actionpack/lib/action_controller/test_process.rb, line 374 374: def process(action, parameters = nil, session = nil, flash = nil) 375: # Sanity check for required instance variables so we can give an 376: # understandable error message. 377: %w(@controller @request @response).each do |iv_name| 378: if !(instance_variable_names.include?(iv_name) || instance_variable_names.include?(iv_name.to_sym)) || instance_variable_get(iv_name).nil? 379: raise "#{iv_name} is nil: make sure you set it in your test's setup method." 380: end 381: end 382: 383: @request.recycle! 384: 385: @html_document = nil 386: @request.env['REQUEST_METHOD'] ||= "GET" 387: @request.action = action.to_s 388: 389: parameters ||= {} 390: @request.assign_parameters(@controller.class.controller_path, action.to_s, parameters) 391: 392: @request.session = ActionController::TestSession.new(session) unless session.nil? 393: @request.session["flash"] = ActionController::Flash::FlashHash.new.update(flash) if flash 394: build_request_uri(action, parameters) 395: @controller.process(@request, @response) 396: end
# File vendor/rails/actionpack/lib/action_controller/test_process.rb, line 437 437: def redirect_to_url 438: @response.redirect_url 439: end
# File vendor/rails/actionpack/lib/action_controller/test_process.rb, line 425 425: def session 426: @response.session 427: end
A helper to make it easier to test different route configurations. This method temporarily replaces ActionController::Routing::Routes with a new RouteSet instance.
The new instance is yielded to the passed block. Typically the block will create some routes using map.draw { map.connect … }:
with_routing do |set| set.draw do |map| map.connect ':controller/:action/:id' assert_equal( ['/content/10/show', {}], map.generate(:controller => 'content', :id => 10, :action => 'show') end end end
# File vendor/rails/actionpack/lib/action_controller/test_process.rb, line 502 502: def with_routing 503: real_routes = ActionController::Routing::Routes 504: ActionController::Routing.module_eval { remove_const :Routes } 505: 506: temporary_routes = ActionController::Routing::RouteSet.new 507: ActionController::Routing.module_eval { const_set :Routes, temporary_routes } 508: 509: yield temporary_routes 510: ensure 511: if ActionController::Routing.const_defined? :Routes 512: ActionController::Routing.module_eval { remove_const :Routes } 513: end 514: ActionController::Routing.const_set(:Routes, real_routes) if real_routes 515: end
Alias for xml_http_request
# File vendor/rails/actionpack/lib/action_controller/test_process.rb, line 398 398: def xml_http_request(request_method, action, parameters = nil, session = nil, flash = nil) 399: @request.env['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest' 400: @request.env['HTTP_ACCEPT'] = 'text/javascript, text/html, application/xml, text/xml, */*' 401: returning send!(request_method, action, parameters, session, flash) do 402: @request.env.delete 'HTTP_X_REQUESTED_WITH' 403: @request.env.delete 'HTTP_ACCEPT' 404: end 405: end