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
462 463 464 465 |
# File 'lib/action_controller/test_process.rb', line 462 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
359 360 361 362 363 364 365 366 367 368 369 |
# File 'lib/action_controller/test_process.rb', line 359 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
415 416 417 418 419 420 421 |
# File 'lib/action_controller/test_process.rb', line 415 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
439 440 441 442 443 444 445 446 447 |
# File 'lib/action_controller/test_process.rb', line 439 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
431 432 433 |
# File 'lib/action_controller/test_process.rb', line 431 def @response. end |
#find_all_tag(conditions) ⇒ Object
458 459 460 |
# File 'lib/action_controller/test_process.rb', line 458 def find_all_tag(conditions) html_document.find_all(conditions) end |
#find_tag(conditions) ⇒ Object
454 455 456 |
# File 'lib/action_controller/test_process.rb', line 454 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). Example:
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)
472 473 474 475 476 477 478 |
# File 'lib/action_controller/test_process.rb', line 472 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
427 428 429 |
# File 'lib/action_controller/test_process.rb', line 427 def flash @response.flash end |
#follow_redirect ⇒ Object
406 407 408 409 410 411 412 413 |
# File 'lib/action_controller/test_process.rb', line 406 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
449 450 451 452 |
# File 'lib/action_controller/test_process.rb', line 449 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
372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 |
# File 'lib/action_controller/test_process.rb', line 372 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_variables.include?(iv_name) || instance_variables.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
435 436 437 |
# File 'lib/action_controller/test_process.rb', line 435 def redirect_to_url @response.redirect_url end |
#session ⇒ Object
423 424 425 |
# File 'lib/action_controller/test_process.rb', line 423 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
497 498 499 500 501 502 503 504 505 506 507 508 509 510 |
# File 'lib/action_controller/test_process.rb', line 497 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
396 397 398 399 400 401 402 403 |
# File 'lib/action_controller/test_process.rb', line 396 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 |