Class: ActionController::IntegrationTest
- Inherits:
-
ActiveSupport::TestCase
- Object
- ActiveSupport::TestCase
- ActionController::IntegrationTest
- Includes:
- ActionController::Integration::Runner
- Defined in:
- lib/action_controller/integration.rb
Overview
An IntegrationTest is one that spans multiple controllers and actions, tying them all together to ensure they work together as expected. It tests more completely than either unit or functional tests do, exercising the entire stack, from the dispatcher to the database.
At its simplest, you simply extend IntegrationTest and write your tests using the get/post methods:
require "#{File.dirname(__FILE__)}/test_helper"
class ExampleTest < ActionController::IntegrationTest
fixtures :people
def test_login
# get the login page
get "/login"
assert_equal 200, status
# post the login and follow through to the home page
post "/login", :username => people(:jamis).username,
:password => people(:jamis).password
follow_redirect!
assert_equal 200, status
assert_equal "/home", path
end
end
However, you can also have multiple session instances open per test, and even extend those instances with assertions and methods to create a very powerful testing DSL that is specific for your application. You can even reference any named routes you happen to have defined!
require "#{File.dirname(__FILE__)}/test_helper"
class AdvancedTest < ActionController::IntegrationTest
fixtures :people, :rooms
def test_login_and_speak
jamis, david = login(:jamis), login(:david)
room = rooms(:office)
jamis.enter(room)
jamis.speak(room, "anybody home?")
david.enter(room)
david.speak(room, "hello!")
end
private
module CustomAssertions
def enter(room)
# reference a named route, for maximum internal consistency!
get(room_url(:id => room.id))
assert(...)
...
end
def speak(room, message)
xml_http_request "/say/#{room.id}", :message => message
assert(...)
...
end
end
def login(who)
open_session do |sess|
sess.extend(CustomAssertions)
who = people(who)
sess.post "/login", :username => who.username,
:password => who.password
assert(...)
end
end
end
Direct Known Subclasses
Class Method Summary collapse
-
.use_instantiated_fixtures ⇒ Object
:nodoc:.
-
.use_instantiated_fixtures=(flag) ⇒ Object
:nodoc:.
-
.use_transactional_fixtures ⇒ Object
:nodoc:.
-
.use_transactional_fixtures=(flag) ⇒ Object
:nodoc:.
Instance Method Summary collapse
-
#initialize(name) ⇒ IntegrationTest
constructor
Work around a bug in test/unit caused by the default test being named as a symbol (:default_test), which causes regex test filters (like “ruby test.rb -n /foo/”) to fail because =~ doesn’t work on symbols.
-
#run(*args) ⇒ Object
Work around test/unit’s requirement that every subclass of TestCase have at least one test method.
Methods included from ActionController::Integration::Runner
#copy_session_variables!, #method_missing, #open_session, #reset!
Constructor Details
#initialize(name) ⇒ IntegrationTest
Work around a bug in test/unit caused by the default test being named as a symbol (:default_test), which causes regex test filters (like “ruby test.rb -n /foo/”) to fail because =~ doesn’t work on symbols.
635 636 637 |
# File 'lib/action_controller/integration.rb', line 635 def initialize(name) #:nodoc: super(name.to_s) end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class ActionController::Integration::Runner
Class Method Details
.use_instantiated_fixtures ⇒ Object
:nodoc:
673 674 675 676 677 |
# File 'lib/action_controller/integration.rb', line 673 def use_instantiated_fixtures #:nodoc: @_use_instantiated_fixtures ? @use_instantiated_fixtures : superclass.use_instantiated_fixtures end |
.use_instantiated_fixtures=(flag) ⇒ Object
:nodoc:
662 663 664 665 |
# File 'lib/action_controller/integration.rb', line 662 def use_instantiated_fixtures=(flag) #:nodoc: @_use_instantiated_fixtures = true @use_instantiated_fixtures = flag end |
.use_transactional_fixtures ⇒ Object
:nodoc:
667 668 669 670 671 |
# File 'lib/action_controller/integration.rb', line 667 def use_transactional_fixtures #:nodoc: @_use_transactional_fixtures ? @use_transactional_fixtures : superclass.use_transactional_fixtures end |
.use_transactional_fixtures=(flag) ⇒ Object
:nodoc:
657 658 659 660 |
# File 'lib/action_controller/integration.rb', line 657 def use_transactional_fixtures=(flag) #:nodoc: @_use_transactional_fixtures = true @use_transactional_fixtures = flag end |
Instance Method Details
#run(*args) ⇒ Object
Work around test/unit’s requirement that every subclass of TestCase have at least one test method. Note that this implementation extends to all subclasses, as well, so subclasses of IntegrationTest may also exist without any test methods.
643 644 645 646 |
# File 'lib/action_controller/integration.rb', line 643 def run(*args) #:nodoc: return if @method_name == "default_test" super end |