An integration test 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 "test_helper"

  class ExampleTest < ActionDispatch::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 "test_helper"

  class AdvancedTest < ActionDispatch::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
Methods
A
U
Included Modules
Class Public methods
app()
     # File actionpack/lib/action_dispatch/testing/integration.rb, line 480
480:     def self.app
481:       # DEPRECATE Rails application fallback
482:       # This should be set by the initializer
483:       @@app || (defined?(Rails.application) && Rails.application) || nil
484:     end
app=(app)
     # File actionpack/lib/action_dispatch/testing/integration.rb, line 486
486:     def self.app=(app)
487:       @@app = app
488:     end
Instance Public methods
app()
     # File actionpack/lib/action_dispatch/testing/integration.rb, line 490
490:     def app
491:       super || self.class.app
492:     end
url_options()
     # File actionpack/lib/action_dispatch/testing/integration.rb, line 494
494:     def url_options
495:       reset! unless integration_session
496:       integration_session.url_options
497:     end