123456789_123456789_123456789_123456789_123456789_

Class: ActionDispatch::IntegrationTest

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

::ActiveSupport::Callbacks - Inherited

CALLBACK_FILTER_TYPES

::ActiveSupport::TestCase - Inherited

Assertion

::ActionController::TemplateAssertions - Included

RENDER_TEMPLATE_INSTANCE_VARIABLES

Routing::UrlFor - Attributes & Methods

Class Attribute Summary

Class Method Summary

::ActiveSupport::TestCase - Inherited

::ActiveSupport::DescendantsTracker - Inherited

clear, descendants, direct_descendants,
store_inherited

This is the only method that is not thread safe, but is only ever called during the eager loading phase.

::ActiveSupport::Testing::Declarative - Extended

test

Helper to define a test method using a ::String.

Instance Attribute Summary

Instance Method Summary

Routing::UrlFor - Included

#initialize,
#url_for

Generate a url based on the options provided, default_url_options and the routes defined in routes.rb.

#url_options

Hook overridden in controller to add request information with .default_url_options.

Routing::PolymorphicRoutes - Included

#polymorphic_path

Returns the path component of a URL for the given record.

#polymorphic_url

Constructs a call to a named RESTful route for the given record and returns the resulting URL string.

::ActionController::ModelNaming - Included

#convert_to_model

Converts the given object to an ::ActiveModel compliant one.

#model_name_from_record_or_class

::ActionController::TemplateAssertions - Included

#assert_template

Asserts that the request was rendered with the appropriate template file or partials.

#process, #reset_template_assertion, #setup_subscriptions, #teardown_subscriptions

Integration::Runner - Included

#app,
#method_missing

Delegate unhandled messages to the current session instance.

#open_session

Open a new session instance.

#reset!

Reset the current session.

#respond_to?

Assertions - Included

Assertions::RoutingAssertions - Included

#assert_generates

Asserts that the provided options can be used to generate the provided path.

#assert_recognizes

Asserts that the routing of the given path was handled correctly and that the parsed options (given in the expected_options hash) match path.

#assert_routing

Asserts that path and options match both ways; in other words, it verifies that path generates options and then that options generates path.

#method_missing

ROUTES TODO: These assertions should really work in an integration context.

#with_routing

A helper to make it easier to test different route configurations.

Assertions::ResponseAssertions - Included

#assert_redirected_to

Assert that the redirection options passed in match those of the redirect called in the latest action.

#assert_response

Asserts that the response is one of the following types:

::ActiveSupport::TestCase - Inherited

::ActiveRecord::TestFixtures - Included

::ActiveSupport::Testing::TimeHelpers - Included

#travel

Changes current time to the time in the future or in the past by a given time difference by stubbing Time.now, Date.today, and DateTime.now.

#travel_back

Returns the current time back to its original state, by removing the stubs added by travel and travel_to.

#travel_to

Changes current time to the given time by stubbing Time.now, Date.today, and DateTime.now to return the time or date passed into this method.

::ActiveSupport::Testing::Assertions - Included

#assert_difference

Test numeric difference between the return value of an expression as a result of what is evaluated in the yielded block.

#assert_no_difference

Assertion that the numeric result of evaluating an expression is not changed before and after invoking the passed in block.

#assert_not

Assert that an expression is not truthy.

::ActiveSupport::Callbacks - Inherited

#run_callbacks

Runs the callbacks for the given event.

Dynamic Method Handling

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

Class Attribute Details

.app (rw)

[ GitHub ]

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

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

.app=(app) (rw)

[ GitHub ]

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

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

.default_url_options (rw)

[ GitHub ]

  
# File 'actionpack/lib/action_dispatch/routing/url_for.rb', line 91

class_attribute :default_url_options

.default_url_options?Boolean (rw)

[ GitHub ]

  
# File 'actionpack/lib/action_dispatch/routing/url_for.rb', line 91

class_attribute :default_url_options

Instance Attribute Details

#default_url_options (rw)

[ GitHub ]

  
# File 'actionpack/lib/action_dispatch/routing/url_for.rb', line 91

class_attribute :default_url_options

#default_url_options?Boolean (rw)

[ GitHub ]

  
# File 'actionpack/lib/action_dispatch/routing/url_for.rb', line 91

class_attribute :default_url_options

Instance Method Details

#app

[ GitHub ]

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

def app
  super || self.class.app
end

#document_root_element

[ GitHub ]

  
# File 'actionpack/lib/action_dispatch/testing/integration.rb', line 510

def document_root_element
  html_document.root
end

#url_options

[ GitHub ]

  
# File 'actionpack/lib/action_dispatch/testing/integration.rb', line 505

def url_options
  reset! unless integration_session
  integration_session.url_options
end