Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 287 lines (205 sloc) 8.311 kb
78cbc0e @dashdotat Bootstrapping of changes to doc for testing
dashdotat authored
1 ---
4e899bf @rtomayko Link to Testing doc again and tidy up notes about deprecation
rtomayko authored
2 title: Testing Sinatra with Rack::Test
78cbc0e @dashdotat Bootstrapping of changes to doc for testing
dashdotat authored
3 layout: default
8c053f5 @rtomayko Minor spelling edits
rtomayko authored
4 id: testing
78cbc0e @dashdotat Bootstrapping of changes to doc for testing
dashdotat authored
5 ---
6
4e899bf @rtomayko Link to Testing doc again and tidy up notes about deprecation
rtomayko authored
7 Testing Sinatra with Rack::Test
8 ===============================
78cbc0e @dashdotat Bootstrapping of changes to doc for testing
dashdotat authored
9
4e899bf @rtomayko Link to Testing doc again and tidy up notes about deprecation
rtomayko authored
10 **NOTE: Sinatra's built-in test framework has been deprecated in favor of
11 [Rack::Test](http://gitrdoc.com/brynary/rack-test/tree/master) as of version
12 0.9.2. This document describes testing with Rack::Test.**
4d3383c @rtomayko Add links to Rack::Test and note that Sinatra::Test is deprecated
rtomayko authored
13
4d1d10e @tannerburson Add info about using Rack::Test without the mixin, as well as fix a
tannerburson authored
14 All examples in the following sections assume that `Test::Unit` is being
11372f2 @rtomayko Finish up test doc for now [#123]
rtomayko authored
15 used in an attempt to be as general as possible. See the [Test Framework
4e899bf @rtomayko Link to Testing doc again and tidy up notes about deprecation
rtomayko authored
16 Examples](#frameworks) for information on using the test helpers in
11372f2 @rtomayko Finish up test doc for now [#123]
rtomayko authored
17 other testing environments.
18
19 Example App: `hello_world.rb`
20 -----------------------------
21
22 The following example app is used to illustrate testing features. This is
23 assumed to be in a file named `hello_world.rb`:
24
25 require 'sinatra'
26
27 get '/' do
f465dd7 @sr Fix hello world test example
sr authored
28 "Hello World #{params[:name]}".strip
11372f2 @rtomayko Finish up test doc for now [#123]
rtomayko authored
29 end
78cbc0e @dashdotat Bootstrapping of changes to doc for testing
dashdotat authored
30
4d1d10e @tannerburson Add info about using Rack::Test without the mixin, as well as fix a
tannerburson authored
31 Using The `Rack::Test::Methods` Mixin
4e899bf @rtomayko Link to Testing doc again and tidy up notes about deprecation
rtomayko authored
32 -------------------------------------
78cbc0e @dashdotat Bootstrapping of changes to doc for testing
dashdotat authored
33
4d1d10e @tannerburson Add info about using Rack::Test without the mixin, as well as fix a
tannerburson authored
34 The `Rack::Test::Methods` module includes a variety of helper methods for
11372f2 @rtomayko Finish up test doc for now [#123]
rtomayko authored
35 simulating requests against an application and asserting expectations about
36 the response. It's typically included directly within the test context and
7a7fd4c @tannerburson Cleaning up the testing docs to work with Rack::Test
tannerburson authored
37 makes a few helper methods and attributes available.
4043048 @sr Explain how to install and use Sinatra::Test
sr authored
38
11372f2 @rtomayko Finish up test doc for now [#123]
rtomayko authored
39 The following is a simple example that ensures the hello world app functions
40 properly:
4043048 @sr Explain how to install and use Sinatra::Test
sr authored
41
11372f2 @rtomayko Finish up test doc for now [#123]
rtomayko authored
42 require 'hello_world'
43 require 'test/unit'
7a7fd4c @tannerburson Cleaning up the testing docs to work with Rack::Test
tannerburson authored
44 require 'rack/test'
78cbc0e @dashdotat Bootstrapping of changes to doc for testing
dashdotat authored
45
11372f2 @rtomayko Finish up test doc for now [#123]
rtomayko authored
46 set :environment, :test
47
48 class HelloWorldTest < Test::Unit::TestCase
7a7fd4c @tannerburson Cleaning up the testing docs to work with Rack::Test
tannerburson authored
49 include Rack::Test::Methods
11372f2 @rtomayko Finish up test doc for now [#123]
rtomayko authored
50
86f6eb3 @tannerburson Mention that you have to return Sinatra::Application from def app
tannerburson authored
51 def app
52 Sinatra::Application
53 end
54
11372f2 @rtomayko Finish up test doc for now [#123]
rtomayko authored
55 def test_it_says_hello_world
56 get '/'
7a7fd4c @tannerburson Cleaning up the testing docs to work with Rack::Test
tannerburson authored
57 assert last_response.ok?
58 assert_equal 'Hello World', last_response.body
11372f2 @rtomayko Finish up test doc for now [#123]
rtomayko authored
59 end
60
61 def test_it_says_hello_to_a_person
62 get '/', :name => 'Simon'
7a7fd4c @tannerburson Cleaning up the testing docs to work with Rack::Test
tannerburson authored
63 assert last_response.body.include?('Simon')
11372f2 @rtomayko Finish up test doc for now [#123]
rtomayko authored
64 end
65 end
66
4d1d10e @tannerburson Add info about using Rack::Test without the mixin, as well as fix a
tannerburson authored
67 Using `Rack::Test` without the Mixin
4e899bf @rtomayko Link to Testing doc again and tidy up notes about deprecation
rtomayko authored
68 ------------------------------------
4d1d10e @tannerburson Add info about using Rack::Test without the mixin, as well as fix a
tannerburson authored
69 For a variety of reasons you may not want to include `Rack::Test::Methods`
70 into your own classes. `Rack::Test` supports this style of testing as well, here is the above example without using Mixin.
71
72
73 require 'hello_world'
74 require 'test/unit'
75 require 'rack/test'
76
77 set :environment, :test
78
79 class HelloWorldTest < Test::Unit::TestCase
80
81 def test_it_says_hello_world
82 browser = Rack::Test::Session.new(Sinatra::Application)
83 browser.get '/'
84 assert browser.last_response.ok?
85 assert_equal 'Hello World', browser.last_response.body
86 end
87
88 def test_it_says_hello_to_a_person
89 browser = Rack::Test::Session.new(Sinatra::Application)
90 browser.get '/', :name => 'Simon'
91 assert browser.last_response.body.include?('Simon')
92 end
93 end
94
95
7a7fd4c @tannerburson Cleaning up the testing docs to work with Rack::Test
tannerburson authored
96 ### Rack::Test's Mock Request Methods
11372f2 @rtomayko Finish up test doc for now [#123]
rtomayko authored
97
98 The `get`, `put`, `post`, `delete`, and `head` methods simulate the
99 respective type of request on the application. Tests typically begin with
100 a call to one of these methods followed by one or more assertions against
101 the resulting response.
102
103 All mock request methods have the same argument signature:
104
105 get '/path', params={}, rack_env={}
106
107 * `/path` is the request path and may optionally include a query string.
108
109 * `params` is a Hash of query/post parameters, a String request body, or
110 `nil`.
111
112 * `rack_env` is a Hash of Rack environment values. This can be used to
113 set request headers and other request related information, such as session
114 data. See the [Rack SPEC][spec] for more information on possible key/values.
115
116 ### Asserting Expectations About The Response
117
118 Once a request method has been invoked, the following attributes are
119 available for making assertions:
120
121 * `app` - The Sinatra application class that handled the mock request.
122
7a7fd4c @tannerburson Cleaning up the testing docs to work with Rack::Test
tannerburson authored
123 * `last_request` - The [`Rack::MockRequest`][Rack::MockRequest] used to generate
11372f2 @rtomayko Finish up test doc for now [#123]
rtomayko authored
124 the request.
125
7a7fd4c @tannerburson Cleaning up the testing docs to work with Rack::Test
tannerburson authored
126 * `last_response` - A [`Rack::MockResponse`][Rack::MockResponse] instance with
11372f2 @rtomayko Finish up test doc for now [#123]
rtomayko authored
127 information on the response generated by the application.
128
7a7fd4c @tannerburson Cleaning up the testing docs to work with Rack::Test
tannerburson authored
129 Assertions are typically made against the `last_response` object.
130 Consider the following examples:
11372f2 @rtomayko Finish up test doc for now [#123]
rtomayko authored
131
132 def test_it_says_hello_world
133 get '/'
7a7fd4c @tannerburson Cleaning up the testing docs to work with Rack::Test
tannerburson authored
134 assert last_response.ok?
3d0eb28 @ai Fix last_response.header to last_response.headers
ai authored
135 assert_equal 'Hello World'.length.to_s, last_response.headers['Content-Length']
7a7fd4c @tannerburson Cleaning up the testing docs to work with Rack::Test
tannerburson authored
136 assert_equal 'Hello World', last_response.body
11372f2 @rtomayko Finish up test doc for now [#123]
rtomayko authored
137 end
138
139 ### Optional Test Setup
140
7a7fd4c @tannerburson Cleaning up the testing docs to work with Rack::Test
tannerburson authored
141 The `Rack::Test` mock request methods send requests to the return value of
142 a method named `app`.
11372f2 @rtomayko Finish up test doc for now [#123]
rtomayko authored
143
144 If you're testing a modular application that has multiple `Sinatra::Base`
7a7fd4c @tannerburson Cleaning up the testing docs to work with Rack::Test
tannerburson authored
145 subclasses, simply set the `app` method to return your particular class.
11372f2 @rtomayko Finish up test doc for now [#123]
rtomayko authored
146
7a7fd4c @tannerburson Cleaning up the testing docs to work with Rack::Test
tannerburson authored
147 def app
4d1d10e @tannerburson Add info about using Rack::Test without the mixin, as well as fix a
tannerburson authored
148 MySinatraApp
11372f2 @rtomayko Finish up test doc for now [#123]
rtomayko authored
149 end
150
86f6eb3 @tannerburson Mention that you have to return Sinatra::Application from def app
tannerburson authored
151 If you're using a classic style Sinatra application, then you need to return an
152 instance of `Sinatra::Application`.
153
154 def app
155 Sinatra::Application
156 end
157
7a7fd4c @tannerburson Cleaning up the testing docs to work with Rack::Test
tannerburson authored
158 ### Making `Rack::Test` available to all test cases
11372f2 @rtomayko Finish up test doc for now [#123]
rtomayko authored
159
7a7fd4c @tannerburson Cleaning up the testing docs to work with Rack::Test
tannerburson authored
160 If you'd like the `Rack::Test` methods to be available to all test cases
161 without having to include it each time, you can include the `Rack::Test`
11372f2 @rtomayko Finish up test doc for now [#123]
rtomayko authored
162 module in the `Test::Unit::TestCase` class:
163
164 require 'test/unit'
7a7fd4c @tannerburson Cleaning up the testing docs to work with Rack::Test
tannerburson authored
165 require 'rack/test'
78cbc0e @dashdotat Bootstrapping of changes to doc for testing
dashdotat authored
166
4043048 @sr Explain how to install and use Sinatra::Test
sr authored
167 class Test::Unit::TestCase
7a7fd4c @tannerburson Cleaning up the testing docs to work with Rack::Test
tannerburson authored
168 include Rack::Test::Methods
78cbc0e @dashdotat Bootstrapping of changes to doc for testing
dashdotat authored
169 end
170
7a7fd4c @tannerburson Cleaning up the testing docs to work with Rack::Test
tannerburson authored
171 Now all `TestCase` subclasses will automatically have `Rack::Test`
11372f2 @rtomayko Finish up test doc for now [#123]
rtomayko authored
172 available to them.
173
174 Test Framework Examples {#frameworks}
175 -----------------------
176
177 As of version `0.9.1`, Sinatra no longer provides testing framework-specific
178 helpers. Those found in `sinatra/test/*.rb` are deprecated and will be
179 removed in Sinatra `1.0`.
78cbc0e @dashdotat Bootstrapping of changes to doc for testing
dashdotat authored
180
4043048 @sr Explain how to install and use Sinatra::Test
sr authored
181 ### [RSpec][]
78cbc0e @dashdotat Bootstrapping of changes to doc for testing
dashdotat authored
182
11372f2 @rtomayko Finish up test doc for now [#123]
rtomayko authored
183 Sinatra can be tested under RSpec using the `spec/interop` library. The
7a7fd4c @tannerburson Cleaning up the testing docs to work with Rack::Test
tannerburson authored
184 `Rack::Test` module should be included within the `describe` block:
4043048 @sr Explain how to install and use Sinatra::Test
sr authored
185
11372f2 @rtomayko Finish up test doc for now [#123]
rtomayko authored
186 require 'hello_world' # <-- your sinatra app
4043048 @sr Explain how to install and use Sinatra::Test
sr authored
187 require 'spec'
188 require 'spec/interop/test'
7a7fd4c @tannerburson Cleaning up the testing docs to work with Rack::Test
tannerburson authored
189 require 'rack/test'
4043048 @sr Explain how to install and use Sinatra::Test
sr authored
190
11372f2 @rtomayko Finish up test doc for now [#123]
rtomayko authored
191 set :environment, :test
4043048 @sr Explain how to install and use Sinatra::Test
sr authored
192
11372f2 @rtomayko Finish up test doc for now [#123]
rtomayko authored
193 describe 'The HelloWorld App' do
7a7fd4c @tannerburson Cleaning up the testing docs to work with Rack::Test
tannerburson authored
194 include Rack::Test::Methods
4043048 @sr Explain how to install and use Sinatra::Test
sr authored
195
11372f2 @rtomayko Finish up test doc for now [#123]
rtomayko authored
196 it "says hello" do
197 get '/'
7a7fd4c @tannerburson Cleaning up the testing docs to work with Rack::Test
tannerburson authored
198 last_response.should be_ok
199 last_response.body.should == 'Hello World'
11372f2 @rtomayko Finish up test doc for now [#123]
rtomayko authored
200 end
201 end
202
7a7fd4c @tannerburson Cleaning up the testing docs to work with Rack::Test
tannerburson authored
203 Make `Rack::Test` available to all spec contexts by including it in
11372f2 @rtomayko Finish up test doc for now [#123]
rtomayko authored
204 `Test::Unit::TestCase`:
205
206 require 'spec'
207 require 'spec/interop/test'
7a7fd4c @tannerburson Cleaning up the testing docs to work with Rack::Test
tannerburson authored
208 require 'rack/test'
78cbc0e @dashdotat Bootstrapping of changes to doc for testing
dashdotat authored
209
7a7fd4c @tannerburson Cleaning up the testing docs to work with Rack::Test
tannerburson authored
210 Test::Unit::TestCase.send :include, Rack::Test::Methods
78cbc0e @dashdotat Bootstrapping of changes to doc for testing
dashdotat authored
211
11372f2 @rtomayko Finish up test doc for now [#123]
rtomayko authored
212 ### [Bacon][]
78cbc0e @dashdotat Bootstrapping of changes to doc for testing
dashdotat authored
213
11372f2 @rtomayko Finish up test doc for now [#123]
rtomayko authored
214 Testing with Bacon is similar to `test/unit` and RSpec:
78cbc0e @dashdotat Bootstrapping of changes to doc for testing
dashdotat authored
215
11372f2 @rtomayko Finish up test doc for now [#123]
rtomayko authored
216 require 'hello_world' # <-- your sinatra app
217 require 'bacon'
7a7fd4c @tannerburson Cleaning up the testing docs to work with Rack::Test
tannerburson authored
218 require 'rack/test'
4043048 @sr Explain how to install and use Sinatra::Test
sr authored
219
11372f2 @rtomayko Finish up test doc for now [#123]
rtomayko authored
220 set :environment, :test
4043048 @sr Explain how to install and use Sinatra::Test
sr authored
221
11372f2 @rtomayko Finish up test doc for now [#123]
rtomayko authored
222 describe 'The HelloWorld App' do
7a7fd4c @tannerburson Cleaning up the testing docs to work with Rack::Test
tannerburson authored
223 include Rack::Test::Methods
4043048 @sr Explain how to install and use Sinatra::Test
sr authored
224
11372f2 @rtomayko Finish up test doc for now [#123]
rtomayko authored
225 it "says hello" do
226 get '/'
7a7fd4c @tannerburson Cleaning up the testing docs to work with Rack::Test
tannerburson authored
227 last_response.should.be.ok
228 last_response.body.should.equal 'Hello World'
11372f2 @rtomayko Finish up test doc for now [#123]
rtomayko authored
229 end
230 end
78cbc0e @dashdotat Bootstrapping of changes to doc for testing
dashdotat authored
231
7a7fd4c @tannerburson Cleaning up the testing docs to work with Rack::Test
tannerburson authored
232 Make `Rack::Test` available to all spec contexts by including it in
11372f2 @rtomayko Finish up test doc for now [#123]
rtomayko authored
233 `Bacon::Context`:
78cbc0e @dashdotat Bootstrapping of changes to doc for testing
dashdotat authored
234
11372f2 @rtomayko Finish up test doc for now [#123]
rtomayko authored
235 class Bacon::Context
7a7fd4c @tannerburson Cleaning up the testing docs to work with Rack::Test
tannerburson authored
236 include Rack::Test::Methods
4043048 @sr Explain how to install and use Sinatra::Test
sr authored
237 end
238
11372f2 @rtomayko Finish up test doc for now [#123]
rtomayko authored
239 ### [Test::Spec][]
78cbc0e @dashdotat Bootstrapping of changes to doc for testing
dashdotat authored
240
7a7fd4c @tannerburson Cleaning up the testing docs to work with Rack::Test
tannerburson authored
241 The `Rack::Test` module should be included within the context of the
11372f2 @rtomayko Finish up test doc for now [#123]
rtomayko authored
242 `describe` block:
243
244 require 'hello_world' # <-- your sinatra app
245 require 'test/spec'
7a7fd4c @tannerburson Cleaning up the testing docs to work with Rack::Test
tannerburson authored
246 require 'rack/test'
4043048 @sr Explain how to install and use Sinatra::Test
sr authored
247
11372f2 @rtomayko Finish up test doc for now [#123]
rtomayko authored
248 set :environment, :test
4043048 @sr Explain how to install and use Sinatra::Test
sr authored
249
11372f2 @rtomayko Finish up test doc for now [#123]
rtomayko authored
250 describe 'The HelloWorld App' do
7a7fd4c @tannerburson Cleaning up the testing docs to work with Rack::Test
tannerburson authored
251 include Rack::Test::Methods
11372f2 @rtomayko Finish up test doc for now [#123]
rtomayko authored
252
253 it "says hello" do
254 get '/'
7a7fd4c @tannerburson Cleaning up the testing docs to work with Rack::Test
tannerburson authored
255 last_response.should.be.ok
256 last_response.body.should.equal 'Hello World'
11372f2 @rtomayko Finish up test doc for now [#123]
rtomayko authored
257 end
78cbc0e @dashdotat Bootstrapping of changes to doc for testing
dashdotat authored
258 end
259
7a7fd4c @tannerburson Cleaning up the testing docs to work with Rack::Test
tannerburson authored
260 Make `Rack::Test` available to all spec contexts by including it in
11372f2 @rtomayko Finish up test doc for now [#123]
rtomayko authored
261 `Test::Unit::TestCase`:
78cbc0e @dashdotat Bootstrapping of changes to doc for testing
dashdotat authored
262
11372f2 @rtomayko Finish up test doc for now [#123]
rtomayko authored
263 require 'test/spec'
7a7fd4c @tannerburson Cleaning up the testing docs to work with Rack::Test
tannerburson authored
264 require 'rack/test'
4043048 @sr Explain how to install and use Sinatra::Test
sr authored
265
7a7fd4c @tannerburson Cleaning up the testing docs to work with Rack::Test
tannerburson authored
266 Test::Unit::TestCase.send :include, Rack::Test::Methods
11372f2 @rtomayko Finish up test doc for now [#123]
rtomayko authored
267
268 <!-- TODO: Webrat -->
78cbc0e @dashdotat Bootstrapping of changes to doc for testing
dashdotat authored
269
11372f2 @rtomayko Finish up test doc for now [#123]
rtomayko authored
270 See Also
271 --------
4043048 @sr Explain how to install and use Sinatra::Test
sr authored
272
7a7fd4c @tannerburson Cleaning up the testing docs to work with Rack::Test
tannerburson authored
273 See the source for [Rack::Test][] for
11372f2 @rtomayko Finish up test doc for now [#123]
rtomayko authored
274 more information on `get`, `post`, `put`, `delete` and friends.
78cbc0e @dashdotat Bootstrapping of changes to doc for testing
dashdotat authored
275
4043048 @sr Explain how to install and use Sinatra::Test
sr authored
276 [Test::Unit]: http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit.html
277 [RSpec]: http://rspec.info
278 [Bacon]: http://github.com/chneukirchen/bacon
279 [Test::Spec]: http://rubyforge.org/projects/test-spec/
d3daf22 @sr Looks like one can't link to HEAD blob anymore
sr authored
280 [Sinatra::Test]: http://github.com/sinatra/sinatra/blob/987d622a52f3eaf28f8f6beb7451c3e43c23f075/test/test_test.rb
7a7fd4c @tannerburson Cleaning up the testing docs to work with Rack::Test
tannerburson authored
281 [Rack::Test]: http://github.com/brynary/rack-test/
a0578cf @sr Actually, we can. Thanks raggi :-)
sr authored
282 [test]: http://github.com/sinatra/sinatra/blob/master/test/test_test.rb
4043048 @sr Explain how to install and use Sinatra::Test
sr authored
283 [wild]: /wild.html
11372f2 @rtomayko Finish up test doc for now [#123]
rtomayko authored
284 [spec]: http://rack.rubyforge.org/doc/files/SPEC.html
285 [Rack::MockRequest]: http://rack.rubyforge.org/doc/classes/Rack/MockRequest.html
286 [Rack::MockResponse]: http://rack.rubyforge.org/doc/classes/Rack/MockResponse.html
Something went wrong with that request. Please try again.