2008-06-13 03:21:03 -04:00
|
|
|
require 'stringio'
|
|
|
|
require 'uri'
|
2010-04-05 15:15:08 -04:00
|
|
|
require 'active_support/core_ext/kernel/singleton_class'
|
2010-08-09 09:14:00 -04:00
|
|
|
require 'active_support/core_ext/object/try'
|
2009-12-12 19:09:44 -05:00
|
|
|
require 'rack/test'
|
2010-03-16 07:26:14 -04:00
|
|
|
require 'test/unit/assertions'
|
2009-09-23 23:37:52 -04:00
|
|
|
|
|
|
|
module ActionDispatch
|
2006-03-27 22:19:27 -05:00
|
|
|
module Integration #:nodoc:
|
2009-04-30 15:28:42 -04:00
|
|
|
module RequestHelpers
|
|
|
|
# Performs a GET request with the given parameters.
|
|
|
|
#
|
|
|
|
# - +path+: The URI (as a String) on which you want to perform a GET
|
|
|
|
# request.
|
|
|
|
# - +parameters+: The HTTP parameters that you want to pass. This may
|
|
|
|
# be +nil+,
|
|
|
|
# a Hash, or a String that is appropriately encoded
|
|
|
|
# (<tt>application/x-www-form-urlencoded</tt> or
|
|
|
|
# <tt>multipart/form-data</tt>).
|
|
|
|
# - +headers+: Additional HTTP headers to pass, as a Hash. The keys will
|
|
|
|
# automatically be upcased, with the prefix 'HTTP_' added if needed.
|
|
|
|
#
|
|
|
|
# This method returns an Response object, which one can use to
|
|
|
|
# inspect the details of the response. Furthermore, if this method was
|
2009-09-23 23:37:52 -04:00
|
|
|
# called from an ActionDispatch::IntegrationTest object, then that
|
2009-04-30 15:28:42 -04:00
|
|
|
# object's <tt>@response</tt> instance variable will point to the same
|
|
|
|
# response object.
|
|
|
|
#
|
|
|
|
# You can also perform POST, PUT, DELETE, and HEAD requests with +post+,
|
|
|
|
# +put+, +delete+, and +head+.
|
|
|
|
def get(path, parameters = nil, headers = nil)
|
|
|
|
process :get, path, parameters, headers
|
|
|
|
end
|
|
|
|
|
|
|
|
# Performs a POST request with the given parameters. See get() for more
|
|
|
|
# details.
|
|
|
|
def post(path, parameters = nil, headers = nil)
|
|
|
|
process :post, path, parameters, headers
|
|
|
|
end
|
|
|
|
|
|
|
|
# Performs a PUT request with the given parameters. See get() for more
|
|
|
|
# details.
|
|
|
|
def put(path, parameters = nil, headers = nil)
|
|
|
|
process :put, path, parameters, headers
|
|
|
|
end
|
|
|
|
|
|
|
|
# Performs a DELETE request with the given parameters. See get() for
|
|
|
|
# more details.
|
|
|
|
def delete(path, parameters = nil, headers = nil)
|
|
|
|
process :delete, path, parameters, headers
|
|
|
|
end
|
|
|
|
|
|
|
|
# Performs a HEAD request with the given parameters. See get() for more
|
|
|
|
# details.
|
|
|
|
def head(path, parameters = nil, headers = nil)
|
|
|
|
process :head, path, parameters, headers
|
|
|
|
end
|
|
|
|
|
|
|
|
# Performs an XMLHttpRequest request with the given parameters, mirroring
|
|
|
|
# a request from the Prototype library.
|
|
|
|
#
|
|
|
|
# The request_method is :get, :post, :put, :delete or :head; the
|
|
|
|
# parameters are +nil+, a hash, or a url-encoded or multipart string;
|
|
|
|
# the headers are a hash. Keys are automatically upcased and prefixed
|
|
|
|
# with 'HTTP_' if not already.
|
|
|
|
def xml_http_request(request_method, path, parameters = nil, headers = nil)
|
|
|
|
headers ||= {}
|
2009-05-19 17:48:05 -04:00
|
|
|
headers['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest'
|
|
|
|
headers['HTTP_ACCEPT'] ||= [Mime::JS, Mime::HTML, Mime::XML, 'text/xml', Mime::ALL].join(', ')
|
2009-04-30 15:28:42 -04:00
|
|
|
process(request_method, path, parameters, headers)
|
|
|
|
end
|
|
|
|
alias xhr :xml_http_request
|
|
|
|
|
|
|
|
# Follow a single redirect response. If the last response was not a
|
|
|
|
# redirect, an exception will be raised. Otherwise, the redirect is
|
|
|
|
# performed on the location header.
|
|
|
|
def follow_redirect!
|
|
|
|
raise "not a redirect! #{status} #{status_message}" unless redirect?
|
|
|
|
get(response.location)
|
|
|
|
status
|
|
|
|
end
|
|
|
|
|
|
|
|
# Performs a request using the specified method, following any subsequent
|
|
|
|
# redirect. Note that the redirects are followed until the response is
|
|
|
|
# not a redirect--this means you may run into an infinite loop if your
|
|
|
|
# redirect loops back to itself.
|
|
|
|
def request_via_redirect(http_method, path, parameters = nil, headers = nil)
|
|
|
|
process(http_method, path, parameters, headers)
|
|
|
|
follow_redirect! while redirect?
|
|
|
|
status
|
|
|
|
end
|
|
|
|
|
|
|
|
# Performs a GET request, following any subsequent redirect.
|
|
|
|
# See +request_via_redirect+ for more information.
|
|
|
|
def get_via_redirect(path, parameters = nil, headers = nil)
|
|
|
|
request_via_redirect(:get, path, parameters, headers)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Performs a POST request, following any subsequent redirect.
|
|
|
|
# See +request_via_redirect+ for more information.
|
|
|
|
def post_via_redirect(path, parameters = nil, headers = nil)
|
|
|
|
request_via_redirect(:post, path, parameters, headers)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Performs a PUT request, following any subsequent redirect.
|
|
|
|
# See +request_via_redirect+ for more information.
|
|
|
|
def put_via_redirect(path, parameters = nil, headers = nil)
|
|
|
|
request_via_redirect(:put, path, parameters, headers)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Performs a DELETE request, following any subsequent redirect.
|
|
|
|
# See +request_via_redirect+ for more information.
|
|
|
|
def delete_via_redirect(path, parameters = nil, headers = nil)
|
|
|
|
request_via_redirect(:delete, path, parameters, headers)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-08-28 18:04:14 -04:00
|
|
|
# An instance of this class represents a set of requests and responses
|
|
|
|
# performed sequentially by a test process. Because you can instantiate
|
2006-02-28 13:15:46 -05:00
|
|
|
# multiple sessions and run them side-by-side, you can also mimic (to some
|
|
|
|
# limited extent) multiple simultaneous users interacting with your system.
|
|
|
|
#
|
2008-12-07 17:37:48 -05:00
|
|
|
# Typically, you will instantiate a new session using
|
|
|
|
# IntegrationTest#open_session, rather than instantiating
|
|
|
|
# Integration::Session directly.
|
2006-02-28 13:15:46 -05:00
|
|
|
class Session
|
2009-05-11 23:04:39 -04:00
|
|
|
DEFAULT_HOST = "www.example.com"
|
|
|
|
|
2006-09-05 19:04:14 -04:00
|
|
|
include Test::Unit::Assertions
|
2009-12-12 19:09:44 -05:00
|
|
|
include TestProcess, RequestHelpers, Assertions
|
2006-02-28 13:15:46 -05:00
|
|
|
|
2009-04-30 15:28:42 -04:00
|
|
|
%w( status status_message headers body redirect? ).each do |method|
|
|
|
|
delegate method, :to => :response, :allow_nil => true
|
|
|
|
end
|
2009-02-01 02:01:49 -05:00
|
|
|
|
2009-04-30 15:28:42 -04:00
|
|
|
%w( path ).each do |method|
|
|
|
|
delegate method, :to => :request, :allow_nil => true
|
|
|
|
end
|
2006-02-28 13:15:46 -05:00
|
|
|
|
|
|
|
# The hostname used in the last request.
|
2010-04-05 18:01:33 -04:00
|
|
|
def host
|
|
|
|
@host || DEFAULT_HOST
|
|
|
|
end
|
|
|
|
attr_writer :host
|
2006-03-17 22:27:37 -05:00
|
|
|
|
|
|
|
# The remote_addr used in the last request.
|
|
|
|
attr_accessor :remote_addr
|
2006-02-28 13:15:46 -05:00
|
|
|
|
2006-03-18 10:09:27 -05:00
|
|
|
# The Accept header to send.
|
|
|
|
attr_accessor :accept
|
|
|
|
|
2006-02-28 13:15:46 -05:00
|
|
|
# A map of the cookies returned by the last response, and which will be
|
|
|
|
# sent with the next request.
|
2009-05-11 23:04:39 -04:00
|
|
|
def cookies
|
2010-04-05 18:01:33 -04:00
|
|
|
_mock_session.cookie_jar
|
2009-05-11 23:04:39 -04:00
|
|
|
end
|
2006-02-28 13:15:46 -05:00
|
|
|
|
|
|
|
# A reference to the controller instance used by the last request.
|
|
|
|
attr_reader :controller
|
|
|
|
|
|
|
|
# A reference to the request instance used by the last request.
|
|
|
|
attr_reader :request
|
|
|
|
|
|
|
|
# A reference to the response instance used by the last request.
|
|
|
|
attr_reader :response
|
|
|
|
|
2007-11-01 03:59:24 -04:00
|
|
|
# A running counter of the number of requests processed.
|
|
|
|
attr_accessor :request_count
|
|
|
|
|
2010-02-26 20:18:29 -05:00
|
|
|
include ActionDispatch::Routing::UrlFor
|
|
|
|
|
2008-05-25 07:29:00 -04:00
|
|
|
# Create and initialize a new Session instance.
|
2009-09-26 21:51:05 -04:00
|
|
|
def initialize(app)
|
|
|
|
@app = app
|
2010-02-26 20:18:29 -05:00
|
|
|
|
|
|
|
# If the app is a Rails app, make url_helpers available on the session
|
|
|
|
# This makes app.url_for and app.foo_path available in the console
|
|
|
|
if app.respond_to?(:routes) && app.routes.respond_to?(:url_helpers)
|
|
|
|
singleton_class.class_eval { include app.routes.url_helpers }
|
|
|
|
end
|
|
|
|
|
2006-02-28 13:15:46 -05:00
|
|
|
reset!
|
|
|
|
end
|
|
|
|
|
2010-03-15 22:26:58 -04:00
|
|
|
def default_url_options
|
|
|
|
{ :host => host, :protocol => https? ? "https" : "http" }
|
2010-02-26 20:18:29 -05:00
|
|
|
end
|
|
|
|
|
2006-02-28 13:15:46 -05:00
|
|
|
# Resets the instance. This can be used to reset the state information
|
|
|
|
# in an existing session instance, so it can be used from a clean-slate
|
|
|
|
# condition.
|
|
|
|
#
|
|
|
|
# session.reset!
|
|
|
|
def reset!
|
|
|
|
@https = false
|
|
|
|
@controller = @request = @response = nil
|
2010-04-05 18:01:33 -04:00
|
|
|
@_mock_session = nil
|
2007-11-01 03:59:24 -04:00
|
|
|
@request_count = 0
|
2007-05-21 05:15:16 -04:00
|
|
|
|
2009-05-11 23:04:39 -04:00
|
|
|
self.host = DEFAULT_HOST
|
2006-03-17 22:27:37 -05:00
|
|
|
self.remote_addr = "127.0.0.1"
|
2008-12-07 17:37:48 -05:00
|
|
|
self.accept = "text/xml,application/xml,application/xhtml+xml," +
|
|
|
|
"text/html;q=0.9,text/plain;q=0.8,image/png," +
|
|
|
|
"*/*;q=0.5"
|
2006-03-06 23:42:24 -05:00
|
|
|
|
2007-01-28 02:16:55 -05:00
|
|
|
unless defined? @named_routes_configured
|
2006-03-06 23:42:24 -05:00
|
|
|
# install the named routes in this session instance.
|
2010-02-25 12:32:29 -05:00
|
|
|
klass = singleton_class
|
2006-03-06 23:42:24 -05:00
|
|
|
|
|
|
|
# the helpers are made protected by default--we make them public for
|
|
|
|
# easier access during testing and troubleshooting.
|
|
|
|
@named_routes_configured = true
|
|
|
|
end
|
2006-02-28 13:15:46 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
# Specify whether or not the session should mimic a secure HTTPS request.
|
|
|
|
#
|
|
|
|
# session.https!
|
|
|
|
# session.https!(false)
|
2008-12-07 17:37:48 -05:00
|
|
|
def https!(flag = true)
|
2007-05-21 05:15:16 -04:00
|
|
|
@https = flag
|
2006-02-28 13:15:46 -05:00
|
|
|
end
|
|
|
|
|
2008-07-16 08:00:36 -04:00
|
|
|
# Return +true+ if the session is mimicking a secure HTTPS request.
|
2006-02-28 13:15:46 -05:00
|
|
|
#
|
|
|
|
# if session.https?
|
|
|
|
# ...
|
|
|
|
# end
|
|
|
|
def https?
|
|
|
|
@https
|
|
|
|
end
|
|
|
|
|
|
|
|
# Set the host name to use in the next request.
|
|
|
|
#
|
2006-03-25 14:43:40 -05:00
|
|
|
# session.host! "www.example.com"
|
2006-02-28 13:15:46 -05:00
|
|
|
def host!(name)
|
|
|
|
@host = name
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
2010-04-05 18:01:33 -04:00
|
|
|
def _mock_session
|
|
|
|
@_mock_session ||= Rack::MockSession.new(@app, host)
|
|
|
|
end
|
2009-05-11 23:04:39 -04:00
|
|
|
|
2006-02-28 13:15:46 -05:00
|
|
|
# Performs the actual request.
|
2009-05-19 17:48:05 -04:00
|
|
|
def process(method, path, parameters = nil, rack_environment = nil)
|
2009-04-30 15:28:42 -04:00
|
|
|
if path =~ %r{://}
|
|
|
|
location = URI.parse(path)
|
|
|
|
https! URI::HTTPS === location if location.scheme
|
|
|
|
host! location.host if location.host
|
|
|
|
path = location.query ? "#{location.path}?#{location.query}" : location.path
|
|
|
|
end
|
2006-02-28 13:15:46 -05:00
|
|
|
|
2010-01-20 09:59:26 -05:00
|
|
|
unless ActionController::Base < ActionController::Testing
|
|
|
|
ActionController::Base.class_eval do
|
|
|
|
include ActionController::Testing
|
2009-04-24 21:24:03 -04:00
|
|
|
end
|
2006-02-28 13:15:46 -05:00
|
|
|
end
|
|
|
|
|
2010-09-22 20:09:06 -04:00
|
|
|
port = host.split(':')[1]
|
|
|
|
|
2009-09-01 00:08:20 -04:00
|
|
|
env = {
|
2009-04-30 18:26:03 -04:00
|
|
|
:method => method,
|
2009-04-24 21:24:03 -04:00
|
|
|
:params => parameters,
|
2008-12-07 17:37:48 -05:00
|
|
|
|
2010-04-05 18:01:33 -04:00
|
|
|
"SERVER_NAME" => host.split(':')[0],
|
2010-09-22 20:09:06 -04:00
|
|
|
"SERVER_PORT" => (port ? port : (https? ? "443" : "80")),
|
2008-12-07 17:37:48 -05:00
|
|
|
"HTTPS" => https? ? "on" : "off",
|
|
|
|
"rack.url_scheme" => https? ? "https" : "http",
|
|
|
|
|
2006-02-28 13:15:46 -05:00
|
|
|
"REQUEST_URI" => path,
|
2010-09-22 20:09:06 -04:00
|
|
|
"HTTP_HOST" => [host, port].compact.join(':'),
|
2006-03-17 22:27:37 -05:00
|
|
|
"REMOTE_ADDR" => remote_addr,
|
2006-02-28 13:15:46 -05:00
|
|
|
"CONTENT_TYPE" => "application/x-www-form-urlencoded",
|
2010-04-02 14:54:10 -04:00
|
|
|
"HTTP_ACCEPT" => accept
|
2009-04-24 21:24:03 -04:00
|
|
|
}
|
2009-08-31 16:39:19 -04:00
|
|
|
|
2010-04-05 18:01:33 -04:00
|
|
|
session = Rack::Test::Session.new(_mock_session)
|
2010-04-03 23:23:23 -04:00
|
|
|
|
2009-05-19 17:48:05 -04:00
|
|
|
(rack_environment || {}).each do |key, value|
|
2009-04-28 17:25:04 -04:00
|
|
|
env[key] = value
|
|
|
|
end
|
|
|
|
|
2010-04-05 18:01:33 -04:00
|
|
|
# NOTE: rack-test v0.5 doesn't build a default uri correctly
|
|
|
|
# Make sure requested path is always a full uri
|
|
|
|
uri = URI.parse('/')
|
|
|
|
uri.scheme ||= env['rack.url_scheme']
|
|
|
|
uri.host ||= env['SERVER_NAME']
|
|
|
|
uri.port ||= env['SERVER_PORT'].try(:to_i)
|
|
|
|
uri += path
|
|
|
|
|
|
|
|
session.request(uri.to_s, env)
|
2009-04-26 12:37:11 -04:00
|
|
|
|
2007-11-01 03:59:24 -04:00
|
|
|
@request_count += 1
|
2009-09-01 00:08:20 -04:00
|
|
|
@request = ActionDispatch::Request.new(session.last_request.env)
|
2010-04-05 18:01:33 -04:00
|
|
|
response = _mock_session.last_response
|
2010-02-19 22:19:20 -05:00
|
|
|
@response = ActionDispatch::TestResponse.new(response.status, response.headers, response.body)
|
2009-04-26 12:37:11 -04:00
|
|
|
@html_document = nil
|
2008-12-17 09:21:20 -05:00
|
|
|
|
2010-01-20 09:59:26 -05:00
|
|
|
@controller = session.last_request.env['action_controller.instance']
|
|
|
|
|
2009-04-30 15:28:42 -04:00
|
|
|
return response.status
|
2006-02-28 13:15:46 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2007-11-01 03:59:24 -04:00
|
|
|
module Runner
|
2010-03-17 20:52:16 -04:00
|
|
|
include ActionDispatch::Assertions
|
|
|
|
|
2009-09-26 22:38:48 -04:00
|
|
|
def app
|
|
|
|
@app
|
|
|
|
end
|
|
|
|
|
2007-11-01 03:59:24 -04:00
|
|
|
# Reset the current session. This is useful for testing multiple sessions
|
|
|
|
# in a single test case.
|
|
|
|
def reset!
|
2010-03-22 20:04:56 -04:00
|
|
|
@integration_session = Integration::Session.new(app)
|
2007-11-01 03:59:24 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
%w(get post put head delete cookies assigns
|
2008-08-19 19:53:46 -04:00
|
|
|
xml_http_request xhr get_via_redirect post_via_redirect).each do |method|
|
2007-11-01 03:59:24 -04:00
|
|
|
define_method(method) do |*args|
|
|
|
|
reset! unless @integration_session
|
|
|
|
# reset the html_document variable, but only for new get/post calls
|
|
|
|
@html_document = nil unless %w(cookies assigns).include?(method)
|
2010-07-25 04:21:16 -04:00
|
|
|
@integration_session.__send__(method, *args).tap do
|
2007-11-01 03:59:24 -04:00
|
|
|
copy_session_variables!
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Open a new session instance. If a block is given, the new session is
|
|
|
|
# yielded to the block before being returned.
|
|
|
|
#
|
|
|
|
# session = open_session do |sess|
|
|
|
|
# sess.extend(CustomAssertions)
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# By default, a single session is automatically created for you, but you
|
|
|
|
# can use this method to open multiple sessions that ought to be tested
|
|
|
|
# simultaneously.
|
2009-04-24 21:24:03 -04:00
|
|
|
def open_session(app = nil)
|
2010-03-22 20:04:56 -04:00
|
|
|
dup.tap do |session|
|
|
|
|
yield session if block_given?
|
2007-11-01 03:59:24 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Copy the instance variables from the current session instance into the
|
|
|
|
# test instance.
|
|
|
|
def copy_session_variables! #:nodoc:
|
|
|
|
return unless @integration_session
|
|
|
|
%w(controller response request).each do |var|
|
2008-08-31 16:15:26 -04:00
|
|
|
instance_variable_set("@#{var}", @integration_session.__send__(var))
|
2007-11-01 03:59:24 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-02-25 20:51:56 -05:00
|
|
|
extend ActiveSupport::Concern
|
|
|
|
include ActionDispatch::Routing::UrlFor
|
|
|
|
|
2010-02-26 18:20:41 -05:00
|
|
|
def url_options
|
2010-02-26 20:18:29 -05:00
|
|
|
reset! unless @integration_session
|
|
|
|
@integration_session.url_options
|
2010-02-25 20:51:56 -05:00
|
|
|
end
|
|
|
|
|
2007-11-01 03:59:24 -04:00
|
|
|
# Delegate unhandled messages to the current session instance.
|
|
|
|
def method_missing(sym, *args, &block)
|
|
|
|
reset! unless @integration_session
|
2009-08-30 23:46:48 -04:00
|
|
|
if @integration_session.respond_to?(sym)
|
2010-07-25 04:21:16 -04:00
|
|
|
@integration_session.__send__(sym, *args, &block).tap do
|
2009-08-30 23:46:48 -04:00
|
|
|
copy_session_variables!
|
|
|
|
end
|
|
|
|
else
|
|
|
|
super
|
2007-11-01 03:59:24 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2006-02-28 13:15:46 -05:00
|
|
|
end
|
|
|
|
|
2010-08-28 18:04:14 -04:00
|
|
|
# An test that spans multiple controllers and actions,
|
2006-02-28 13:15:46 -05:00
|
|
|
# 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.
|
|
|
|
#
|
2010-08-28 18:04:14 -04:00
|
|
|
# At its simplest, you simply extend <tt>IntegrationTest</tt> and write your tests
|
2006-02-28 13:15:46 -05:00
|
|
|
# using the get/post methods:
|
|
|
|
#
|
2009-12-22 20:25:34 -05:00
|
|
|
# require "test_helper"
|
2006-02-28 13:15:46 -05:00
|
|
|
#
|
2010-09-24 20:15:52 -04:00
|
|
|
# class ExampleTest < ActionDispatch::IntegrationTest
|
2006-02-28 13:15:46 -05:00
|
|
|
# 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
|
2010-08-28 18:04:14 -04:00
|
|
|
# reference any named routes you happen to have defined.
|
2006-02-28 13:15:46 -05:00
|
|
|
#
|
2009-12-22 20:25:34 -05:00
|
|
|
# require "test_helper"
|
2006-02-28 13:15:46 -05:00
|
|
|
#
|
2010-09-24 20:15:52 -04:00
|
|
|
# class AdvancedTest < ActionDispatch::IntegrationTest
|
2006-02-28 13:15:46 -05:00
|
|
|
# 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
|
2008-06-13 03:21:03 -04:00
|
|
|
class IntegrationTest < ActiveSupport::TestCase
|
2007-11-01 03:59:24 -04:00
|
|
|
include Integration::Runner
|
2010-03-17 20:52:16 -04:00
|
|
|
include ActionController::TemplateAssertions
|
2009-09-26 22:38:48 -04:00
|
|
|
|
|
|
|
@@app = nil
|
|
|
|
|
|
|
|
def self.app
|
2009-09-26 22:59:28 -04:00
|
|
|
# DEPRECATE Rails application fallback
|
|
|
|
# This should be set by the initializer
|
2009-10-16 20:04:28 -04:00
|
|
|
@@app || (defined?(Rails.application) && Rails.application) || nil
|
2009-09-26 22:38:48 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.app=(app)
|
|
|
|
@@app = app
|
|
|
|
end
|
|
|
|
|
|
|
|
def app
|
|
|
|
super || self.class.app
|
|
|
|
end
|
2006-02-28 13:15:46 -05:00
|
|
|
end
|
2007-10-02 01:32:14 -04:00
|
|
|
end
|