Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple cov gem missing untested files in Rails

Using simple_cov gem in a Rails app, can we have the files that we are not testing included in the report?

  • If yes, how?

  • If no, that files should count to the coverage percentage, right ?

like image 527
Gustavo Semião-Lobo Avatar asked Feb 27 '14 17:02

Gustavo Semião-Lobo


3 Answers

Try to edit your config/environments/test.rb and set this line:

config.eager_load = false

to true in this way the whole app is loaded and simplecov reads it.

like image 86
alex88 Avatar answered Oct 01 '22 22:10

alex88


Eager load the whole Rails app when running tests suite with code coverage. Add Rails.application.eager_load! to spec_helper.rb.

Simplecov slows down tests that's why I use shell environment variable to turn it on. Usually my spec_helper.rb/rails_helper.rb looks something like this:

if ENV['COVERAGE']
  require 'simplecov'
  # some SimpleCov setup, e.g. formatters
  SimpleCov.start 'rails'
end

ENV['RAILS_ENV'] ||= 'test'
require 'spec_helper'
require File.expand_path('../../config/environment', __FILE__)
require 'rspec/rails'

Rails.application.eager_load! if ENV['COVERAGE']
like image 20
Michał Knapik Avatar answered Oct 01 '22 22:10

Michał Knapik


Since version 0.11.0 the files that should be tracked can be explicit set (Pull Request).

For a Rails app that would be:

require 'simplecov'
SimpleCov.start do
  track_files '{app,lib}/**/*.rb'
end

Or simple use:

require 'simplecov'
SimpleCov.start 'rails'

And the files to be tracked will be set by default (current related code).

like image 25
Filipe Giusti Avatar answered Oct 01 '22 20:10

Filipe Giusti