Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 300 lines (215 sloc) 8.45 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`
175492c @sr testing: update for latest rack/test
sr authored
70 into your own classes. `Rack::Test` supports this style of testing as well,
71 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
72
73
74 require 'hello_world'
75 require 'test/unit'
76 require 'rack/test'
77
78 set :environment, :test
79
80 class HelloWorldTest < Test::Unit::TestCase
81
82 def test_it_says_hello_world
175492c @sr testing: update for latest rack/test
sr authored
83 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
84 browser.get '/'
85 assert browser.last_response.ok?
86 assert_equal 'Hello World', browser.last_response.body
87 end
88
89 def test_it_says_hello_to_a_person
175492c @sr testing: update for latest rack/test
sr authored
90 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
91 browser.get '/', :name => 'Simon'
92 assert browser.last_response.body.include?('Simon')
93 end
94 end
95
96
7a7fd4c @tannerburson Cleaning up the testing docs to work with Rack::Test
tannerburson authored
97 ### Rack::Test's Mock Request Methods
11372f2 @rtomayko Finish up test doc for now [#123]
rtomayko authored
98
99 The `get`, `put`, `post`, `delete`, and `head` methods simulate the
100 respective type of request on the application. Tests typically begin with
101 a call to one of these methods followed by one or more assertions against
102 the resulting response.
103
104 All mock request methods have the same argument signature:
105
106 get '/path', params={}, rack_env={}
107
108 * `/path` is the request path and may optionally include a query string.
109
110 * `params` is a Hash of query/post parameters, a String request body, or
111 `nil`.
112
113 * `rack_env` is a Hash of Rack environment values. This can be used to
114 set request headers and other request related information, such as session
115 data. See the [Rack SPEC][spec] for more information on possible key/values.
116
117 ### Asserting Expectations About The Response
118
119 Once a request method has been invoked, the following attributes are
120 available for making assertions:
121
122 * `app` - The Sinatra application class that handled the mock request.
123
7a7fd4c @tannerburson Cleaning up the testing docs to work with Rack::Test
tannerburson authored
124 * `last_request` - The [`Rack::MockRequest`][Rack::MockRequest] used to generate
11372f2 @rtomayko Finish up test doc for now [#123]
rtomayko authored
125 the request.
126
7a7fd4c @tannerburson Cleaning up the testing docs to work with Rack::Test
tannerburson authored
127 * `last_response` - A [`Rack::MockResponse`][Rack::MockResponse] instance with
11372f2 @rtomayko Finish up test doc for now [#123]
rtomayko authored
128 information on the response generated by the application.
129
7a7fd4c @tannerburson Cleaning up the testing docs to work with Rack::Test
tannerburson authored
130 Assertions are typically made against the `last_response` object.
131 Consider the following examples:
11372f2 @rtomayko Finish up test doc for now [#123]
rtomayko authored
132
133 def test_it_says_hello_world
134 get '/'
7a7fd4c @tannerburson Cleaning up the testing docs to work with Rack::Test
tannerburson authored
135 assert last_response.ok?
3d0eb28 @ai Fix last_response.header to last_response.headers
ai authored
136 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
137 assert_equal 'Hello World', last_response.body
11372f2 @rtomayko Finish up test doc for now [#123]
rtomayko authored
138 end
139
140 ### Optional Test Setup
141
7a7fd4c @tannerburson Cleaning up the testing docs to work with Rack::Test
tannerburson authored
142 The `Rack::Test` mock request methods send requests to the return value of
143 a method named `app`.
11372f2 @rtomayko Finish up test doc for now [#123]
rtomayko authored
144
145 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
146 subclasses, simply set the `app` method to return your particular class.
11372f2 @rtomayko Finish up test doc for now [#123]
rtomayko authored
147
7a7fd4c @tannerburson Cleaning up the testing docs to work with Rack::Test
tannerburson authored
148 def app
4d1d10e @tannerburson Add info about using Rack::Test without the mixin, as well as fix a
tannerburson authored
149 MySinatraApp
11372f2 @rtomayko Finish up test doc for now [#123]
rtomayko authored
150 end
151
86f6eb3 @tannerburson Mention that you have to return Sinatra::Application from def app
tannerburson authored
152 If you're using a classic style Sinatra application, then you need to return an
153 instance of `Sinatra::Application`.
154
155 def app
156 Sinatra::Application
157 end
158
7a7fd4c @tannerburson Cleaning up the testing docs to work with Rack::Test
tannerburson authored
159 ### Making `Rack::Test` available to all test cases
11372f2 @rtomayko Finish up test doc for now [#123]
rtomayko authored
160
7a7fd4c @tannerburson Cleaning up the testing docs to work with Rack::Test
tannerburson authored
161 If you'd like the `Rack::Test` methods to be available to all test cases
162 without having to include it each time, you can include the `Rack::Test`
11372f2 @rtomayko Finish up test doc for now [#123]
rtomayko authored
163 module in the `Test::Unit::TestCase` class:
164
165 require 'test/unit'
7a7fd4c @tannerburson Cleaning up the testing docs to work with Rack::Test
tannerburson authored
166 require 'rack/test'
78cbc0e @dashdotat Bootstrapping of changes to doc for testing
dashdotat authored
167
4043048 @sr Explain how to install and use Sinatra::Test
sr authored
168 class Test::Unit::TestCase
7a7fd4c @tannerburson Cleaning up the testing docs to work with Rack::Test
tannerburson authored
169 include Rack::Test::Methods
78cbc0e @dashdotat Bootstrapping of changes to doc for testing
dashdotat authored
170 end
171
7a7fd4c @tannerburson Cleaning up the testing docs to work with Rack::Test
tannerburson authored
172 Now all `TestCase` subclasses will automatically have `Rack::Test`
11372f2 @rtomayko Finish up test doc for now [#123]
rtomayko authored
173 available to them.
174
175 Test Framework Examples {#frameworks}
176 -----------------------
177
178 As of version `0.9.1`, Sinatra no longer provides testing framework-specific
179 helpers. Those found in `sinatra/test/*.rb` are deprecated and will be
180 removed in Sinatra `1.0`.
78cbc0e @dashdotat Bootstrapping of changes to doc for testing
dashdotat authored
181
4043048 @sr Explain how to install and use Sinatra::Test
sr authored
182 ### [RSpec][]
78cbc0e @dashdotat Bootstrapping of changes to doc for testing
dashdotat authored
183
6c6b287 @baldowl Tweaks to doc about using Rack::Test with RSpec.
baldowl authored
184 Sinatra can be tested under plain RSpec. The `Rack::Test` module should be
185 included within the `describe` block:
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 require 'hello_world' # <-- your sinatra app
4043048 @sr Explain how to install and use Sinatra::Test
sr authored
188 require 'spec'
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
6c6b287 @baldowl Tweaks to doc about using Rack::Test with RSpec.
baldowl authored
196 def app
197 Sinatra::Application
198 end
199
11372f2 @rtomayko Finish up test doc for now [#123]
rtomayko authored
200 it "says hello" do
201 get '/'
7a7fd4c @tannerburson Cleaning up the testing docs to work with Rack::Test
tannerburson authored
202 last_response.should be_ok
203 last_response.body.should == 'Hello World'
11372f2 @rtomayko Finish up test doc for now [#123]
rtomayko authored
204 end
205 end
206
6c6b287 @baldowl Tweaks to doc about using Rack::Test with RSpec.
baldowl authored
207 Make `Rack::Test` available to all spec contexts by including it via
208 `Spec::Runner`:
11372f2 @rtomayko Finish up test doc for now [#123]
rtomayko authored
209
210 require 'spec'
7a7fd4c @tannerburson Cleaning up the testing docs to work with Rack::Test
tannerburson authored
211 require 'rack/test'
78cbc0e @dashdotat Bootstrapping of changes to doc for testing
dashdotat authored
212
6c6b287 @baldowl Tweaks to doc about using Rack::Test with RSpec.
baldowl authored
213 Spec::Runner.configure do |conf|
214 conf.include Rack::Test::Methods
215 end
78cbc0e @dashdotat Bootstrapping of changes to doc for testing
dashdotat authored
216
11372f2 @rtomayko Finish up test doc for now [#123]
rtomayko authored
217 ### [Bacon][]
78cbc0e @dashdotat Bootstrapping of changes to doc for testing
dashdotat authored
218
11372f2 @rtomayko Finish up test doc for now [#123]
rtomayko authored
219 Testing with Bacon is similar to `test/unit` and RSpec:
78cbc0e @dashdotat Bootstrapping of changes to doc for testing
dashdotat authored
220
11372f2 @rtomayko Finish up test doc for now [#123]
rtomayko authored
221 require 'hello_world' # <-- your sinatra app
222 require 'bacon'
7a7fd4c @tannerburson Cleaning up the testing docs to work with Rack::Test
tannerburson authored
223 require 'rack/test'
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 set :environment, :test
4043048 @sr Explain how to install and use Sinatra::Test
sr authored
226
11372f2 @rtomayko Finish up test doc for now [#123]
rtomayko authored
227 describe 'The HelloWorld App' do
7a7fd4c @tannerburson Cleaning up the testing docs to work with Rack::Test
tannerburson authored
228 include Rack::Test::Methods
4043048 @sr Explain how to install and use Sinatra::Test
sr authored
229
ff35bfd @sr testing: fix beacon and test/spec examples
sr authored
230 def app
231 Sinatra::Application
232 end
233
11372f2 @rtomayko Finish up test doc for now [#123]
rtomayko authored
234 it "says hello" do
235 get '/'
7a7fd4c @tannerburson Cleaning up the testing docs to work with Rack::Test
tannerburson authored
236 last_response.should.be.ok
237 last_response.body.should.equal 'Hello World'
11372f2 @rtomayko Finish up test doc for now [#123]
rtomayko authored
238 end
239 end
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 Make `Rack::Test` available to all spec contexts by including it in
11372f2 @rtomayko Finish up test doc for now [#123]
rtomayko authored
242 `Bacon::Context`:
78cbc0e @dashdotat Bootstrapping of changes to doc for testing
dashdotat authored
243
11372f2 @rtomayko Finish up test doc for now [#123]
rtomayko authored
244 class Bacon::Context
7a7fd4c @tannerburson Cleaning up the testing docs to work with Rack::Test
tannerburson authored
245 include Rack::Test::Methods
4043048 @sr Explain how to install and use Sinatra::Test
sr authored
246 end
247
11372f2 @rtomayko Finish up test doc for now [#123]
rtomayko authored
248 ### [Test::Spec][]
78cbc0e @dashdotat Bootstrapping of changes to doc for testing
dashdotat authored
249
7a7fd4c @tannerburson Cleaning up the testing docs to work with Rack::Test
tannerburson authored
250 The `Rack::Test` module should be included within the context of the
11372f2 @rtomayko Finish up test doc for now [#123]
rtomayko authored
251 `describe` block:
252
253 require 'hello_world' # <-- your sinatra app
254 require 'test/spec'
7a7fd4c @tannerburson Cleaning up the testing docs to work with Rack::Test
tannerburson authored
255 require 'rack/test'
4043048 @sr Explain how to install and use Sinatra::Test
sr authored
256
11372f2 @rtomayko Finish up test doc for now [#123]
rtomayko authored
257 set :environment, :test
4043048 @sr Explain how to install and use Sinatra::Test
sr authored
258
11372f2 @rtomayko Finish up test doc for now [#123]
rtomayko authored
259 describe 'The HelloWorld App' do
7a7fd4c @tannerburson Cleaning up the testing docs to work with Rack::Test
tannerburson authored
260 include Rack::Test::Methods
11372f2 @rtomayko Finish up test doc for now [#123]
rtomayko authored
261
ff35bfd @sr testing: fix beacon and test/spec examples
sr authored
262 def app
263 Sinatra::Application
264 end
265
11372f2 @rtomayko Finish up test doc for now [#123]
rtomayko authored
266 it "says hello" do
267 get '/'
7a7fd4c @tannerburson Cleaning up the testing docs to work with Rack::Test
tannerburson authored
268 last_response.should.be.ok
269 last_response.body.should.equal 'Hello World'
11372f2 @rtomayko Finish up test doc for now [#123]
rtomayko authored
270 end
78cbc0e @dashdotat Bootstrapping of changes to doc for testing
dashdotat authored
271 end
272
7a7fd4c @tannerburson Cleaning up the testing docs to work with Rack::Test
tannerburson authored
273 Make `Rack::Test` available to all spec contexts by including it in
11372f2 @rtomayko Finish up test doc for now [#123]
rtomayko authored
274 `Test::Unit::TestCase`:
78cbc0e @dashdotat Bootstrapping of changes to doc for testing
dashdotat authored
275
11372f2 @rtomayko Finish up test doc for now [#123]
rtomayko authored
276 require 'test/spec'
7a7fd4c @tannerburson Cleaning up the testing docs to work with Rack::Test
tannerburson authored
277 require 'rack/test'
4043048 @sr Explain how to install and use Sinatra::Test
sr authored
278
7a7fd4c @tannerburson Cleaning up the testing docs to work with Rack::Test
tannerburson authored
279 Test::Unit::TestCase.send :include, Rack::Test::Methods
11372f2 @rtomayko Finish up test doc for now [#123]
rtomayko authored
280
281 <!-- TODO: Webrat -->
78cbc0e @dashdotat Bootstrapping of changes to doc for testing
dashdotat authored
282
11372f2 @rtomayko Finish up test doc for now [#123]
rtomayko authored
283 See Also
284 --------
4043048 @sr Explain how to install and use Sinatra::Test
sr authored
285
7a7fd4c @tannerburson Cleaning up the testing docs to work with Rack::Test
tannerburson authored
286 See the source for [Rack::Test][] for
11372f2 @rtomayko Finish up test doc for now [#123]
rtomayko authored
287 more information on `get`, `post`, `put`, `delete` and friends.
78cbc0e @dashdotat Bootstrapping of changes to doc for testing
dashdotat authored
288
4043048 @sr Explain how to install and use Sinatra::Test
sr authored
289 [Test::Unit]: http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit.html
290 [RSpec]: http://rspec.info
291 [Bacon]: http://github.com/chneukirchen/bacon
292 [Test::Spec]: http://rubyforge.org/projects/test-spec/
d3daf22 @sr Looks like one can't link to HEAD blob anymore
sr authored
293 [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
294 [Rack::Test]: http://github.com/brynary/rack-test/
a0578cf @sr Actually, we can. Thanks raggi :-)
sr authored
295 [test]: http://github.com/sinatra/sinatra/blob/master/test/test_test.rb
4043048 @sr Explain how to install and use Sinatra::Test
sr authored
296 [wild]: /wild.html
11372f2 @rtomayko Finish up test doc for now [#123]
rtomayko authored
297 [spec]: http://rack.rubyforge.org/doc/files/SPEC.html
298 [Rack::MockRequest]: http://rack.rubyforge.org/doc/classes/Rack/MockRequest.html
299 [Rack::MockResponse]: http://rack.rubyforge.org/doc/classes/Rack/MockResponse.html
Something went wrong with that request. Please try again.