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
-
#settings ⇒ Object
Returns the value of attribute settings.
Instance Methods delegated to last_response collapse
-
#body ⇒ String
Body of last_response.
-
#errors ⇒ Array
Errors of last_response.
-
#headers ⇒ Hash
Headers of last_response.
-
#status ⇒ Integer
HTTP status of last_response.
Class Methods delegated to app collapse
-
.configure(*envs) {|_self| ... } ⇒ Object
Set configuration options for Sinatra and/or the app.
-
.disable(*opts) ⇒ Object
Same as calling ‘set :option, false` for each of the given options.
-
.enable(*opts) ⇒ Object
Same as calling ‘set :option, true` for each of the given options.
-
.helpers(*extensions, &block) ⇒ Object
Makes the methods defined in the block and in the Modules given in ‘extensions` available to the handlers and templates.
-
.register(*extensions, &block) ⇒ Object
Register an extension.
-
.set(option, value = (not_set = true), ignore_setter = false, &block) ⇒ Object
Sets an option to the given value.
-
.use(middleware, *args, &block) ⇒ Object
Use the specified Rack middleware.
Instance Methods delegated to current_session collapse
-
#env_for(uri = "", opts = {}) ⇒ Hash
Return the Rack environment used for a request to ‘uri`.
Instance Methods delegated to rack_mock_session collapse
Instance Method Summary collapse
-
#app ⇒ Sinatra::Base
Returns a Rack::Lint-wrapped Sinatra app.
-
#app=(base) ⇒ Object
(also: #set_app)
Replaces the configured app.
-
#last_env ⇒ Object
The env of the last request.
- #last_request? ⇒ Boolean
-
#mock_app(base = Sinatra::Base, &block) ⇒ Sinatra
Instantiate and configure a mock Sinatra app.
-
#options(uri, params = {}, env = {}, &block) ⇒ Object
Processes an OPTIONS request in the context of the current session.
-
#patch(uri, params = {}, env = {}, &block) ⇒ Object
Processes a PATCH request in the context of the current session.
-
#session ⇒ Hash
Session of last request, or the empty Hash.
Instance Attribute Details
#settings ⇒ Object
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.
|
# 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.
|
# 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
#app ⇒ Sinatra::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.)
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.
136 137 138 |
# File 'lib/sinatra/test_helpers.rb', line 136 def app=(base) @app = base end |
#body ⇒ String
Body of last_response
|
# File 'lib/sinatra/test_helpers.rb', line 24
|
#cookie_jar ⇒ Rack::Test::CookieJar
Returns a 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`.
101 |
# File 'lib/sinatra/test_helpers.rb', line 101 def_delegators :current_session, :env_for |
#errors ⇒ Array
Errors of last_response
48 |
# File 'lib/sinatra/test_helpers.rb', line 48 def_delegators :last_response, :body, :headers, :status, :errors |
#headers ⇒ Hash
Headers of last_response
|
# File 'lib/sinatra/test_helpers.rb', line 31
|
#last_env ⇒ Object
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
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.
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.
162 163 164 165 |
# File 'lib/sinatra/test_helpers.rb', line 162 def (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.
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 |
#session ⇒ Hash
Returns Session of last request, or the empty Hash.
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 |
#status ⇒ Integer
HTTP status of last_response
|
# File 'lib/sinatra/test_helpers.rb', line 37
|