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



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

def method_missing(selector, *args)
  return @controller.send(selector, *args) if ActionController::Routing::NamedRoutes::Helpers.include?(selector)
  return super
end

Class Method Details

.included(base) ⇒ Object



331
332
333
334
335
336
337
338
339
340
341
# File 'lib/action_controller/test_process.rb', line 331

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 @request
        process(action, parameters, session, flash)
      end
    EOV
  end
end

Instance Method Details

#assigns(key = nil) ⇒ Object



384
385
386
387
388
389
390
# File 'lib/action_controller/test_process.rb', line 384

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



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

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



400
401
402
# File 'lib/action_controller/test_process.rb', line 400

def cookies
  @response.cookies
end

#find_all_tag(conditions) ⇒ Object



426
427
428
# File 'lib/action_controller/test_process.rb', line 426

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

#find_tag(conditions) ⇒ Object



422
423
424
# File 'lib/action_controller/test_process.rb', line 422

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


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

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

#flashObject



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

def flash
  @response.flash
end

#follow_redirectObject



376
377
378
379
380
381
382
# File 'lib/action_controller/test_process.rb', line 376

def follow_redirect
  if @response.redirected_to[:controller]
    raise "Can't follow redirects outside of current controller (#{@response.redirected_to[:controller]})"
  end

  get(@response.redirected_to.delete(:action), @response.redirected_to.stringify_keys)
end

#html_documentObject



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

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



344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
# File 'lib/action_controller/test_process.rb', line 344

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|
    raise "@#{iv_name} is nil: make sure you set it in your test's setup method." if instance_variable_get("@#{iv_name}").nil?
  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_urlObject



404
405
406
# File 'lib/action_controller/test_process.rb', line 404

def redirect_to_url
  @response.redirect_url
end

#sessionObject



392
393
394
# File 'lib/action_controller/test_process.rb', line 392

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 { set.connect ':controller/:id/:action' }
  assert_equal(
     ['/content/10/show', {}],
     set.generate(:controller => 'content', :id => 10, :action => 'show')
  )
end


459
460
461
462
463
464
465
466
467
468
469
470
471
472
# File 'lib/action_controller/test_process.rb', line 459

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



366
367
368
369
370
371
372
373
# File 'lib/action_controller/test_process.rb', line 366

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