forked from sinatra/sinatra.github.com
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sinatra-runner.html
51 lines (34 loc) · 2.31 KB
/
sinatra-runner.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<p>NOTE: This feature is experimental, and missing tests!</p>
<p>Helps you spinning up and shutting down your own sinatra app. This is
especially helpful for running real network tests against a sinatra
backend.</p>
<p>The backend server could look like the following (in test/server.rb).</p>
<pre class="ruby"><span class="ruby-identifier">require</span> <span class="ruby-string">"sinatra"</span>
<span class="ruby-identifier">get</span> <span class="ruby-string">"/"</span> <span class="ruby-keyword">do</span>
<span class="ruby-string">"Cheers from test server"</span>
<span class="ruby-keyword">end</span>
<span class="ruby-identifier">get</span> <span class="ruby-string">"/ping"</span> <span class="ruby-keyword">do</span>
<span class="ruby-string">"1"</span>
<span class="ruby-keyword">end</span>
</pre>
<p>Note that you need to implement a ping action for internal use.</p>
<p>Next, you need to write your runner.</p>
<pre class="ruby"><span class="ruby-identifier">require</span> <span class="ruby-string">'sinatra/runner'</span>
<span class="ruby-keyword">class</span> <span class="ruby-constant">Runner</span> <span class="ruby-operator"><</span> <span class="ruby-constant">Sinatra</span><span class="ruby-operator">::</span><span class="ruby-constant">Runner</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier">app_file</span>
<span class="ruby-constant">File</span>.<span class="ruby-identifier">expand_path</span>(<span class="ruby-string">"../server.rb"</span>, <span class="ruby-keyword">__FILE__</span>)
<span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span>
</pre>
<p>Override Runner#app_file, #command, #port, #protocol and #ping_path for
customization.</p>
<p>**Don't forget to override #app_file specific to your application!**</p>
<p>Wherever you need this test backend, here's how you manage it. The
following example assumes you have a test in your app that needs to be run
against your test backend.</p>
<pre>runner = ServerRunner.new
runner.run
# ..tests against localhost:4567 here..
runner.kill</pre>
<p>For an example, check <a
href="https://github.com/apotonick/roar/blob/master/test/integration/runner.rb">github.com/apotonick/roar/blob/master/test/integration/runner.rb</a></p>