Module: Sinatra::TestHelpers

Extended by:
Forwardable
Includes:
Rack::Test::Methods
Defined in:
lib/sinatra/test_helpers.rb

Overview

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

Instance Attribute Summary collapse

Instance Methods delegated to last_response collapse

Class Methods delegated to app collapse

Instance Methods delegated to current_session collapse

Instance Methods delegated to rack_mock_session collapse

Instance Method Summary collapse

Instance Attribute Details

#settingsObject

Returns the value of attribute settings.



20
21
22
# File 'lib/sinatra/test_helpers.rb', line 20

def settings
  @settings
end

Class Method Details

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

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



# File 'lib/sinatra/test_helpers.rb', line 53

.disable(*opts) ⇒ Object

Same as calling ‘set :option, false` for each of the given options.



# File 'lib/sinatra/test_helpers.rb', line 71

.enable(*opts) ⇒ Object

Same as calling ‘set :option, true` for each of the given options.



# File 'lib/sinatra/test_helpers.rb', line 66

.helpers(*extensions, &block) ⇒ Object

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



# File 'lib/sinatra/test_helpers.rb', line 80

.register(*extensions, &block) ⇒ Object

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



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

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

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

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)


# File 'lib/sinatra/test_helpers.rb', line 60

.use(middleware, *args, &block) ⇒ Object

Use the specified Rack middleware



# File 'lib/sinatra/test_helpers.rb', line 76

Instance Method Details

#appSinatra::Base

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.)

Returns:

  • (Sinatra::Base)


151
152
153
154
# File 'lib/sinatra/test_helpers.rb', line 151

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

#app=(base) ⇒ Object Also known as: set_app

Replaces the configured app.

Parameters:

  • base (Sinatra::Base)

    a configured app



136
137
138
# File 'lib/sinatra/test_helpers.rb', line 136

def app=(base)
  @app = base
end

#bodyString

Body of last_response

Returns:

  • (String)

    body of the last response

See Also:



# File 'lib/sinatra/test_helpers.rb', line 24

Returns:

  • (Rack::Test::CookieJar)


110
# File 'lib/sinatra/test_helpers.rb', line 110

def_delegators :rack_mock_session, :cookie_jar

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

Return the Rack environment used for a request to ‘uri`.

Returns:

  • (Hash)


101
# File '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



48
# File '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



# File 'lib/sinatra/test_helpers.rb', line 31

#last_envObject

Returns The env of the last request.

Returns:

  • The env of the last request



198
199
200
# File 'lib/sinatra/test_helpers.rb', line 198

def last_env
  last_request.env
end

#last_request?Boolean

Returns:

  • (Boolean)


181
182
183
184
185
186
# File 'lib/sinatra/test_helpers.rb', line 181

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

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

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.

Parameters:

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

    App base class

Returns:

  • (Sinatra)

    Configured mocked app



123
124
125
126
127
128
129
130
131
# File '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) ⇒ Object

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

Parameters:

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


162
163
164
165
# File '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) ⇒ Object

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

Parameters:

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


174
175
176
177
# File '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 Session of last request, or the empty Hash.

Returns:

  • (Hash)

    Session of last request, or the empty Hash

Raises:

  • (Rack::Test:Error)

    If sessions are not enabled for app



190
191
192
193
194
195
# File '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



# File 'lib/sinatra/test_helpers.rb', line 37