Skip to content

HTTPS clone URL

Subversion checkout URL

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