Sinatra Basics : Using RSpec as the Test Framework


To learn how to use RSpec testing framework to test the Sinatra application.


Step 1

Create Gemfile in the root of the project:

source ''

gem 'shotgun'
gem 'sinatra'
gem "rack-test", require: "rack/test"
gem "rspec"

Step 2

Install the gems:

bundle install

This installed RSpec version 3.2 on my system.

Step 3

Create spec/spec_helper.rb with the following code:

require 'rack/test'
require 'rspec'

ENV['RACK_ENV'] = 'test'

module RSpecMixin
  include Rack::Test::Methods

  def app

RSpec.configure do |c| 
  c.include RSpecMixin 

Step 4

In spec/sample_spec.rb add the test:

require_relative '../spec_helper.rb'

RSpec.describe 'Specing a Sinatra Application' do

  it 'should greet' do
    get '/' 

    expect(last_response).to be_ok
    expect(last_response.body).to eq('hi')

Step 5

Create sample.rb in the project folder:

require 'sinatra'

get '/' do

Step 6

Run the test:

$bundle exec rspec spec/sample_spec.rb --color


In this article, you learned how to use RSpec to test the Sinatra application.

Related Articles

Ace the Technical Interview

  • Easily find the gaps in your knowledge
  • Get customized lessons based on where you are
  • Take consistent action everyday
  • Builtin accountability to keep you on track
  • You will solve bigger problems over time
  • Get the job of your dreams

Take the 30 Day Coding Skills Challenge

Gain confidence to attend the interview

No spam ever. Unsubscribe anytime.