Class: Test::Rails::FunctionalTestCase

Inherits:
TestCase
  • Object
show all
Includes:
ActionController::Assertions::SelectorAssertions, ActionController::TestProcess
Defined in:
lib/test/rails/functional_test_case.rb

Overview

FunctionalTestCase is an abstract class that sets up a controller instance for its subclasses.

Direct Known Subclasses

ControllerTestCase, HelperTestCase, ViewTestCase

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#flashObject (readonly)

Flash accessor. The flash can be assigned to before calling process or render and it will Just Work (yay!)

view:

<div class="error"><%= flash[:error] %></div>

test:

flash[:error] = 'You did a bad thing.'
render
assert_tag :tag => 'div', :attributes => { :class => 'error' },
           :content => 'You did a bad thing.'


62
63
64
# File 'lib/test/rails/functional_test_case.rb', line 62

def flash
  @flash
end

#sessionObject (readonly)

Session accessor. The session can be assigned to before calling process or render and it will Just Work (yay!)

test:

def test_logout
  session[:user] = users(:herbert)
  post :logout
  assert_equal nil, session[:user]
end


76
77
78
# File 'lib/test/rails/functional_test_case.rb', line 76

def session
  @session
end

Instance Method Details

#setupObject

Sets up instance variables to allow tests depending on a controller work.

setup uses the instance variable @controller_class_name to determine which controller class to instantiate.

setup also instantiates a new @request and @response object.

If you need to perform extra setup actions, define #setup_extra and FunctionalTestCase will call it after performing the rest of its setup actions.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/test/rails/functional_test_case.rb', line 24

def setup
  return if self.class.name =~ /TestCase$/ and not $TESTING_RTC
  super

  @controller_class = Object.path2class @controller_class_name
  raise "Can't determine controller class for #{self.class}" if @controller_class.nil?

  @controller = @controller_class.new

  @session = ActionController::TestSession.new

  @flash = ActionController::Flash::FlashHash.new
  @session['flash'] = @flash

  @request = ActionController::TestRequest.new
  @request.session = @session

  # HACK There's probably an official way to do this
  @controller.instance_variable_set :@_session, @request.session

  @response = ActionController::TestResponse.new

  setup_extra if respond_to? :setup_extra
end