Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@myronmarston
Created April 5, 2012 20:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save myronmarston/2313727 to your computer and use it in GitHub Desktop.
Save myronmarston/2313727 to your computer and use it in GitHub Desktop.
require 'sinatra/base'
require 'json'
class MyApp < Sinatra::Base
before { content_type "application/json;charset=utf-8" }
not_found { JSON.dump("error" => "Not Found") }
end
source 'https://rubygems.org'
gem 'rack-test'
gem 'sinatra'
GEM
remote: https://rubygems.org/
specs:
rack (1.4.1)
rack-protection (1.2.0)
rack
rack-test (0.6.1)
rack (>= 1.0)
sinatra (1.3.2)
rack (~> 1.3, >= 1.3.6)
rack-protection (~> 1.2)
tilt (~> 1.3, >= 1.3.3)
tilt (1.3.3)
PLATFORMS
ruby
DEPENDENCIES
rack-test
sinatra
➜ sinatra_example RACK_ENV=test ruby test.rb
Run options:
# Running tests:
.
Finished tests in 0.019838s, 50.4083 tests/s, 100.8166 assertions/s.
1 tests, 2 assertions, 0 failures, 0 errors, 0 skips
➜ sinatra_example ruby test.rb
Run options:
# Running tests:
F
Finished tests in 0.022823s, 43.8154 tests/s, 87.6309 assertions/s.
1) Failure:
test_not_found(MyAppTest) [test.rb:15]:
<"application/json;charset=utf-8"> expected but was
<"text/html;charset=utf-8">.
1 tests, 2 assertions, 1 failures, 0 errors, 0 skips
require 'test/unit'
require 'rack/test'
require_relative 'app'
class MyAppTest < Test::Unit::TestCase
include Rack::Test::Methods
def app
MyApp.new
end
def test_not_found
get '/not_found'
assert_equal 404, last_response.status
assert_equal 'application/json;charset=utf-8', last_response.headers['Content-Type']
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment