Skip to content

Latest commit

 

History

History
109 lines (74 loc) · 2.35 KB

testing.markdown

File metadata and controls

109 lines (74 loc) · 2.35 KB
title layout
Testing with Sinatra
default

Testing with Sinatra

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.

Install it

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

Use it

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.

app.rb

require 'sinatra'

get '/' do
  "Hello #{params[:name]}"
end

test/test_helpers.rb

require 'test/unit'
require 'sinatra/test'

Sinatra::Default.set :environment, :test
require 'app'

class Test::Unit::TestCase
  include Sinatra::Test
end

test/app_test.rb

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.