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
Delegate unhandled messages to the current session instance.
[ show source ]
# File vendor/rails/actionpack/lib/action_controller/integration.rb, line 545
545: def method_missing(sym, *args, &block)
546: reset! unless @integration_session
547: returning @integration_session.send(sym, *args, &block) do
548: copy_session_variables!
549: end
550: end
Open a new session instance. If a block is given, the new session is yielded to the block before being returned.
session = open_session do |sess|
sess.extend(CustomAssertions)
end
By default, a single session is automatically created for you, but you can use this method to open multiple sessions that ought to be tested simultaneously.
[ show source ]
# File vendor/rails/actionpack/lib/action_controller/integration.rb, line 514
514: def open_session
515: session = Integration::Session.new
516:
517: # delegate the fixture accessors back to the test instance
518: extras = Module.new { attr_accessor :delegate, :test_result }
519: self.class.fixture_table_names.each do |table_name|
520: name = table_name.tr(".", "_")
521: next unless respond_to?(name)
522: extras.send(:define_method, name) { |*args| delegate.send(name, *args) }
523: end
524:
525: # delegate add_assertion to the test case
526: extras.send(:define_method, :add_assertion) { test_result.add_assertion }
527: session.extend(extras)
528: session.delegate = self
529: session.test_result = @_result
530:
531: yield session if block_given?
532: session
533: end
Reset the current session. This is useful for testing multiple sessions in a single test case.
[ show source ]
# File vendor/rails/actionpack/lib/action_controller/integration.rb, line 489
489: def reset!
490: @integration_session = open_session
491: end