Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 110 lines (74 sloc) 2.41 kb
78cbc0e @dashdotat Bootstrapping of changes to doc for testing
dashdotat authored
1 ---
4043048 @sr Explain how to install and use Sinatra::Test
sr authored
2 title: Testing with Sinatra
78cbc0e @dashdotat Bootstrapping of changes to doc for testing
dashdotat authored
3 layout: default
4 ---
5
4043048 @sr Explain how to install and use Sinatra::Test
sr authored
6 Testing with Sinatra
7 ====================
78cbc0e @dashdotat Bootstrapping of changes to doc for testing
dashdotat authored
8
4043048 @sr Explain how to install and use Sinatra::Test
sr authored
9 The `Sinatra::Test` module includes a variety of helper methods
10 to test your app.
78cbc0e @dashdotat Bootstrapping of changes to doc for testing
dashdotat authored
11
4043048 @sr Explain how to install and use Sinatra::Test
sr authored
12 As of version `0.9.1`, Sinatra does not provides any testing
13 framework-specific helpers anymore. Those found in `sinatra/test/*.rb`
14 are deprecated and will be removed in Sinatra `1.0`.
78cbc0e @dashdotat Bootstrapping of changes to doc for testing
dashdotat authored
15
4043048 @sr Explain how to install and use Sinatra::Test
sr authored
16 However, this document explains how to install Sinatra's test helpers
17 intro a variety of testing frameworks.
78cbc0e @dashdotat Bootstrapping of changes to doc for testing
dashdotat authored
18
4043048 @sr Explain how to install and use Sinatra::Test
sr authored
19 Install it
20 ----------
21
22 ### [Test::Unit][] and [Test::Spec][]
23
24 require 'test/unit' # or test/spec
25 require 'sinatra/test'
78cbc0e @dashdotat Bootstrapping of changes to doc for testing
dashdotat authored
26
4043048 @sr Explain how to install and use Sinatra::Test
sr authored
27 Sinatra::Default.set :environment, :test
1878b99 @foca requiRe instead of requie
foca authored
28 require 'app'
78cbc0e @dashdotat Bootstrapping of changes to doc for testing
dashdotat authored
29
4043048 @sr Explain how to install and use Sinatra::Test
sr authored
30 class Test::Unit::TestCase
31 include Sinatra::Test
78cbc0e @dashdotat Bootstrapping of changes to doc for testing
dashdotat authored
32 end
33
34
4043048 @sr Explain how to install and use Sinatra::Test
sr authored
35 ### [RSpec][]
78cbc0e @dashdotat Bootstrapping of changes to doc for testing
dashdotat authored
36
4043048 @sr Explain how to install and use Sinatra::Test
sr authored
37 Only the stuff that needs to be required change:
38
39 require 'spec'
40 require 'spec/interop/test'
41 require 'sinatra/test'
42
43 ...
44
45 ### [Bacon][]
46
47 require 'bacon'
48 require 'sinatra/test'
78cbc0e @dashdotat Bootstrapping of changes to doc for testing
dashdotat authored
49
4043048 @sr Explain how to install and use Sinatra::Test
sr authored
50 Sinatra::Default.set :environment, :test
1878b99 @foca requiRe instead of requie
foca authored
51 require 'app'
78cbc0e @dashdotat Bootstrapping of changes to doc for testing
dashdotat authored
52
4043048 @sr Explain how to install and use Sinatra::Test
sr authored
53 class Bacon::Context
54 include Sinatra::Test
78cbc0e @dashdotat Bootstrapping of changes to doc for testing
dashdotat authored
55 end
56
4043048 @sr Explain how to install and use Sinatra::Test
sr authored
57 <!-- TODO: Webrat -->
78cbc0e @dashdotat Bootstrapping of changes to doc for testing
dashdotat authored
58
4043048 @sr Explain how to install and use Sinatra::Test
sr authored
59 Use it
60 ------
61
62 To be as general as possible, these examples assume `Test::Unit` is being used.
63
64 *NOTE:* There are plenty of apps [in the wild][wild] that are using other
65 testing frameworks.
66
67 ## app.rb
78cbc0e @dashdotat Bootstrapping of changes to doc for testing
dashdotat authored
68
69 require 'sinatra'
70
4043048 @sr Explain how to install and use Sinatra::Test
sr authored
71 get '/' do
72 "Hello #{params[:name]}"
73 end
74
75 ## test/test\_helpers.rb
78cbc0e @dashdotat Bootstrapping of changes to doc for testing
dashdotat authored
76
4043048 @sr Explain how to install and use Sinatra::Test
sr authored
77 require 'test/unit'
78 require 'sinatra/test'
79
80 Sinatra::Default.set :environment, :test
1878b99 @foca requiRe instead of requie
foca authored
81 require 'app'
4043048 @sr Explain how to install and use Sinatra::Test
sr authored
82
83 class Test::Unit::TestCase
84 include Sinatra::Test
78cbc0e @dashdotat Bootstrapping of changes to doc for testing
dashdotat authored
85 end
86
4043048 @sr Explain how to install and use Sinatra::Test
sr authored
87 ## test/app\_test.rb
78cbc0e @dashdotat Bootstrapping of changes to doc for testing
dashdotat authored
88
4043048 @sr Explain how to install and use Sinatra::Test
sr authored
89 require File.dirname(__FILE__) + '/test_helper'
90
91 class MyAppTest < Test::Unit::TestCase
92 get test_it_says_hello
93 get '/', :name => 'Ryan "Middleware" Tomayko'
94 assert body.include?("Middleware")
78cbc0e @dashdotat Bootstrapping of changes to doc for testing
dashdotat authored
95 end
96 end
97
4043048 @sr Explain how to install and use Sinatra::Test
sr authored
98 <!-- TODO: document usage of get, post etc -->
99
100 See [Sinatra::Test][] and the [accompagning tests][test] for more information
101 on `get`, `post`, `delete` and friends.
78cbc0e @dashdotat Bootstrapping of changes to doc for testing
dashdotat authored
102
4043048 @sr Explain how to install and use Sinatra::Test
sr authored
103 [Test::Unit]: http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit.html
104 [RSpec]: http://rspec.info
105 [Bacon]: http://github.com/chneukirchen/bacon
106 [Test::Spec]: http://rubyforge.org/projects/test-spec/
107 [Sinatra::Test]: http://github.com/sinatra/sinatra/blob/HEAD/lib/sinatra/test.rb
108 [test]: http://github.com/sinatra/sinatra/blob/HEAD/test/test_test.rb
109 [wild]: /wild.html
Something went wrong with that request. Please try again.