Class: ActionDispatch::IntegrationTest

Inherits:
ActiveSupport::TestCase
  • Object
show all
Includes:
ActionController::TemplateAssertions, ActionDispatch::Integration::Runner
Defined in:
lib/action_dispatch/testing/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 "test_helper"

class ExampleTest < ActionController::IntegrationTest
  fixtures :people

  def 
    # 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 < 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

Constant Summary collapse

@@app =
nil

Constants included from Assertions

Assertions::NO_STRIP

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ActionController::TemplateAssertions

#assert_template, #setup_subscriptions, #teardown_subscriptions

Methods included from ActionDispatch::Integration::Runner

#copy_session_variables!, #method_missing, #open_session, #reset!, #url_options

Methods included from Routing::UrlFor

#url_for, #url_options

Methods included from Routing::PolymorphicRoutes

#polymorphic_path, #polymorphic_url

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class ActionDispatch::Integration::Runner

Class Method Details

.appObject



457
458
459
460
461
# File 'lib/action_dispatch/testing/integration.rb', line 457

def self.app
  # DEPRECATE Rails application fallback
  # This should be set by the initializer
  @@app || (defined?(Rails.application) && Rails.application) || nil
end

.app=(app) ⇒ Object



463
464
465
# File 'lib/action_dispatch/testing/integration.rb', line 463

def self.app=(app)
  @@app = app
end

Instance Method Details

#appObject



467
468
469
# File 'lib/action_dispatch/testing/integration.rb', line 467

def app
  super || self.class.app
end