Module: ActionController::TestProcess
- Included in:
- Integration::Session, Test::Unit::TestCase
- Defined in:
- lib/action_controller/test_process.rb
Class Method Summary collapse
Instance Method Summary collapse
- #assigns(key = nil) ⇒ Object
- #build_request_uri(action, parameters) ⇒ Object
- #cookies ⇒ Object
- #find_all_tag(conditions) ⇒ Object
- #find_tag(conditions) ⇒ Object
-
#fixture_file_upload(path, mime_type = nil, binary = false) ⇒ Object
Shortcut for
ActionController::TestUploadedFile.new(Test::Unit::TestCase.fixture_path + path, type)
:. - #flash ⇒ Object
- #follow_redirect ⇒ Object
- #html_document ⇒ Object
- #method_missing(selector, *args) ⇒ Object
-
#process(action, parameters = nil, session = nil, flash = nil) ⇒ Object
execute the request and set/volley the response.
- #redirect_to_url ⇒ Object
- #session ⇒ Object
-
#with_routing ⇒ Object
A helper to make it easier to test different route configurations.
- #xml_http_request(request_method, action, parameters = nil, session = nil, flash = nil) ⇒ Object (also: #xhr)
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(selector, *args) ⇒ Object
465 466 467 468 |
# File 'lib/action_controller/test_process.rb', line 465 def method_missing(selector, *args) return @controller.send!(selector, *args) if ActionController::Routing::Routes.named_routes.helpers.include?(selector) return super end |
Class Method Details
.included(base) ⇒ Object
360 361 362 363 364 365 366 367 368 369 370 |
# File 'lib/action_controller/test_process.rb', line 360 def self.included(base) # execute the request simulating a specific HTTP method and set/volley the response %w( get post put delete head ).each do |method| base.class_eval <<-EOV, __FILE__, __LINE__ def #{method}(action, parameters = nil, session = nil, flash = nil) @request.env['REQUEST_METHOD'] = "#{method.upcase}" if defined?(@request) process(action, parameters, session, flash) end EOV end end |
Instance Method Details
#assigns(key = nil) ⇒ Object
418 419 420 421 422 423 424 |
# File 'lib/action_controller/test_process.rb', line 418 def assigns(key = nil) if key.nil? @response.template.assigns else @response.template.assigns[key.to_s] end end |
#build_request_uri(action, parameters) ⇒ Object
442 443 444 445 446 447 448 449 450 |
# File 'lib/action_controller/test_process.rb', line 442 def build_request_uri(action, parameters) unless @request.env['REQUEST_URI'] = @controller.send!(:rewrite_options, parameters) .update(:only_path => true, :action => action) url = ActionController::UrlRewriter.new(@request, parameters) @request.set_REQUEST_URI(url.rewrite()) end end |
#cookies ⇒ Object
434 435 436 |
# File 'lib/action_controller/test_process.rb', line 434 def @response. end |
#find_all_tag(conditions) ⇒ Object
461 462 463 |
# File 'lib/action_controller/test_process.rb', line 461 def find_all_tag(conditions) html_document.find_all(conditions) end |
#find_tag(conditions) ⇒ Object
457 458 459 |
# File 'lib/action_controller/test_process.rb', line 457 def find_tag(conditions) html_document.find(conditions) end |
#fixture_file_upload(path, mime_type = nil, binary = false) ⇒ Object
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)
478 479 480 481 482 483 484 |
# File 'lib/action_controller/test_process.rb', line 478 def fixture_file_upload(path, mime_type = nil, binary = false) ActionController::TestUploadedFile.new( Test::Unit::TestCase.respond_to?(:fixture_path) ? Test::Unit::TestCase.fixture_path + path : path, mime_type, binary ) end |
#flash ⇒ Object
430 431 432 |
# File 'lib/action_controller/test_process.rb', line 430 def flash @response.flash end |
#follow_redirect ⇒ Object
407 408 409 410 411 412 413 414 |
# File 'lib/action_controller/test_process.rb', line 407 def follow_redirect redirected_controller = @response.redirected_to[:controller] if redirected_controller && redirected_controller != @controller.controller_name raise "Can't follow redirects outside of current controller (from #{@controller.controller_name} to #{redirected_controller})" end get(@response.redirected_to.delete(:action), @response.redirected_to.stringify_keys) end |
#html_document ⇒ Object
452 453 454 455 |
# File 'lib/action_controller/test_process.rb', line 452 def html_document xml = @response.content_type =~ /xml$/ @html_document ||= HTML::Document.new(@response.body, false, xml) end |
#process(action, parameters = nil, session = nil, flash = nil) ⇒ Object
execute the request and set/volley the response
373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 |
# File 'lib/action_controller/test_process.rb', line 373 def process(action, parameters = nil, session = nil, flash = nil) # Sanity check for required instance variables so we can give an # understandable error message. %w(@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! @html_document = nil @request.env['REQUEST_METHOD'] ||= "GET" @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) @controller.process(@request, @response) end |
#redirect_to_url ⇒ Object
438 439 440 |
# File 'lib/action_controller/test_process.rb', line 438 def redirect_to_url @response.redirect_url end |
#session ⇒ Object
426 427 428 |
# File 'lib/action_controller/test_process.rb', line 426 def session @response.session end |
#with_routing ⇒ Object
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
503 504 505 506 507 508 509 510 511 512 513 514 515 516 |
# File 'lib/action_controller/test_process.rb', line 503 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 |
#xml_http_request(request_method, action, parameters = nil, session = nil, flash = nil) ⇒ Object Also known as: xhr
397 398 399 400 401 402 403 404 |
# File 'lib/action_controller/test_process.rb', line 397 def xml_http_request(request_method, action, parameters = nil, session = nil, flash = nil) @request.env['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest' @request.env['HTTP_ACCEPT'] = 'text/javascript, text/html, application/xml, text/xml, */*' returning send!(request_method, action, parameters, session, flash) do @request.env.delete 'HTTP_X_REQUESTED_WITH' @request.env.delete 'HTTP_ACCEPT' end end |