# File lib/action_controller/test_process.rb, line 395 def self.included(base) # Executes a request simulating GET HTTP method and set/volley the response def get(action, parameters = nil, session = nil, flash = nil) process(action, parameters, session, flash, "GET") end # Executes a request simulating POST HTTP method and set/volley the response def post(action, parameters = nil, session = nil, flash = nil) process(action, parameters, session, flash, "POST") end # Executes a request simulating PUT HTTP method and set/volley the response def put(action, parameters = nil, session = nil, flash = nil) process(action, parameters, session, flash, "PUT") end # Executes a request simulating DELETE HTTP method and set/volley the response def delete(action, parameters = nil, session = nil, flash = nil) process(action, parameters, session, flash, "DELETE") end # Executes a request simulating HEAD HTTP method and set/volley the response def head(action, parameters = nil, session = nil, flash = nil) process(action, parameters, session, flash, "HEAD") end end
# File lib/action_controller/test_process.rb, line 460 def assigns(key = nil) if key.nil? @response.template.assigns else @response.template.assigns[key.to_s] end end
# File lib/action_controller/test_process.rb, line 484 def build_request_uri(action, parameters) unless @request.env['REQUEST_URI'] options = @controller.__send__(:rewrite_options, parameters) options.update(:only_path => true, :action => action) url = ActionController::UrlRewriter.new(@request, parameters) @request.set_REQUEST_URI(url.rewrite(options)) end end
Executes a request simulating DELETE HTTP method and set/volley the response
# File lib/action_controller/test_process.rb, line 412 def delete(action, parameters = nil, session = nil, flash = nil) process(action, parameters, session, flash, "DELETE") end
# File lib/action_controller/test_process.rb, line 503 def find_all_tag(conditions) html_document.find_all(conditions) end
# File lib/action_controller/test_process.rb, line 499 def find_tag(conditions) html_document.find(conditions) end
Shortcut for ActionController::TestUploadedFile.new(ActionController::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 lib/action_controller/test_process.rb, line 523 def fixture_file_upload(path, mime_type = nil, binary = false) fixture_path = ActionController::TestCase.send(:fixture_path) if ActionController::TestCase.respond_to?(:fixture_path) ActionController::TestUploadedFile.new("#{fixture_path}#{path}", mime_type, binary) end
# File lib/action_controller/test_process.rb, line 472 def flash @response.flash end
Executes a request simulating GET HTTP method and set/volley the response
# File lib/action_controller/test_process.rb, line 397 def get(action, parameters = nil, session = nil, flash = nil) process(action, parameters, session, flash, "GET") end
Executes a request simulating HEAD HTTP method and set/volley the response
# File lib/action_controller/test_process.rb, line 417 def head(action, parameters = nil, session = nil, flash = nil) process(action, parameters, session, flash, "HEAD") end
# File lib/action_controller/test_process.rb, line 494 def html_document xml = @response.content_type =~ /xml$/ @html_document ||= HTML::Document.new(@response.body, false, xml) end
# File lib/action_controller/test_process.rb, line 507 def method_missing(selector, *args, &block) if @controller && ActionController::Routing::Routes.named_routes.helpers.include?(selector) @controller.send(selector, *args, &block) else super end end
Executes a request simulating POST HTTP method and set/volley the response
# File lib/action_controller/test_process.rb, line 402 def post(action, parameters = nil, session = nil, flash = nil) process(action, parameters, session, flash, "POST") end
# File lib/action_controller/test_process.rb, line 422 def process(action, parameters = nil, session = nil, flash = nil, http_method = 'GET') # Sanity check for required instance variables so we can give an # understandable error message. %(@controller @request @response).each do |iv_name| if !(instance_variable_names.include?(iv_name) || instance_variable_names.include?(iv_name.to_sym)) || instance_variable_get(iv_name).nil? raise "#{iv_name} is nil: make sure you set it in your test's setup method." end end @request.recycle! @response.recycle! @html_document = nil @request.env['REQUEST_METHOD'] = http_method @request.action = action.to_s parameters ||= {} @request.assign_parameters(@controller.class.controller_path, action.to_s, parameters) @request.session = ActionController::TestSession.new(session) unless session.nil? @request.session["flash"] = ActionController::Flash::FlashHash.new.update(flash) if flash build_request_uri(action, parameters) Base.class_eval { include ProcessWithTest } unless Base < ProcessWithTest @controller.process_with_test(@request, @response) end
Executes a request simulating PUT HTTP method and set/volley the response
# File lib/action_controller/test_process.rb, line 407 def put(action, parameters = nil, session = nil, flash = nil) process(action, parameters, session, flash, "PUT") end
# File lib/action_controller/test_process.rb, line 480 def redirect_to_url @response.redirect_url end
# File lib/action_controller/test_process.rb, line 468 def session @request.session 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 lib/action_controller/test_process.rb, line 545 def with_routing real_routes = ActionController::Routing::Routes ActionController::Routing.module_eval { remove_const :Routes } temporary_routes = ActionController::Routing::RouteSet.new ActionController::Routing.module_eval { const_set :Routes, temporary_routes } yield temporary_routes ensure if ActionController::Routing.const_defined? :Routes ActionController::Routing.module_eval { remove_const :Routes } end ActionController::Routing.const_set(:Routes, real_routes) if real_routes end
# File lib/action_controller/test_process.rb, line 450 def xml_http_request(request_method, action, parameters = nil, session = nil, flash = nil) @request.env['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest' @request.env['HTTP_ACCEPT'] = [Mime::JS, Mime::HTML, Mime::XML, 'text/xml', Mime::ALL].join(', ') __send__(request_method, action, parameters, session, flash).tap do @request.env.delete 'HTTP_X_REQUESTED_WITH' @request.env.delete 'HTTP_ACCEPT' end end
Generated with the Darkfish Rdoc Generator 2.