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) ⇒ 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
450 451 452 453 |
# File 'lib/action_controller/test_process.rb', line 450 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
348 349 350 351 352 353 354 355 356 357 358 |
# File 'lib/action_controller/test_process.rb', line 348 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
404 405 406 407 408 409 410 |
# File 'lib/action_controller/test_process.rb', line 404 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
428 429 430 431 432 433 434 435 436 |
# File 'lib/action_controller/test_process.rb', line 428 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
420 421 422 |
# File 'lib/action_controller/test_process.rb', line 420 def @response. end |
#find_all_tag(conditions) ⇒ Object
446 447 448 |
# File 'lib/action_controller/test_process.rb', line 446 def find_all_tag(conditions) html_document.find_all(conditions) end |
#find_tag(conditions) ⇒ Object
442 443 444 |
# File 'lib/action_controller/test_process.rb', line 442 def find_tag(conditions) html_document.find(conditions) end |
#fixture_file_upload(path, mime_type = nil) ⇒ 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')
457 458 459 460 461 462 |
# File 'lib/action_controller/test_process.rb', line 457 def fixture_file_upload(path, mime_type = nil) ActionController::TestUploadedFile.new( Test::Unit::TestCase.respond_to?(:fixture_path) ? Test::Unit::TestCase.fixture_path + path : path, mime_type ) end |
#flash ⇒ Object
416 417 418 |
# File 'lib/action_controller/test_process.rb', line 416 def flash @response.flash end |
#follow_redirect ⇒ Object
395 396 397 398 399 400 401 402 |
# File 'lib/action_controller/test_process.rb', line 395 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
438 439 440 |
# File 'lib/action_controller/test_process.rb', line 438 def html_document @html_document ||= HTML::Document.new(@response.body) end |
#process(action, parameters = nil, session = nil, flash = nil) ⇒ Object
execute the request and set/volley the response
361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 |
# File 'lib/action_controller/test_process.rb', line 361 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_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
424 425 426 |
# File 'lib/action_controller/test_process.rb', line 424 def redirect_to_url @response.redirect_url end |
#session ⇒ Object
412 413 414 |
# File 'lib/action_controller/test_process.rb', line 412 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
481 482 483 484 485 486 487 488 489 490 491 492 493 494 |
# File 'lib/action_controller/test_process.rb', line 481 def with_routing real_routes = ActionController::Routing::Routes ActionController::Routing.send :remove_const, :Routes temporary_routes = ActionController::Routing::RouteSet.new ActionController::Routing.send :const_set, :Routes, temporary_routes yield temporary_routes ensure if ActionController::Routing.const_defined? :Routes ActionController::Routing.send(: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
385 386 387 388 389 390 391 392 |
# File 'lib/action_controller/test_process.rb', line 385 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 self.send(request_method, action, parameters, session, flash) do @request.env.delete 'HTTP_X_REQUESTED_WITH' @request.env.delete 'HTTP_ACCEPT' end end |