Class: ActionDispatch::IntegrationTest

Inherits:
ActiveSupport::TestCase show all
Includes:
ActionController::TemplateAssertions, ActionDispatch::Integration::Runner, Routing::UrlFor
Defined in:
actionpack/lib/action_dispatch/testing/integration.rb,
railties/lib/rails/test_help.rb

Overview

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

Constant Summary collapse

@@app =
nil

Constants included from Assertions

Assertions::NO_STRIP

Constants inherited from ActiveSupport::TestCase

ActiveSupport::TestCase::Assertion

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Routing::UrlFor

#initialize, #url_for

Methods included from ActiveSupport::Concern

#append_features, extended, #included

Methods included from Routing::PolymorphicRoutes

#polymorphic_path, #polymorphic_url

Methods included from ActionController::ModelNaming

#convert_to_model, #model_name_from_record_or_class

Methods included from ActionController::TemplateAssertions

#assert_template, #process, #setup_subscriptions, #teardown_subscriptions

Methods included from ActionDispatch::Integration::Runner

#copy_session_variables!, #default_url_options, #default_url_options=, #method_missing, #open_session, #reset!, #respond_to?

Methods included from Assertions::TagAssertions

#assert_no_tag, #assert_tag, #find_all_tag, #find_tag, #html_document

Methods included from Assertions::SelectorAssertions

#assert_select, #assert_select_email, #assert_select_encoded, #count_description, #css_select

Methods included from Assertions::RoutingAssertions

#assert_generates, #assert_recognizes, #assert_routing, #method_missing, #with_routing

Methods included from Assertions::ResponseAssertions

#assert_redirected_to, #assert_response

Methods included from Assertions::DomAssertions

#assert_dom_equal, #assert_dom_not_equal

Methods inherited from ActiveSupport::TestCase

#assert_nothing_raised, for_tag, test_order

Methods included from ActiveSupport::Testing::Declarative

extended, #test

Methods included from ActiveRecord::TestFixtures

#after_teardown, #before_setup, #enlist_fixture_connections, #run_in_transaction?, #setup_fixtures, #teardown_fixtures

Methods included from ActiveSupport::Testing::Deprecation

#assert_deprecated, #assert_not_deprecated

Methods included from ActiveSupport::Testing::Assertions

#assert_difference, #assert_no_difference, #assert_not

Methods included from ActiveSupport::Testing::SetupAndTeardown

#after_teardown, #before_setup

Methods included from ActiveSupport::Testing::TaggedLogging

#before_setup

Dynamic Method Handling

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

Class Method Details

.appObject



489
490
491
# File 'actionpack/lib/action_dispatch/testing/integration.rb', line 489

def self.app
  @@app || ActionDispatch.test_app
end

.app=(app) ⇒ Object



493
494
495
# File 'actionpack/lib/action_dispatch/testing/integration.rb', line 493

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

Instance Method Details

#appObject



497
498
499
# File 'actionpack/lib/action_dispatch/testing/integration.rb', line 497

def app
  super || self.class.app
end

#url_optionsObject



501
502
503
504
# File 'actionpack/lib/action_dispatch/testing/integration.rb', line 501

def url_options
  reset! unless integration_session
  integration_session.url_options
end