module Sinatra::TestHelpers
Helper methods to ease testing your Sinatra
application. Partly extracted from Sinatra
. Testing framework agnostic.
Attributes
Public Instance Methods
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 151 def app @app ||= Class.new Sinatra::Base Rack::Lint.new @app end
Replaces the configured app.
@param base [Sinatra::Base] a configured app
# File lib/sinatra/test_helpers.rb, line 136 def app=(base) @app = base end
@return The env of the last request
# File lib/sinatra/test_helpers.rb, line 198 def last_env last_request.env end
@return [Boolean]
# File lib/sinatra/test_helpers.rb, line 181 def last_request? last_request true rescue Rack::Test::Error false end
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 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
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 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
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 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
@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 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