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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(selector, *args) ⇒ Object



467
468
469
470
471
472
473
# File 'lib/action_controller/test_process.rb', line 467

def method_missing(selector, *args)
  if ActionController::Routing::Routes.named_routes.helpers.include?(selector)
    @controller.send(selector, *args)
  else
    super
  end
end

Class Method Details

.included(base) ⇒ Object



370
371
372
373
374
375
376
377
378
379
380
381
# File 'lib/action_controller/test_process.rb', line 370

def self.included(base)
  # execute the request simulating a specific HTTP method and set/volley the response
  # TODO: this should be un-DRY'ed for the sake of API documentation.
  %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



420
421
422
423
424
425
426
# File 'lib/action_controller/test_process.rb', line 420

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



444
445
446
447
448
449
450
451
452
# File 'lib/action_controller/test_process.rb', line 444

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

#cookiesObject



436
437
438
# File 'lib/action_controller/test_process.rb', line 436

def cookies
  @response.cookies
end

#find_all_tag(conditions) ⇒ Object



463
464
465
# File 'lib/action_controller/test_process.rb', line 463

def find_all_tag(conditions)
  html_document.find_all(conditions)
end

#find_tag(conditions) ⇒ Object



459
460
461
# File 'lib/action_controller/test_process.rb', line 459

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)


483
484
485
486
487
488
489
# File 'lib/action_controller/test_process.rb', line 483

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

#flashObject



432
433
434
# File 'lib/action_controller/test_process.rb', line 432

def flash
  @response.flash
end

#html_documentObject



454
455
456
457
# File 'lib/action_controller/test_process.rb', line 454

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



384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
# File 'lib/action_controller/test_process.rb', line 384

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!
  @response.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_urlObject



440
441
442
# File 'lib/action_controller/test_process.rb', line 440

def redirect_to_url
  @response.redirect_url
end

#sessionObject



428
429
430
# File 'lib/action_controller/test_process.rb', line 428

def session
  @response.session
end

#with_routingObject

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


508
509
510
511
512
513
514
515
516
517
518
519
520
521
# File 'lib/action_controller/test_process.rb', line 508

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



410
411
412
413
414
415
416
417
# File 'lib/action_controller/test_process.rb', line 410

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