Rails:合并simplecov结果给出RuntimeError(不能修改冻结对象)

时间:2016-12-10 08:36:16

标签: ruby-on-rails ruby simplecov

我尝试使用simplecov通过Rails应用程序中的测试来检查代码覆盖率。所以我遇到了常见问题:未覆盖的文件未显示在报告中。将Rails.application.eager_load!添加到test_helper.rb以及更改config/environments/test.rb(此问题的解决方案:Simple cov gem missing untested files in Rails)对我来说无效。

我尝试了这里提出的解决方案:https://github.com/colszowka/simplecov/issues/16#issuecomment-31076575

现在我的test_helper.rb看起来像这样:

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

现在调用rails test后我收到错误:

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>'

有没有办法修复此错误或加载没有这堆代码的文件?

1 个答案:

答案 0 :(得分:0)

最后通过改变线

解决了这个问题
merged = SimpleCov::Result.new(Coverage.result).original_result.merge_resultset(base_result)

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

仍然不知道为什么修复错误。