A Sinatra-Sequel-RSpec Template

4 Comments

I’m just getting to the fun part of writing an app, the part where I’m actually writing the app and not just trying to get my environment setup. A couple days ago, I would’ve loved to have found a post that explains how to get Sinatra, Sequel, and RSpec working together plus have all the files organized so that I could just dive in.

If that’s what you’re looking for and all you want to do is get hacking, just download the repo. It provides an example folder structure, migrations, model, helper, routes and specs to help you quickly get started writing your own RESTful app.

Get the Sinatra-Sequel-RSpec Template on Github.

If you’d like an in-depth walk-thru, here’s the (admittedly boring) play-by-play.

Sinatra-Sequel-RSpec Template Walk-Thru

Step 1: Hello Sinatra

I started with the “Hello World” example on the Sinatra site. I literally cut and pasted the code on the homepage into a new file called app.rb.

> mkdir sinatra-sequel-rspec
> cd sinatra-sequel-rspec
> sudo gem install sinatra

I created an app.rb file:

1
2
3
4
5
6
require 'rubygems'
require 'sinatra'
 
get '/hi' do
  "Hello World!"
end

I also created a readme, before I pushed my first commit to github. If you’re following along at home and have the repo, git checkout 3eeb602.

I highly recommend the shotgun gem for running sinatra locally.

> sudo gem install shotgun
> shotgun app.rb

Try it out by going to http://localhost:9393/hi

Step 2: Hello Sequel

Next, I added in some Sequel. Ryan Tomayko’s sinatra-sequel gem made it a snap. I really like to keep my files small and organized, so I created a config folder for the database configuration and migrations.

> sudo gem install sequel sinatra-sequel
> sudo gem install sqlite3
> mkdir config

And then I created the config/init.rb file:

1
2
3
4
5
6
7
8
9
10
11
require 'sinatra/sequel'
require 'sqlite3'
 
configure :development do
  set :database, 'sqlite://tmp/development.sqlite'
end
configure :test do
  set :database, 'sqlite3::memory:'
end
 
require 'config/migrations'

You don’t have to use sqlite, Sequel supports connecting to a lot of databases.

Next I created my config/migrations.rb file.

> touch config/migrations.rb

Now that we have the config/init.rb file in place, app.rb needs to require it.

1
2
3
4
require 'rubygems'
require 'sinatra'
require 'config/init'
...

And that concluded my second commit. If you’re following along at home and have the repo git checkout a45f06e.

Step 3: Hello RSpec

> sudo gem install rspec
> sudo gem install rack-test
> mkdir spec

spec/spec_helper.rb will run the tests.

1
2
3
4
5
6
7
8
9
10
11
require File.join(File.dirname(__FILE__), '..', 'app.rb')
 
require 'rack/test'
require 'ruby-debug'
require 'spec'
 
# set test environment
set :environment, :test
set :run, false
set :raise_errors, true
set :logging, false

And you might want a spec/spec.opts with your preferred options.

1
2
3
4
--colour
--format progress
--loadby mtime
--reverse

And finally I needed a rake task to run my specs, so I added Rakefile:

1
2
3
4
5
6
7
8
desc "Run those specs"
task :spec do
require 'spec/rake/spectask'
 
Spec::Rake::SpecTask.new do |t|
t.spec_files = FileList['spec/**/*_spec.rb']
end
end

And finally, I finally I wrote my first spec in spec/app_spec.rb.

1
2
3
4
5
6
7
8
9
10
require File.dirname(__FILE__) + '/spec_helper'
 
describe "App" do
  include Rack::Test::Methods
 
  it "should respond to /" do
    get '/'
    last_response.should be_ok
  end
end

I then ran rake spec and my test FAILED! Which is exactly what I wanted. I fixed the route in app.rb, got it green, and proved RSpec was working.

My third commit now had all three building blocks, Sinatra, Sequel, and RSpec. If you’re following along at home and have the repo git checkout bd1fbf5.

Step 4: Hello People

My simple app says hello to people. Next, l added a person model, specs, and a migration.

Let’s do the migration first. Edit config/migrations.rb and add…

1
2
3
4
5
6
7
8
9
10
11
12
# Migrations will run automatically. The DSL like wrapper syntax is courtesy
# of sinatra-sequel
#
# For details on sequel's schema modifications, check out:
# http://sequel.rubyforge.org/rdoc/files/doc/schema_rdoc.html
 
migration "create the people table" do
  database.create_table :people do
    primary_key :id
    string      :name
  end
end

This migration will run automatically. Don’t change it after it’s run. More robust up and down ActiveRecord-like migrations are available for Sequel, but since this is a really simple app, really simple migrations will do.

Next, I created the folders for my model and its specs.

> mkdir models
> mkdir spec/models

To load the models, append config/init.rb with…

12
13
14
Dir["models/**/*.rb"].each{|model|
  require model
}

My specs for the Person model will go in spec/models/person_spec.rb.

1
2
3
4
5
6
7
8
9
10
11
require File.dirname(__FILE__) + '/../spec_helper'
 
describe Person do
  describe "validations" do
    it "should require a name" do
      Person.new().should_not be_valid
      Person.new(:name => '').should_not be_valid
      Person.new(:name => "Maggie").should be_valid
    end
  end
end

I started autospec before creatig models/person_rb.

1
2
3
4
5
class Person < Sequel::Model
  def validate
  errors.add(:name, "can't be empty") if name.nil? || name.empty?
  end
end

Note that like ActiveRecord, Sequel does pluralizations. So the person records go in the people table.

Before I commited, I needed a .gitignore:

1
*.sqlite

I then pushed my fourth commit. If you’re following along at home and have the repo git checkout 10901eb.

Step 5: Hello Everyone!

After that things got easy. I continued to run autospec, write tests, and implemented code. Look at the individual commits or play with the final code.

  • commit 07bfdf5: Add a public folder for css, a layout and our first view.
  • commit a9845f9: Add the restful controller for people and helpers to get partials.
  • commit 4b3837c: Added the readme and license.

I hope this template and walk-thru help you get started with Sinatra, Sequel and RSpec faster. I’m sure the template’s not perfect, so you’re welcome to fork it.

4 Comments (+add yours?)

  1. David Padbury
    Sep 05, 2010 @ 08:05:19

    That’s really useful – thanks Rob! I have noticed a bit of a problem setting up the test environment for the specs. You’ve got the “set :environment, :test” in spec_helper.rb, however this gets executed after the configure methods in init.rb run. Therefore the configure :development methods are being run, not the configure :test methods. Any ideas?

    Reply

  2. WaYdotNET
    Feb 27, 2011 @ 10:47:21

    Many Thanks for this post…

    Carlo Bertini

    Reply

  3. Winfred
    Jul 04, 2014 @ 07:00:27

    This article is very informative but it took me a long time
    to find it in google. I found it on 22 spot, you should focus on quality backlinks building, it will help you to increase traffic.
    And i know how to help you, just type in google – k2
    seo tips and tricks

    Reply

  4. Chang
    Aug 30, 2014 @ 17:37:22

    I read a lot of interesting articles here. Probably you
    spend a lot of time writing, i know how to save you a lot of time, there is an online
    tool that creates readable, SEO friendly posts in minutes, just search in google
    - laranitas free content source

    Reply

Leave a Reply