DEV Community

Jose Castro
Jose Castro

Posted on

Setup Rspec and Simplecov for Rails project on Docker

  • Rspec is a testing software for Ruby on Rails as a alternative to Minitest
  • Simplecov is a code coverage analysis tool for Ruby

Fot this example Jiffy is the name of our project

Requirements

Step 1: Clone Repo

# Clone git repo
git clone git@github.com:brayvasq/jiffy.git

# Move to the repo directory
cd jiffy/
Enter fullscreen mode Exit fullscreen mode

Step 2: Add and install gems

  • Add rspec and simplecov gems on test environment
# Gemfile
group :test do
  gem 'rspec-rails', '~> 4.1.0'
  gem 'simplecov', require: false
end
Enter fullscreen mode Exit fullscreen mode

And rebuild our custom image again.

docker-compose build web
Enter fullscreen mode Exit fullscreen mode

Generate boilerplate config files to rspec docker-compose exec web rails generate rspec:install

# Run container in background
docker-compose up -d

# Install basic config files to rspec
docker-compose exec web rails generate rspec:install
Enter fullscreen mode Exit fullscreen mode

Now we can run tests:

# Run tests
docker-compose exec web rspec spec
Enter fullscreen mode Exit fullscreen mode

Run rspec

Step 3: Setup simplecov

When installing simplecov gem, coverage folder is created. We can add it to the .gitignore

docker-compose exec web echo "coverage" >> .gitignore
Enter fullscreen mode Exit fullscreen mode

Initialiaze simplecov. Add the following lines in the first two lines of spec_helper file

# spec/spec_helper
require 'simplecov'
SimpleCov.start 'rails'
Enter fullscreen mode Exit fullscreen mode

Example output. Run tests and then:

# Run tests
docker-compose exec web rspec spec

# Open coverage output
open coverage/index.html
Enter fullscreen mode Exit fullscreen mode

Or open manually coverage/index.html

Coverage report

Note: This guide can be used as a base for any gem

Final

Any questions or comments are welcome, I'll share more content in following posts.

Thanks for reading!

Top comments (0)