- 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
- Git (https://git-scm.com/).
- Docker (https://www.docker.com/get-started).
- docker-compose (https://docs.docker.com/compose/).
- A rails project.
Step 1: Clone Repo
# Clone git repo
git clone git@github.com:brayvasq/jiffy.git
# Move to the repo directory
cd jiffy/
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
And rebuild our custom image again.
docker-compose build web
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
Now we can run tests:
# Run tests
docker-compose exec web rspec spec
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
Initialiaze simplecov. Add the following lines in the first two lines of spec_helper file
# spec/spec_helper
require 'simplecov'
SimpleCov.start 'rails'
Example output. Run tests and then:
# Run tests
docker-compose exec web rspec spec
# Open coverage output
open coverage/index.html
Or open manually coverage/index.html
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)