SimpleCov for Spork/RSpec (Code Test Coverage)

If you’re into testing, you know about test coverages. Some developers aim to strive 100% coverage, but personally, I’d avoid being dogmatic about test coverage. After all test coverage is doesn’t ensure that your code is well tested.

But if you’re into test coverage, or just want a quick and easy way to view your team’s test progress, this gem, simplecov, maybe what you’re looking for.

SimpleCov generates a HTML report in coverage/ located in your Rails.root.

To get this setup on Spork:

  1. Gemfile
# Gemfile
group :test do
  gem 'simplecov', :require => false
end
  1. Due to an issue with the way Spork starts its environment, you need to tweak your configuration strategy from the original one on the README.
# spec/spec_helper

Spork.prefork do

  unless ENV['DRB'] do
     require 'simplecov'
     SimpleCov.start
  end
end

.
.
.

# create the following if it doesn't already exist:
Spork.each_run do
  if ENV['DRB']
    require 'simplecov'
    SimpleCov.start
  end
end 
$ spork && rspec .

Away!

  1. You should see a coverage/ generated right after running the tests. Open the index.html file inside and fight those pesky < 50% coverage files.

Caveats: Apparently, the coverage includes files located in vendor/gems into the report. Avoid them as needed.