0

I'm trying to use simplecov to check code coverage by tests in Rails application. So I bumped into common problem: the uncovered files are not shown in the report. Adding Rails.application.eager_load! to test_helper.rb as well as changing config/environments/test.rb (solutions for this question: Simple cov gem missing untested files in Rails) didn't work for me.

I tried the solution proposed here: https://github.com/colszowka/simplecov/issues/16#issuecomment-31076575

Now my test_helper.rb looks like this:

require 'simplecov'
SimpleCov.start do 
  add_group 'Controllers', 'app/controllers'
  add_group 'Models', 'app/models'
  add_group 'Helpers', 'app/helpers'
  add_group 'Mailers', 'app/mailers'
  add_group 'Views', 'app/views'
end


all_files = Dir['**/*.rb']
base_result = {}
all_files.each do |file|
absolute = File::expand_path(file)
lines = File.readlines(absolute, :encoding => 'UTF-8')
base_result[absolute] = lines.map do |l|
  l.strip!
  l.empty? || l =~ /^end$/ || l[0] == '#' ? nil : 0
end
end


SimpleCov.at_exit do
merged = SimpleCov::Result.new(Coverage.result).original_result.merge_resultset(base_result)
result = SimpleCov::Result.new(merged)
result.format!
end


#Rails.application.eager_load!

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

class ActiveSupport::TestCase 
  # Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order. 
  fixtures :all 

  # Add more helper methods to be used by all tests here... 
end

Now after calling rails test I get the error:

Running via Spring preloader in process 17211
Run options: --seed 8726

# Running:

...................................

Finished in 3.251970s, 10.7627 runs/s, 15.6828 assertions/s.

35 runs, 51 assertions, 0 failures, 0 errors, 0 skips
/var/lib/gems/2.3.0/gems/simplecov-0.12.0/lib/simplecov/merge_helpers.rb:29:in `extend_object': can't modify frozen object (RuntimeError)
    from /var/lib/gems/2.3.0/gems/simplecov-0.12.0/lib/simplecov/merge_helpers.rb:29:in `extend'
    from /var/lib/gems/2.3.0/gems/simplecov-0.12.0/lib/simplecov/merge_helpers.rb:29:in `block in merge_resultset'
    from /var/lib/gems/2.3.0/gems/simplecov-0.12.0/lib/simplecov/merge_helpers.rb:28:in `each_key'
    from /var/lib/gems/2.3.0/gems/simplecov-0.12.0/lib/simplecov/merge_helpers.rb:28:in `merge_resultset'
    from /home/tamila/Ruby/workspace/books_app/test/test_helper.rb:24:in `block in <top (required)>'
    from /var/lib/gems/2.3.0/gems/simplecov-0.12.0/lib/simplecov/defaults.rb:67:in `block in <top (required)>'
    from /var/lib/gems/2.3.0/gems/spring-1.7.2/lib/spring/application.rb:161:in `fork'
    from /var/lib/gems/2.3.0/gems/spring-1.7.2/lib/spring/application.rb:161:in `serve'
    from /var/lib/gems/2.3.0/gems/spring-1.7.2/lib/spring/application.rb:131:in `block in run'
    from /var/lib/gems/2.3.0/gems/spring-1.7.2/lib/spring/application.rb:125:in `loop'
    from /var/lib/gems/2.3.0/gems/spring-1.7.2/lib/spring/application.rb:125:in `run'
    from /var/lib/gems/2.3.0/gems/spring-1.7.2/lib/spring/application/boot.rb:19:in `<top (required)>'
    from /usr/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require'
    from /usr/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require'
    from -e:1:in `<main>'

Is there a way either to fix this error or to load the files without this bunch of code?

Community
  • 1
  • 1
Tami
  • 153
  • 2
  • 2
  • 11

1 Answers1

0

Finally solved it myself by changing the line

merged = SimpleCov::Result.new(Coverage.result).original_result.merge_resultset(base_result)

to

merged = SimpleCov.result.original_result.merge_resultset(base_result)

Still have no idea why it fixed the error.

Tami
  • 153
  • 2
  • 2
  • 11