The first step is to add the rspec gem to your
Gemfile and run bundle
source "http://rubygems.org" gem 'sinatra' group :test do gem 'rspec' endNext you need to create the
spec directory and add the spec_helper.rb file
require File.join(File.dirname(__FILE__), '..', 'app.rb') require 'sinatra' require 'rack/test' # setup test environment set :environment, :test set :run, false set :raise_errors, true set :logging, false def app Sinatra::Application end RSpec.configure do |config| config.include Rack::Test::Methods endThis is for a classic style sinatra app, if you are using a modular app, you need to make some slight modifications for it to work. I'll show how to do that in a later post.
And finally you are ready to create the spec file that corresponds to the
app.rb
require 'spec_helper'
describe "Sinatra App" do
it "should respond to GET" do
get '/'
last_response.should be_ok
last_response.body.should match(/It works!/)
end
end
I usually don't use rspec for integration tests, for that I use cucumber. I normally use rspec for unit testing, but since this is a sample app and I don't have any models to test, I included this test for completeness sake. I'll write another blog post how to add cucumber tests to this sample sinatra app later.To make life easier, I've added a rake task to run the rspec tests
require 'rspec/core/rake_task' desc "run specs" RSpec::Core::RakeTask.newAnd then I've updated the deployment task to require the rspec task, so we can't update the application unless it passes all tests.
namespace :vmc do
desc "update cloud foundry deployment"
task :update => [:bundle, :spec] do
sh "vmc update #{VMC_APP_NAME}"
end
end
I've created a github repository for this app to make it easier for you to use it as a template.