Sinatra Basics : Using RSpec as the Test Framework
To learn how to use RSpec testing framework to test the Sinatra application.
Create Gemfile in the root of the project:
source 'https://rubygems.org' gem 'shotgun' gem 'sinatra' gem "rack-test", require: "rack/test" gem "rspec"
Install the gems:
This installed RSpec version 3.2 on my system.
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 Sinatra::Application end end RSpec.configure do |c| c.include RSpecMixin end
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') end end
Create sample.rb in the project folder:
require 'sinatra' get '/' do 'hi' end
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.
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