Home / Community
Home / Community

Sinatra, the green way

2474931f864bd75089bfd026a75c87dfby Mario Zaizar|

Mar 14, 2011


Sinatra is one of the most popular micro frameworks for Ruby, it’s an easy and fast way to create web applications or services.

On the other hand, Cucumber is a powerful tool to create user stories and acceptance tests. Together they are a quick and efficient way to create web applications.

In this post we'll create our first Sinatra application using 'outside-in' development with Cucumber or how I like to call it, the green way.

First Sinatra application.

First of all, we need to create our basic environment. Personally, I like to use a gemset per project.

Then we need to create our application's directory. Let’s call it ‘greenway’. Inside the ‘greenway’ directory, call bundle init (assuming you already installed bundler) and then edit the Gemfile so that it looks like the example below:

  source 'http://rubygems.org'

  gem 'sinatra'
  gem 'cucumber'
  gem 'cucumber-sinatra'
  gem 'capybara'
  gem 'rspec'

With rvm you can use diferents rubies and gemsets for each project. If you don't know rvm. What are you waiting for? go to RVM. For more info on gemsets and RVM check out these posts: getting started with rvm Use a project specific Ruby version

Now we can install our gems, with this command

  $ bundle install

If you haven’t installed Bundler, you can do so with:

  $ gem install bundler

Now we need to create our Sinatra application, set the configuration for Cucumber, define the web steps and so on. To start we have to install the cucumber-sinatra gem, it does most of the configuration work for us. For more info visit cucumber-sinatra $ cucumber-sinatra init --app Hello src/hello.rb

Writing tests

Now we can write our cucumber test... yes, I said test.

Inside the features directory create a new file called hello.feature.

        Feature: Cucumber web testing
          In Order to verify if my app works
          As A developer
          I want to see my home page

          Scenario: View hello page
            Given I am on the home page
            Then I should see "It's alive!"

Then run the test: $ cucumber feature/hello.feature

Red tests

And... it Fails! But don’t worry, that was expected. Now we are ready to write the code we need to make that test pass.

Writing Code

This is the moment to write the code to pass the test. Open the file src/hello.rb and edit it to look like the following:

        require 'sinatra/base'

        class Hello < Sinatra::Base
          get '/' do
            "It's alive!"

And now, run the test again

  $ cucumber feature/hello.feature

Green tests

Now we're green. If you want to see your web application, you can do it with:

  $ rackup -p 4567

And you just have to go to "localhost:4567/" in your browser

Congrats for your first "green way" Sinatra application! To celebrate, I’ll leave you with Frank


Cucumber Behavior Driven Development tool.

Siantra a DSL for quickly creating web applications in Ruby.

Cucumber-Sinatra helpful gem to initialize a cucumber environment for a sinatra application.

RVM Command line tool to easily install, manage and work with multiple ruby environments.

Bundler a tool that manages gem dependencies for your ruby application.

blog comments powered byDisqus