Module ActionController::TestProcess
In: vendor/rails/actionpack/lib/action_controller/test_process.rb

Methods

Public Class methods

[Source]

     # File vendor/rails/actionpack/lib/action_controller/test_process.rb, line 370
370:     def self.included(base)
371:       # execute the request simulating a specific HTTP method and set/volley the response
372:       # TODO: this should be un-DRY'ed for the sake of API documentation.
373:       %w( get post put delete head ).each do |method|
374:         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__
375:       end
376:     end

Public Instance methods

[Source]

     # File vendor/rails/actionpack/lib/action_controller/test_process.rb, line 421
421:     def assigns(key = nil)
422:       if key.nil?
423:         @response.template.assigns
424:       else
425:         @response.template.assigns[key.to_s]
426:       end
427:     end

[Source]

     # File vendor/rails/actionpack/lib/action_controller/test_process.rb, line 445
445:     def build_request_uri(action, parameters)
446:       unless @request.env['REQUEST_URI']
447:         options = @controller.__send__(:rewrite_options, parameters)
448:         options.update(:only_path => true, :action => action)
449: 
450:         url = ActionController::UrlRewriter.new(@request, parameters)
451:         @request.set_REQUEST_URI(url.rewrite(options))
452:       end
453:     end

[Source]

     # File vendor/rails/actionpack/lib/action_controller/test_process.rb, line 437
437:     def cookies
438:       @response.cookies
439:     end

[Source]

     # File vendor/rails/actionpack/lib/action_controller/test_process.rb, line 464
464:     def find_all_tag(conditions)
465:       html_document.find_all(conditions)
466:     end

[Source]

     # File vendor/rails/actionpack/lib/action_controller/test_process.rb, line 460
460:     def find_tag(conditions)
461:       html_document.find(conditions)
462:     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)

[Source]

     # File vendor/rails/actionpack/lib/action_controller/test_process.rb, line 484
484:     def fixture_file_upload(path, mime_type = nil, binary = false)
485:       ActionController::TestUploadedFile.new(
486:         Test::Unit::TestCase.respond_to?(:fixture_path) ? Test::Unit::TestCase.fixture_path + path : path,
487:         mime_type,
488:         binary
489:       )
490:     end

[Source]

     # File vendor/rails/actionpack/lib/action_controller/test_process.rb, line 433
433:     def flash
434:       @response.flash
435:     end

[Source]

     # File vendor/rails/actionpack/lib/action_controller/test_process.rb, line 455
455:     def html_document
456:       xml = @response.content_type =~ /xml$/
457:       @html_document ||= HTML::Document.new(@response.body, false, xml)
458:     end

[Source]

     # File vendor/rails/actionpack/lib/action_controller/test_process.rb, line 468
468:     def method_missing(selector, *args)
469:       if ActionController::Routing::Routes.named_routes.helpers.include?(selector)
470:         @controller.send(selector, *args)
471:       else
472:         super
473:       end
474:     end

execute the request and set/volley the response

[Source]

     # File vendor/rails/actionpack/lib/action_controller/test_process.rb, line 385
385:     def process(action, parameters = nil, session = nil, flash = nil)
386:       # Sanity check for required instance variables so we can give an
387:       # understandable error message.
388:       %w(@controller @request @response).each do |iv_name|
389:         if !(instance_variable_names.include?(iv_name) || instance_variable_names.include?(iv_name.to_sym)) || instance_variable_get(iv_name).nil?
390:           raise "#{iv_name} is nil: make sure you set it in your test's setup method."
391:         end
392:       end
393: 
394:       @request.recycle!
395:       @response.recycle!
396: 
397:       @html_document = nil
398:       @request.env['REQUEST_METHOD'] ||= "GET"
399: 
400:       @request.action = action.to_s
401: 
402:       parameters ||= {}
403:       @request.assign_parameters(@controller.class.controller_path, action.to_s, parameters)
404: 
405:       @request.session = ActionController::TestSession.new(session) unless session.nil?
406:       @request.session["flash"] = ActionController::Flash::FlashHash.new.update(flash) if flash
407:       build_request_uri(action, parameters)
408:       @controller.process(@request, @response)
409:     end

[Source]

     # File vendor/rails/actionpack/lib/action_controller/test_process.rb, line 441
441:     def redirect_to_url
442:       @response.redirect_url
443:     end

[Source]

     # File vendor/rails/actionpack/lib/action_controller/test_process.rb, line 429
429:     def session
430:       @response.session
431:     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

[Source]

     # File vendor/rails/actionpack/lib/action_controller/test_process.rb, line 509
509:     def with_routing
510:       real_routes = ActionController::Routing::Routes
511:       ActionController::Routing.module_eval { remove_const :Routes }
512: 
513:       temporary_routes = ActionController::Routing::RouteSet.new
514:       ActionController::Routing.module_eval { const_set :Routes, temporary_routes }
515: 
516:       yield temporary_routes
517:     ensure
518:       if ActionController::Routing.const_defined? :Routes
519:         ActionController::Routing.module_eval { remove_const :Routes }
520:       end
521:       ActionController::Routing.const_set(:Routes, real_routes) if real_routes
522:     end
xhr(request_method, action, parameters = nil, session = nil, flash = nil)

Alias for xml_http_request

[Source]

     # File vendor/rails/actionpack/lib/action_controller/test_process.rb, line 411
411:     def xml_http_request(request_method, action, parameters = nil, session = nil, flash = nil)
412:       @request.env['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest'
413:       @request.env['HTTP_ACCEPT'] = 'text/javascript, text/html, application/xml, text/xml, */*'
414:       returning __send__(request_method, action, parameters, session, flash) do
415:         @request.env.delete 'HTTP_X_REQUESTED_WITH'
416:         @request.env.delete 'HTTP_ACCEPT'
417:       end
418:     end

[Validate]