[~/rack/rack-unit-test(master)]$ cat rack_hello_world.rb # my_app.rb # require 'rack' class MyApp def call env [200, {"Content-Type" => "text/html"}, ["Hello"]] end end
[~/rack/rack-unit-test(master)]$ cat test_hello_world.rb require "test/unit" require "rack/test" require './rack_hello_world' class AppTest < Test::Unit::TestCase include Rack::Test::Methods def app Rack::Builder.new do run MyApp.new end.to_app end def test_index get "/" #puts last_response.inspect assert last_response.ok? end def test_body get "/" assert_equal last_response.body, 'Hello', "body must be hello" end end
Rack::Test
in a testing environment.
It depends on an app
method
being defined in the same context,
def app Rack::Builder.new do run MyApp.new end.to_app end
and provides the Rack::Test
API
methods (see
Rack::Test::Session
for their documentation).
get
method issue a GET
request for the given URI.
Stores the issues request object in #last_request
and the app's response in
#last_response
(whose class is
Rack::MockResponse) .
Yield #last_response
to a block if given.
def test_index get "/" assert last_response.ok? end
(Object) basic_authorize(username, password) (also: #authorize)
Set the username and password for HTTP
Basic authorization, to be included in subsequent requests in the HTTP_AUTHORIZATION
header.
(Object) delete(uri, params = {}, env = {}, &block)
Issue a DELETE
request for the given URI.
(Object) digest_authorize(username, password)
Set the username and password for HTTP
Digest authorization, to be included in subsequent requests in the HTTP_AUTHORIZATION
header.
(Object) env(name, value)
Set an env var to be included on all subsequent requests through the session.
(Object) follow_redirect!
Rack::Test will not follow any redirects automatically.
(Object) get(uri, params = {}, env = {}, &block)
Issue a GET
request for the given URI
with the given params and Rack environment.
(Object) head(uri, params = {}, env = {}, &block)
Issue a HEAD
request for the given URI.
(Object) header(name, value)
Set a header to be included on all subsequent requests through the session.
(Session) initialize(mock_session) constructor
Creates a Rack::Test::Session for a given Rack app or Rack::MockSession.
(Object) options(uri, params = {}, env = {}, &block)
Issue an OPTIONS
request for the given URI.
(Object) patch(uri, params = {}, env = {}, &block)
Issue a PATCH
request for the given URI.
(Object) post(uri, params = {}, env = {}, &block)
Issue a POST
request for the given URI.
(Object) put(uri, params = {}, env = {}, &block)
Issue a PUT
request for the given URI.
(Object) request(uri, env = {}, &block)
Issue a request to the Rack app for the given URI
and optional Rack environment.
#last_response
object has methods:
=~(other) body() empty?() match(other)and attributes:
errors [RW] original_headers[R] Headers
app
:
def app Rack::Builder.new do use(Rack::Session::Cookie, {:key => 'rack session',· #:domain => 'localhost', #:path => '/', #:expire_after => 2592000,· :secret => 'change_me'}) run RockPaperScissors::App.new end.to_app end
last_response.body
returns the last response received in the session. Raises an error
if no requests have been sent yet.
[~/rack/rack-unit-test(master)]$ cat Rakefile task :default => :test desc "run the tests" task :test do sh "ruby test_hello_world.rb" end
[~/rack/rack-unit-test(master)]$ cat Gemfile source 'https://rubygems.org' gem 'rack' gem 'rack-test'
[~/rack/rack-unit-test(master)]$ rake ruby test_hello_world.rb Run options: # Running tests: .. Finished tests in 0.015253s, 131.1217 tests/s, 131.1217 assertions/s. 2 tests, 2 assertions, 0 failures, 0 errors, 0 skips
Casiano Rodriguez León 2015-01-07