我正在尝试使用SimpleCov通过Rails应用程序中的测试来检查代码覆盖范围。因此,我遇到了常见的问题:报告中未显示发现的文件。将Rails.application.eager_load!
添加到test_helper.rb
并更改config/environments/test.rb
(解决此问题的解决方案:简单的Cov Gem缺少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>'
是否有一种方法可以解决此错误,也可以加载文件而不这么多代码?
最终通过更改行
自己解决了它merged = SimpleCov::Result.new(Coverage.result).original_result.merge_resultset(base_result)
to
merged = SimpleCov.result.original_result.merge_resultset(base_result)
仍然不知道为什么要解决错误。