Module: ActionController::TestProcess

Included in:
Integration::Session, TestCase, ActionView::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, &block) ⇒ Object



507
508
509
510
511
512
513
# File 'lib/action_controller/test_process.rb', line 507

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

Class Method Details

.included(base) ⇒ Object



395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
# File 'lib/action_controller/test_process.rb', line 395

def self.included(base)
  # Executes a request simulating GET HTTP method and set/volley the response
  def get(action, parameters = nil, session = nil, flash = nil)
    process(action, parameters, session, flash, "GET")
  end

  # Executes a request simulating POST HTTP method and set/volley the response
  def post(action, parameters = nil, session = nil, flash = nil)
    process(action, parameters, session, flash, "POST")
  end

  # Executes a request simulating PUT HTTP method and set/volley the response
  def put(action, parameters = nil, session = nil, flash = nil)
    process(action, parameters, session, flash, "PUT")
  end

  # Executes a request simulating DELETE HTTP method and set/volley the response
  def delete(action, parameters = nil, session = nil, flash = nil)
    process(action, parameters, session, flash, "DELETE")
  end

  # Executes a request simulating HEAD HTTP method and set/volley the response
  def head(action, parameters = nil, session = nil, flash = nil)
    process(action, parameters, session, flash, "HEAD")
  end
end

Instance Method Details

#assigns(key = nil) ⇒ Object



460
461
462
463
464
465
466
# File 'lib/action_controller/test_process.rb', line 460

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



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

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



476
477
478
# File 'lib/action_controller/test_process.rb', line 476

def cookies
  @response.cookies
end

#delete(action, parameters = nil, session = nil, flash = nil) ⇒ Object

Executes a request simulating DELETE HTTP method and set/volley the response



412
413
414
# File 'lib/action_controller/test_process.rb', line 412

def delete(action, parameters = nil, session = nil, flash = nil)
  process(action, parameters, session, flash, "DELETE")
end

#find_all_tag(conditions) ⇒ Object



503
504
505
# File 'lib/action_controller/test_process.rb', line 503

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

#find_tag(conditions) ⇒ Object



499
500
501
# File 'lib/action_controller/test_process.rb', line 499

def find_tag(conditions)
  html_document.find(conditions)
end

#fixture_file_upload(path, mime_type = nil, binary = false) ⇒ Object

Shortcut for ActionController::TestUploadedFile.new(ActionController::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)


523
524
525
526
# File 'lib/action_controller/test_process.rb', line 523

def fixture_file_upload(path, mime_type = nil, binary = false)
  fixture_path = ActionController::TestCase.send(:fixture_path) if ActionController::TestCase.respond_to?(:fixture_path)
  ActionController::TestUploadedFile.new("#{fixture_path}#{path}", mime_type, binary)
end

#flashObject



472
473
474
# File 'lib/action_controller/test_process.rb', line 472

def flash
  @response.flash
end

#get(action, parameters = nil, session = nil, flash = nil) ⇒ Object

Executes a request simulating GET HTTP method and set/volley the response



397
398
399
# File 'lib/action_controller/test_process.rb', line 397

def get(action, parameters = nil, session = nil, flash = nil)
  process(action, parameters, session, flash, "GET")
end

#head(action, parameters = nil, session = nil, flash = nil) ⇒ Object

Executes a request simulating HEAD HTTP method and set/volley the response



417
418
419
# File 'lib/action_controller/test_process.rb', line 417

def head(action, parameters = nil, session = nil, flash = nil)
  process(action, parameters, session, flash, "HEAD")
end

#html_documentObject



494
495
496
497
# File 'lib/action_controller/test_process.rb', line 494

def html_document
  xml = @response.content_type =~ /xml$/
  @html_document ||= HTML::Document.new(@response.body, false, xml)
end

#post(action, parameters = nil, session = nil, flash = nil) ⇒ Object

Executes a request simulating POST HTTP method and set/volley the response



402
403
404
# File 'lib/action_controller/test_process.rb', line 402

def post(action, parameters = nil, session = nil, flash = nil)
  process(action, parameters, session, flash, "POST")
end

#process(action, parameters = nil, session = nil, flash = nil, http_method = 'GET') ⇒ Object



422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
# File 'lib/action_controller/test_process.rb', line 422

def process(action, parameters = nil, session = nil, flash = nil, http_method = 'GET')
  # 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'] = http_method

  @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)

  Base.class_eval { include ProcessWithTest } unless Base < ProcessWithTest
  @controller.process_with_test(@request, @response)
end

#put(action, parameters = nil, session = nil, flash = nil) ⇒ Object

Executes a request simulating PUT HTTP method and set/volley the response



407
408
409
# File 'lib/action_controller/test_process.rb', line 407

def put(action, parameters = nil, session = nil, flash = nil)
  process(action, parameters, session, flash, "PUT")
end

#redirect_to_urlObject



480
481
482
# File 'lib/action_controller/test_process.rb', line 480

def redirect_to_url
  @response.redirect_url
end

#sessionObject



468
469
470
# File 'lib/action_controller/test_process.rb', line 468

def session
  @request.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


545
546
547
548
549
550
551
552
553
554
555
556
557
558
# File 'lib/action_controller/test_process.rb', line 545

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



450
451
452
453
454
455
456
457
# File 'lib/action_controller/test_process.rb', line 450

def xml_http_request(request_method, action, parameters = nil, session = nil, flash = nil)
  @request.env['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest'
  @request.env['HTTP_ACCEPT'] =  [Mime::JS, Mime::HTML, Mime::XML, 'text/xml', Mime::ALL].join(', ')
  returning __send__(request_method, action, parameters, session, flash) do
    @request.env.delete 'HTTP_X_REQUESTED_WITH'
    @request.env.delete 'HTTP_ACCEPT'
  end
end