Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Cleaning up the testing docs to work with Rack::Test

  • Loading branch information...
commit 7a7fd4c574e7db0dd58702ea6419564e18bc1b1c 1 parent ca9cc0d
@tannerburson tannerburson authored
Showing with 53 additions and 102 deletions.
  1. +53 −102 testing.markdown
View
155 testing.markdown
@@ -7,17 +7,10 @@ id: testing
Testing with Sinatra
====================
-**NOTE: Sinatra's built-in test framework -- and everything described
-on this page -- is deprecated. Please see
+**NOTE: Sinatra's built-in test framework has been deprecated. This page uses the new recommended test framework
[Rack::Test](http://gitrdoc.com/brynary/rack-test/tree/master) instead.**
-Sinatra includes utility classes and modules to assist in testing
-applications. The utilities are based heavily on Rack's
-[`MockRequest`][Rack::MockRequest] and [`MockResponse`][Rack::MockResponse]
-objects — a basic understanding of these classes are highly
-recommended.
-
-All examples in the following sections assume that `Test::Unit` is being
+All examples in the follow sections assume that `Test::Unit` is being
used in an attempt to be as general as possible. See the [Test Framework
Examples](#frameworks) for information on using Sinatra's test helpers in
other testing environments.
@@ -37,39 +30,39 @@ assumed to be in a file named `hello_world.rb`:
"Hello World #{params[:name]}".strip
end
-Using The `Sinatra::Test` Mixin
+Using The `Rack::Test` Mixin
-------------------------------
-The `Sinatra::Test` module includes a variety of helper methods for
+The `Rack::Test` module includes a variety of helper methods for
simulating requests against an application and asserting expectations about
the response. It's typically included directly within the test context and
-makes a slew of helper methods and attributes available.
+makes a few helper methods and attributes available.
The following is a simple example that ensures the hello world app functions
properly:
require 'hello_world'
require 'test/unit'
- require 'sinatra/test'
+ require 'rack/test'
set :environment, :test
class HelloWorldTest < Test::Unit::TestCase
- include Sinatra::Test
+ include Rack::Test::Methods
def test_it_says_hello_world
get '/'
- assert response.ok?
- assert_equal 'Hello World', response.body
+ assert last_response.ok?
+ assert_equal 'Hello World', last_response.body
end
def test_it_says_hello_to_a_person
get '/', :name => 'Simon'
- assert response.body.include?('Simon')
+ assert last_response.body.include?('Simon')
end
end
-### Sinatra::Test's Mock Request Methods
+### Rack::Test's Mock Request Methods
The `get`, `put`, `post`, `delete`, and `head` methods simulate the
respective type of request on the application. Tests typically begin with
@@ -96,94 +89,51 @@ available for making assertions:
* `app` - The Sinatra application class that handled the mock request.
- * `request` - The [`Rack::MockRequest`][Rack::MockRequest] used to generate
+ * `last_request` - The [`Rack::MockRequest`][Rack::MockRequest] used to generate
the request.
- * `response` - A [`Rack::MockResponse`][Rack::MockResponse] instance with
+ * `last_response` - A [`Rack::MockResponse`][Rack::MockResponse] instance with
information on the response generated by the application.
-Assertions are typically made against the `response` object. Because this is
-so often the case, the `Sinatra::Test` module forwards missing methods
-directly to the `response` object, making assertions against the response
-especially terse. Consider the following examples:
+Assertions are typically made against the `last_response` object.
+Consider the following examples:
def test_it_says_hello_world
get '/'
- assert response.ok?
- assert_equal 'Hello World'.length.to_s, response.header['Content-Length']
- assert_equal 'Hello World', response.body
+ assert last_response.ok?
+ assert_equal 'Hello World'.length.to_s, last_response.header['Content-Length']
+ assert_equal 'Hello World', last_response.body
end
-And with the `response` references removed:
-
- def test_it_says_hello_world
- get '/'
- assert ok?
- assert_equal 'Hello World'.length.to_s, header['Content-Length']
- assert_equal 'Hello World', body
- end
### Optional Test Setup
-The `Sinatra::Test` mock request methods send requests to `@app`. If no
-`@app` variable is set, the `Sinatra::Application` class (this is the
-default application defined at the top-level) is assumed to be the target of
-requests.
+The `Rack::Test` mock request methods send requests to the return value of
+a method named `app`.
If you're testing a modular application that has multiple `Sinatra::Base`
-subclasses, simply set the `@app` instance variable in the test's `setup`
-method or before calling a mock request method:
+subclasses, simply set the `app` method to return your particular class.
- def setup
- @app = MySinatraApp
+ def app
+ MySinatraApp
end
-### Making `Sinatra::Test` available to all test cases
+### Making `Rack::Test` available to all test cases
-If you'd like the `Sinatra::Test` methods to be available to all test cases
-without having to include it each time, you can include the `Sinatra::Test`
+If you'd like the `Rack::Test` methods to be available to all test cases
+without having to include it each time, you can include the `Rack::Test`
module in the `Test::Unit::TestCase` class:
require 'test/unit'
- require 'sinatra/test'
+ require 'rack/test'
class Test::Unit::TestCase
- include Sinatra::Test
+ include Rack::Test::Methods
end
-Now all `TestCase` subclasses will automatically have `Sinatra::Test`
+Now all `TestCase` subclasses will automatically have `Rack::Test`
available to them.
-Using `Sinatra::TestHarness`
-----------------------------
-
-The `Sinatra::TestHarness` class works exactly like the `Sinatra::Test`
-module but moves all mock request methods and the request/response objects
-into a separate context so they're clearly separated from your test context.
-Although slightly more verbose, some feel this separation results in tests
-that are easier to understand and maintain.
-
- require 'hello_world'
- require 'test/unit'
- require 'sinatra/test'
-
- set :environment, :test
-
- class HelloWorldTest < Test::Unit::TestCase
- def setup
- @client = Sinatra::TestHarness.new(Sinatra::Application)
- end
-
- def test_it_says_hello_world
- response = @client.get('/')
- assert response.ok?
- assert_equal 'Hello World'.length.to_s, response.header['Content-Length']
- assert_equal 'Hello World', response.body
- end
-
- ...
- end
-
Test Framework Examples {#frameworks}
-----------------------
@@ -194,33 +144,33 @@ removed in Sinatra `1.0`.
### [RSpec][]
Sinatra can be tested under RSpec using the `spec/interop` library. The
-`Sinatra::Test` module should be included within the `describe` block:
+`Rack::Test` module should be included within the `describe` block:
require 'hello_world' # <-- your sinatra app
require 'spec'
require 'spec/interop/test'
- require 'sinatra/test'
+ require 'rack/test'
set :environment, :test
describe 'The HelloWorld App' do
- include Sinatra::Test
+ include Rack::Test::Methods
it "says hello" do
get '/'
- response.should be_ok
- response.body.should == 'Hello World'
+ last_response.should be_ok
+ last_response.body.should == 'Hello World'
end
end
-Make `Sinatra::Test` available to all spec contexts by including it in
+Make `Rack::Test` available to all spec contexts by including it in
`Test::Unit::TestCase`:
require 'spec'
require 'spec/interop/test'
- require 'sinatra/test'
+ require 'rack/test'
- Test::Unit::TestCase.send :include, Sinatra::Test
+ Test::Unit::TestCase.send :include, Rack::Test::Methods
### [Bacon][]
@@ -228,62 +178,62 @@ Testing with Bacon is similar to `test/unit` and RSpec:
require 'hello_world' # <-- your sinatra app
require 'bacon'
- require 'sinatra/test'
+ require 'rack/test'
set :environment, :test
describe 'The HelloWorld App' do
- include Sinatra::Test
+ include Rack::Test::Methods
it "says hello" do
get '/'
- response.should.be.ok
- response.body.should.equal 'Hello World'
+ last_response.should.be.ok
+ last_response.body.should.equal 'Hello World'
end
end
-Make `Sinatra::Test` available to all spec contexts by including it in
+Make `Rack::Test` available to all spec contexts by including it in
`Bacon::Context`:
class Bacon::Context
- include Sinatra::Test
+ include Rack::Test::Methods
end
### [Test::Spec][]
-The `Sinatra::Test` module should be included within the context of the
+The `Rack::Test` module should be included within the context of the
`describe` block:
require 'hello_world' # <-- your sinatra app
require 'test/spec'
- require 'sinatra/test'
+ require 'rack/test'
set :environment, :test
describe 'The HelloWorld App' do
- include Sinatra::Test
+ include Rack::Test::Methods
it "says hello" do
get '/'
- response.should.be.ok
- response.body.should.equal 'Hello World'
+ last_response.should.be.ok
+ last_response.body.should.equal 'Hello World'
end
end
-Make `Sinatra::Test` available to all spec contexts by including it in
+Make `Rack::Test` available to all spec contexts by including it in
`Test::Unit::TestCase`:
require 'test/spec'
- require 'sinatra/test'
+ require 'rack/test'
- Test::Unit::TestCase.send :include, Sinatra::Test
+ Test::Unit::TestCase.send :include, Rack::Test::Methods
<!-- TODO: Webrat -->
See Also
--------
-See the source for [Sinatra::Test][] and the [accompanying specs][test] for
+See the source for [Rack::Test][] for
more information on `get`, `post`, `put`, `delete` and friends.
[Test::Unit]: http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit.html
@@ -291,6 +241,7 @@ more information on `get`, `post`, `put`, `delete` and friends.
[Bacon]: http://github.com/chneukirchen/bacon
[Test::Spec]: http://rubyforge.org/projects/test-spec/
[Sinatra::Test]: http://github.com/sinatra/sinatra/blob/987d622a52f3eaf28f8f6beb7451c3e43c23f075/test/test_test.rb
+[Rack::Test]: http://github.com/brynary/rack-test/
[test]: http://github.com/sinatra/sinatra/blob/master/test/test_test.rb
[wild]: /wild.html
[spec]: http://rack.rubyforge.org/doc/files/SPEC.html

0 comments on commit 7a7fd4c

Please sign in to comment.
Something went wrong with that request. Please try again.