Ruby, Rspec and Travis CI

August 2017 · 4 minute read

Sample project at

Initialize Repo

Initialise repo on Github with a

Login > Your Profile > Repositories > New

Repository Name: ruby_travis_example
Public: YES
Initialise this repository with a YES

Create Repository

Clone to your local working directory From terminal:

$ cd {your_working_dir}

$ git clone {repo_name}

Ruby project base structure

  • Create a .ruby-version file
  • Create the lib dir for the project files
  • Create the project root file app.rb
  • Create a /lib directory for where class will be stored

Base Dir

- /lib
	- main.rb
- .ruby-version
- app.rb

Initialise Rspec

  • Create Gemfile with rspec spec
  • Generate your gemfile.lock $ gem install bundle && bundle install
  • Initialise Rspec $ rspec –init

Add to project root

- /spec
- .rspec
- Gemfile
- Gemfile.lock

Write a test and corresponding code

I’ve just written some sample code to retrieve a user input and return their name, for the sake of demonstration and to ensure the tests build correctly.



require_relative './lib/main'

command =

$stdout.print "What's your name? \n"

# Reading user input
loop do
  input = gets.chomp

  next if input.empty?

  unless 'EXIT'.match?(input)

  $stdout.print "Goodbye! \n"


require 'rspec'
require 'spec_helper'
require './lib/main'

describe Main do
  describe '#interpret' do
    it 'should return the command which is a String' do
      instance =
      command = 'Selena'

      expect(instance.interpret(command)).to be_a String
      expect(instance.interpret(command)).to eq 'Selena'


# Main class
class Main
  # Interpret method
  # @param name [String]
  # @return name
  def interpret(name)
    $stdout.print "Hello #{name.capitalize}. \n"


Configure Rake

Travis CI will attempt to run rake to execute your tests. You need to ensure you have a default task which will run all the tests you wish to execute.

So, let’s get the specs building with Rake.

Add rake to your Gemfile

group :test do
  gem 'rake'

Install the gem

$ bundle install


task default: [:spec]
desc 'run Rspec specs'

task :spec do
  sh 'rspec spec'

Run rake to ensure it’s working correctly

$ rake spec

Configure travis-CI

Create a .travis.yml in the app root and add the language. By default, if a ruby version is to specified, travis will look for a .ruby-version in the project root and test against that.


language: ruby

Push to MASTER & See your project build

Status Image

Embed status image to your

If everything worked and the build was successful, add the status image to your README


# Ruby Travis Example
[![Build Status](](


Now you can implement Travis-CI with all your Ruby projects and ensure your tests pass whenever committing new code!! :D :D

Code Coverage

Now that builds are passing successfully, let’s find out how good they are by testing the code coverage with SimpleCov

Configure SimpleCov

  • Add SimpleCov to your Gemfile and bundle install:
group :test do
	gem 'simplecov', require: false

$ bundle install

  • Load and launch SimpleCov at the very top of your test/spec_helper.rb
require 'simplecov'
  • Create a .gitignore and add the following to ensure that coverage results are not tracked by Git

Test that the code coverage runs > $ rake spec

Expected result

Finished in 0.00381 seconds (files took 0.36614 seconds to load)

1 example, 0 failures

Coverage report generated for RSpec to /Users/selena/Sites/ruby_travis/coverage. 14 / 14 LOC (100.0%) covered.

Add Code Coverage to Travis

Set up account with CodeClimate

Add your repo

Go to > Test Coverage tab and retrieve your Test Reporter ID

  • Add the codeclimate-test-reporter and simplecov gems to your Gemfile
group :test do
  gem ‘codeclimate-test-reporter’, ‘~> 1.0.0’
  • Configure your .travis.yml file to use Code Climate
    repo_token: <test-reporter-ID>
# regular test configuration
  - bundle exec codeclimate-test-reporter

Code Coverage Bages

#Code climate
[![Code Climate](](

#Test coverage
[![Test Coverage](](