Skip to content

Instantly share code, notes, and snippets.

@groesser3
Forked from avdi/sinatra-tests-baked-in.rb
Created February 20, 2011 08:22
Show Gist options
  • Save groesser3/835821 to your computer and use it in GitHub Desktop.
Save groesser3/835821 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require 'rubygems'
gem 'rack', '=0.9.1'
gem 'thin', '=1.0.0'
require 'sinatra'
get '/' do
content_type 'text/plain'
"Hello, world"
end
# Run me with 'spec' executable to run my specs!
if $0 =~ /spec$/
set :environment, :test
set :run, false # Don't autostart server
require 'spec/interop/test'
require 'sinatra/test'
describe "Example App" do
include Sinatra::Test
it "should serve a greeting" do
get '/'
response.should be_ok
response.body.should == "Hello, world"
end
it "should serve content as text/plain" do
get '/'
response.headers['Content-Type'].should == 'text/plain'
end
end
end
__END__
$ spec -fs sinatra-tests-baked-in.rb
Example App
- should serve a greeting
- should serve content as text/plain
Finished in 0.007221 seconds
2 examples, 0 failures
Otherwise, if we call it as a Ruby program, it runs the Sinatra server as we would expect:
$ ruby sinatra-tests-baked-in.rb
== Sinatra/0.9.1.1 has taken the stage on 4567 for development with backup from Thin
>> Thin web server (v1.0.0 codename That's What She Said)
>> Maximum connections set to 1024
>> Listening on 0.0.0.0:4567, CTRL+C to stop
And there you have it: a true single-file application, specs and all.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment