123456789_123456789_123456789_123456789_123456789_

Module: Sinatra::TestHelpers

Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
self, Forwardable
Instance Chain:
self, Rack::Test::Methods
Defined in: sinatra-contrib/lib/sinatra/test_helpers.rb

Overview

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

Instance Methods delegated to last_response

Class Methods delegated to app

Instance Methods delegated to current_session

Instance Methods delegated to rack_mock_session

Instance Attribute Summary

Instance Method Summary

Class Method Details

.configure(*envs) {|_self| ... }) {|_self| ... }

Set configuration options for ::Sinatra and/or the app. Allows scoping of settings for certain environments.

Yield Parameters:

  • _self (Sinatra::Base)

    the object that the method was called on

[ GitHub ]

.disable(*opts)

Same as calling set <code>:option</code>, false for each of the given options.

[ GitHub ]

.enable(*opts)

Same as calling set <code>:option</code>, true for each of the given options.

[ GitHub ]

.helpers(*extensions, &block)

Makes the methods defined in the block and in the Modules given in extensions available to the handlers and templates.

[ GitHub ]

.register(*extensions, &block)

Register an extension. Alternatively take a block from which an extension will be created and registered on the fly.

[ GitHub ]

  
# File 'sinatra-contrib/lib/sinatra/test_helpers.rb', line 91

def_delegators :app, :configure, :set, :enable, :disable, :use, :helpers, :register

.set(option, value = (not_set, ignore_setter = false, &block)

Sets an option to the given value. If the value is a proc, the proc will be called every time the option is accessed.

Raises:

  • (ArgumentError)
[ GitHub ]

.use(middleware, *args, &block)

Use the specified ::Rack middleware

[ GitHub ]

Instance Attribute Details

#appSinatra::Base (rw)

Returns a Rack::Lint-wrapped ::Sinatra app.

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

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

[ GitHub ]

  
# File 'sinatra-contrib/lib/sinatra/test_helpers.rb', line 151

def app
  @app ||= Class.new Sinatra::Base
  Rack::Lint.new @app
end

#app=(base) (rw) Also known as: #set_app

Replaces the configured app.

Parameters:

[ GitHub ]

  
# File 'sinatra-contrib/lib/sinatra/test_helpers.rb', line 136

def app=(base)
  @app = base
end

#last_request?Boolean (readonly)

[ GitHub ]

  
# File 'sinatra-contrib/lib/sinatra/test_helpers.rb', line 181

def last_request?
  last_request
  true
rescue Rack::Test::Error
  false
end

#settings (rw)

[ GitHub ]

  
# File 'sinatra-contrib/lib/sinatra/test_helpers.rb', line 20

attr_accessor :settings

Instance Method Details

#bodyString

Body of last_response

Returns:

  • (String)

    body of the last response

See Also:

[ GitHub ]

#env_for(uri = "", opts = {}) ⇒ Hash

Return the ::Rack environment used for a request to uri.

[ GitHub ]

  
# File 'sinatra-contrib/lib/sinatra/test_helpers.rb', line 101

def_delegators :current_session, :env_for

#errorsArray

Errors of last_response

Returns:

  • (Array)

    errors of the last response

[ GitHub ]

  
# File 'sinatra-contrib/lib/sinatra/test_helpers.rb', line 48

def_delegators :last_response, :body, :headers, :status, :errors

#headersHash

Headers of last_response

Returns:

  • (Hash)

    hash of the last response

[ GitHub ]

#last_envObject

Returns:

  • The env of the last request

[ GitHub ]

  
# File 'sinatra-contrib/lib/sinatra/test_helpers.rb', line 198

def last_env
  last_request.env
end

#mock_app(base = Sinatra::Base, &block) ⇒ Sinatra

Instantiate and configure a mock ::Sinatra app.

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

Parameters:

  • base (Sinatra::Base) (defaults to: Sinatra::Base)

    App base class

Returns:

  • (Sinatra)

    Configured mocked app

[ GitHub ]

  
# File 'sinatra-contrib/lib/sinatra/test_helpers.rb', line 123

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)

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

Parameters:

  • uri (String)
  • params (Hash) (defaults to: {})
  • env (Hash) (defaults to: {})
[ GitHub ]

  
# File 'sinatra-contrib/lib/sinatra/test_helpers.rb', line 162

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)

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

Parameters:

  • uri (String)
  • params (Hash) (defaults to: {})
  • env (Hash) (defaults to: {})
[ GitHub ]

  
# File 'sinatra-contrib/lib/sinatra/test_helpers.rb', line 174

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

#sessionHash

Returns:

  • (Hash)

    Session of last request, or the empty Hash

Raises:

  • (Rack::Test:Error)

    If sessions are not enabled for app

[ GitHub ]

  
# File 'sinatra-contrib/lib/sinatra/test_helpers.rb', line 190

def session
  return {} unless last_request?
  raise Rack::Test::Error, 'session not enabled for app' unless last_env['rack.session'] || app.session?

  last_request.session
end

#statusInteger

HTTP status of last_response

Returns:

  • (Integer)

    HTTP status of the last response

[ GitHub ]