module Sinatra::TestHelpers

Helper methods to ease testing your Sinatra application. Partly extracted from Sinatra. Testing framework agnostic.

Attributes

settings[RW]

Public Instance Methods

app() click to toggle source

Returns a Rack::Lint-wrapped Sinatra app.

If no app has been configured, a new subclass of Sinatra::Base will be used and stored.

(Rack::Lint validates your application and the requests and responses according to the Rack spec.)

@return [Sinatra::Base]

# File lib/sinatra/test_helpers.rb, line 162
def app
  @app ||= Class.new Sinatra::Base
  Rack::Lint.new @app
end
app=(base) click to toggle source

Replaces the configured app.

@param base [Sinatra::Base] a configured app

# File lib/sinatra/test_helpers.rb, line 147
def app=(base)
  @app = base
end
Also aliased as: set_app
last_env() click to toggle source

@return The env of the last request

# File lib/sinatra/test_helpers.rb, line 208
def last_env
  last_request.env
end
last_request?() click to toggle source

@return [Boolean]

# File lib/sinatra/test_helpers.rb, line 192
def last_request?
  last_request
  true
rescue Rack::Test::Error
  false
end
mock_app(base = Sinatra::Base, &block) click to toggle source

Instantiate and configure a mock Sinatra app.

Takes a `base` app class, or defaults to Sinatra::Base, and instantiates an app instance. Any given code in `block` is `class_eval`'d on this new instance before the instance is returned.

@param base [Sinatra::Base] App base class

@return [Sinatra] Configured mocked app

# File lib/sinatra/test_helpers.rb, line 134
def mock_app(base = Sinatra::Base, &block)
  inner = nil
  @app  = Sinatra.new(base) do
    inner = self
    class_eval(&block)
  end
  @settings = inner
  app
end
options(uri, params = {}, env = {}, &block) click to toggle source

Processes an OPTIONS request in the context of the current session.

@param uri [String] @param params [Hash] @param env [Hash]

# File lib/sinatra/test_helpers.rb, line 173
def options(uri, params = {}, env = {}, &block)
  env = env_for(uri, env.merge(:method => "OPTIONS", :params => params))
  current_session.send(:process_request, uri, env, &block)
end
patch(uri, params = {}, env = {}, &block) click to toggle source

Processes a PATCH request in the context of the current session.

@param uri [String] @param params [Hash] @param env [Hash]

# File lib/sinatra/test_helpers.rb, line 185
def patch(uri, params = {}, env = {}, &block)
  env = env_for(uri, env.merge(:method => "PATCH", :params => params))
  current_session.send(:process_request, uri, env, &block)
end
session() click to toggle source

@raise [Rack::Test:Error] If sessions are not enabled for app @return [Hash] Session of last request, or the empty Hash

# File lib/sinatra/test_helpers.rb, line 201
def session
  return {} unless last_request?
  raise Rack::Test::Error, "session not enabled for app" unless last_env["rack.session"] or app.session?
  last_request.session
end
set_app(base)
Alias for: app=