title | layout |
---|---|
Testing with Sinatra |
default |
The Sinatra::Test
module includes a variety of helper methods
to test your app.
As of version 0.9.1
, Sinatra does not provides any testing
framework-specific helpers anymore. Those found in sinatra/test/*.rb
are deprecated and will be removed in Sinatra 1.0
.
However, this document explains how to install Sinatra's test helpers intro a variety of testing frameworks.
Test::Unit and Test::Spec
require 'test/unit' # or test/spec
require 'sinatra/test'
Sinatra::Default.set :environment, :test
require 'app'
class Test::Unit::TestCase
include Sinatra::Test
end
Only the stuff that needs to be required change:
require 'spec'
require 'spec/interop/test'
require 'sinatra/test'
...
require 'bacon'
require 'sinatra/test'
Sinatra::Default.set :environment, :test
require 'app'
class Bacon::Context
include Sinatra::Test
end
To be as general as possible, these examples assume Test::Unit
is being used.
NOTE: There are plenty of apps in the wild that are using other testing frameworks.
require 'sinatra'
get '/' do
"Hello #{params[:name]}"
end
require 'test/unit'
require 'sinatra/test'
Sinatra::Default.set :environment, :test
require 'app'
class Test::Unit::TestCase
include Sinatra::Test
end
require File.dirname(__FILE__) + '/test_helper'
class MyAppTest < Test::Unit::TestCase
get test_it_says_hello
get '/', :name => 'Ryan "Middleware" Tomayko'
assert body.include?("Middleware")
end
end
See Sinatra::Test and the accompagning tests for more information
on get
, post
, delete
and friends.