Ruby, Rspec and Travis CI

August 2017 · 4 minute read


Sample project at https://github.com/SelenaSmall/ruby_travis

Initialize Repo

Initialise repo on Github with a README.md https://github.com

Login > Your Profile > Repositories > New

Repository Name: ruby_travis_example
Public: YES
Initialise this repository with a README.md: 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
- README.md

Initialise Rspec

https://github.com/rspec/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.

http://www.betterspecs.org/ https://www.anchor.com.au/wp-content/uploads/rspec_cheatsheet_attributed.pdf

app

#!/usr/bin/ruby

require_relative './lib/main'

command = Main.new

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

# Reading user input
loop do
  input = gets.chomp

  next if input.empty?

  unless 'EXIT'.match?(input)
    command.interpret(input)
    next
  end

  $stdout.print "Goodbye! \n"
  break
end

spec/main_spec

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 = Main.new
      command = 'Selena'

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

lib/main

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

    name
  end
end

Configure Rake

https://github.com/ruby/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'
end

Install the gem

$ bundle install

Rakefile

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

task :spec do
  sh 'rspec spec'
end

Run rake to ensure it’s working correctly

$ rake spec


Configure travis-CI

https://docs.travis-ci.com/user/languages/ruby/

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.

.travis.yml

language: ruby

Push to MASTER & See your project build

https://docs.travis-ci.com/


Status Image

Embed status image to your README.md

https://docs.travis-ci.com/user/status-images/

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

README

# Ruby Travis Example
[![Build Status](https://travis-ci.org/SelenaSmall/ruby-travis-example.png)](https://travis-ci.org/SelenaSmall/ruby-travis-example)

Success

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 https://github.com/colszowka/simplecov

Configure SimpleCov

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

$ bundle install

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

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 https://codeclimate.com/

Add your repo

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

https://docs.codeclimate.com/v1.0/docs/travis-ci-ruby-test-coverage

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

Code Coverage Bages

https://codeclimate.com/github/codeclimate/codeclimate/badges

#Code climate
[![Code Climate](https://codeclimate.com/github/SelenaSmall/ruby_travis/badges/gpa.svg)](https://codeclimate.com/github/SelenaSmall/ruby_travis)

#Test coverage
[![Test Coverage](https://codeclimate.com/github/SelenaSmall/ruby_travis/coverage.svg)](https://codeclimate.com/github/SelenaSmall/ruby_travis/coverage)