Module: Rack::Test::Methods

Extended by:
Forwardable
Defined in:
lib/rack/test/methods.rb

Overview

This module serves as the primary integration point for using Rack::Test in a testing environment. It depends on an app method being defined in the same context, and provides the Rack::Test API methods (see Rack::Test::Session for their documentation). It defines the following methods that are delegated to the current session: :request, :get, :post, :put, :patch, :delete, :options, :head, :custom_request, :follow_redirect!, :header, :env, :set_cookie, :clear_cookies, :authorize, :basic_authorize, :last_response, and :last_request.

Example:

class HomepageTest < Test::Unit::TestCase
  include Rack::Test::Methods

  def app
    MyApp
  end
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#_rack_test_current_session=(value) ⇒ Object

Private accessor to avoid uninitialized instance variable warning in Ruby 2.*



90
91
92
# File 'lib/rack/test/methods.rb', line 90

def _rack_test_current_session=(value)
  @_rack_test_current_session = value
end

Instance Method Details

#build_rack_test_session(_name) ⇒ Object

Create a new Rack::Test::Session for #app.



40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/rack/test/methods.rb', line 40

def build_rack_test_session(_name) # :nodoc:
  if respond_to?(:build_rack_mock_session, true)
    # Backwards compatibility for capybara
    build_rack_mock_session
  else
    if respond_to?(:default_host)
      Session.new(app, default_host)
    else
      Session.new(app)
    end
  end
end

#current_sessionObject

Return the currently actively session. This is the session to which the delegated methods are sent.



55
56
57
# File 'lib/rack/test/methods.rb', line 55

def current_session
  @_rack_test_current_session ||= rack_test_session
end

#rack_test_session(name = :default) ⇒ Object Also known as: rack_mock_session

Return the existing session with the given name, or a new rack session. Always use a new session if name is nil.



29
30
31
32
33
34
# File 'lib/rack/test/methods.rb', line 29

def rack_test_session(name = :default) # :nodoc:
  return build_rack_test_session(name) unless name

  @_rack_test_sessions ||= {}
  @_rack_test_sessions[name] ||= build_rack_test_session(name)
end

#with_session(name) ⇒ Object

Create a new session (or reuse an existing session with the given name), and make it the current session for the given block.



61
62
63
64
65
66
# File 'lib/rack/test/methods.rb', line 61

def with_session(name)
  session = _rack_test_current_session
  yield(@_rack_test_current_session = rack_test_session(name))
ensure
  @_rack_test_current_session = session
end