1

类似于SimpleCov 计算用户模型的 0% 覆盖率,但没有乐趣。

我相信(/希望?)我正在遵循希望将 Spring 与 SimpleCov 一起使用中的建议?.

随着“加载并启动 simplecov”代码在spec/rails_helper.rbSpring 下运行,我的User类在加载devise_forsimplecovroutes.rb之前被加载。

在没有 Spring 的情况下运行 -例如$ rspec- 一切都很好。

我缺少什么让它在 Spring 下工作?


  • ruby 2.2.1p85(2015-02-26 修订版 49769)[x86_64-darwin15]
  • simplecov 0.12.0
  • 导轨 4.2.5
  • 春天 1.6.3
  • 设计 3.5.6
  • factory_girl 4.5.0
  • factory_girl_rails 4.6.0

spec/rails_helper.rb

ENV['RAILS_ENV'] ||= 'test'

if ENV['RAILS_ENV'] == 'test'
  puts 'loading simplecov'
  require 'simplecov'
  SimpleCov.start 'rails'
end

require File.expand_path('../../config/environment', __FILE__)
[...]

app/models/user.rb

class User
  puts 'loading User'
  puts caller
  [...]

config/routes.rb

Rails.application.routes.draw do
  scope '(:locale)', locale: /en|es/ do
    root to: 'home#index'

    devise_for :users # this is line 6

输出:

$ spring stop; spring rspec
Spring is not running

loading User
<project-path>/app/models/user.rb:2:in `<top (required)>'
<gems-path>/activesupport-4.2.5/lib/active_support/dependencies.rb:457:in `load'
<gems-path>/activesupport-4.2.5/lib/active_support/dependencies.rb:457:in `block in load_file'
<gems-path>/activesupport-4.2.5/lib/active_support/dependencies.rb:647:in `new_constants_in'
<gems-path>/activesupport-4.2.5/lib/active_support/dependencies.rb:456:in `load_file'
<gems-path>/activesupport-4.2.5/lib/active_support/dependencies.rb:354:in `require_or_load'
<gems-path>/activesupport-4.2.5/lib/active_support/dependencies.rb:494:in `load_missing_constant'
<gems-path>/activesupport-4.2.5/lib/active_support/dependencies.rb:184:in `const_missing'
<gems-path>/activesupport-4.2.5/lib/active_support/inflector/methods.rb:261:in `const_get'
<gems-path>/activesupport-4.2.5/lib/active_support/inflector/methods.rb:261:in `block in constantize'
<gems-path>/activesupport-4.2.5/lib/active_support/inflector/methods.rb:259:in `each'
<gems-path>/activesupport-4.2.5/lib/active_support/inflector/methods.rb:259:in `inject'
<gems-path>/activesupport-4.2.5/lib/active_support/inflector/methods.rb:259:in `constantize'
<gems-path>/activesupport-4.2.5/lib/active_support/dependencies.rb:566:in `get'
<gems-path>/activesupport-4.2.5/lib/active_support/dependencies.rb:597:in `constantize'
<gems-path>/devise-3.5.6/lib/devise.rb:292:in `get'
<gems-path>/devise-3.5.6/lib/devise/mapping.rb:81:in `to'
<gems-path>/devise-3.5.6/lib/devise/mapping.rb:76:in `modules'
<gems-path>/devise-3.5.6/lib/devise/mapping.rb:93:in `routes'
<gems-path>/devise-3.5.6/lib/devise/mapping.rb:160:in `default_used_route'
<gems-path>/devise-3.5.6/lib/devise/mapping.rb:70:in `initialize'
<gems-path>/devise-3.5.6/lib/devise.rb:326:in `new'
<gems-path>/devise-3.5.6/lib/devise.rb:326:in `add_mapping'
<gems-path>/devise-3.5.6/lib/devise/rails/routes.rb:238:in `block in devise_for'
<gems-path>/devise-3.5.6/lib/devise/rails/routes.rb:237:in `each'
<gems-path>/devise-3.5.6/lib/devise/rails/routes.rb:237:in `devise_for'
<project-path>/config/routes.rb:6:in `block (2 levels) in <top (required)>'
<gems-path>/actionpack-4.2.5/lib/action_dispatch/routing/mapper.rb:817:in `scope'
<project-path>/config/routes.rb:3:in `block in <top (required)>'
<gems-path>/actionpack-4.2.5/lib/action_dispatch/routing/route_set.rb:434:in `instance_exec'
<gems-path>/actionpack-4.2.5/lib/action_dispatch/routing/route_set.rb:434:in `eval_block'
<gems-path>/actionpack-4.2.5/lib/action_dispatch/routing/route_set.rb:412:in `draw'
<project-path>/config/routes.rb:1:in `<top (required)>'
<gems-path>/activesupport-4.2.5/lib/active_support/dependencies.rb:268:in `load'
<gems-path>/activesupport-4.2.5/lib/active_support/dependencies.rb:268:in `block in load'
<gems-path>/activesupport-4.2.5/lib/active_support/dependencies.rb:240:in `load_dependency'
<gems-path>/activesupport-4.2.5/lib/active_support/dependencies.rb:268:in `load'
<gems-path>/railties-4.2.5/lib/rails/application/routes_reloader.rb:40:in `block in load_paths'
<gems-path>/railties-4.2.5/lib/rails/application/routes_reloader.rb:40:in `each'
<gems-path>/railties-4.2.5/lib/rails/application/routes_reloader.rb:40:in `load_paths'
<gems-path>/railties-4.2.5/lib/rails/application/routes_reloader.rb:16:in `reload!'
<gems-path>/railties-4.2.5/lib/rails/application/routes_reloader.rb:26:in `block in updater'
<gems-path>/activesupport-4.2.5/lib/active_support/file_update_checker.rb:75:in `call'
<gems-path>/activesupport-4.2.5/lib/active_support/file_update_checker.rb:75:in `execute'
<gems-path>/railties-4.2.5/lib/rails/application/routes_reloader.rb:27:in `updater'
<gems-path>/railties-4.2.5/lib/rails/application/routes_reloader.rb:7:in `execute_if_updated'
<gems-path>/railties-4.2.5/lib/rails/application/finisher.rb:69:in `block in <module:Finisher>'
<gems-path>/railties-4.2.5/lib/rails/initializable.rb:30:in `instance_exec'
<gems-path>/railties-4.2.5/lib/rails/initializable.rb:30:in `run'
<gems-path>/railties-4.2.5/lib/rails/initializable.rb:55:in `block in run_initializers'
<ruby-path>/lib/ruby/2.2.0/tsort.rb:226:in `block in tsort_each'
<ruby-path>/lib/ruby/2.2.0/tsort.rb:348:in `block (2 levels) in each_strongly_connected_component'
<ruby-path>/lib/ruby/2.2.0/tsort.rb:429:in `each_strongly_connected_component_from'
<ruby-path>/lib/ruby/2.2.0/tsort.rb:347:in `block in each_strongly_connected_component'
<ruby-path>/lib/ruby/2.2.0/tsort.rb:345:in `each'
<ruby-path>/lib/ruby/2.2.0/tsort.rb:345:in `call'
<ruby-path>/lib/ruby/2.2.0/tsort.rb:345:in `each_strongly_connected_component'
<ruby-path>/lib/ruby/2.2.0/tsort.rb:224:in `tsort_each'
<ruby-path>/lib/ruby/2.2.0/tsort.rb:203:in `tsort_each'
<gems-path>/railties-4.2.5/lib/rails/initializable.rb:54:in `run_initializers'
<gems-path>/railties-4.2.5/lib/rails/application.rb:352:in `initialize!'
<project-path>/config/environment.rb:5:in `<top (required)>'
<gems-path>/activesupport-4.2.5/lib/active_support/dependencies.rb:274:in `require'
<gems-path>/activesupport-4.2.5/lib/active_support/dependencies.rb:274:in `block in require'
<gems-path>/activesupport-4.2.5/lib/active_support/dependencies.rb:240:in `load_dependency'
<gems-path>/activesupport-4.2.5/lib/active_support/dependencies.rb:274:in `require'
<gems-path>/spring-1.6.3/lib/spring/application.rb:92:in `preload'
<gems-path>/spring-1.6.3/lib/spring/application.rb:143:in `serve'
<gems-path>/spring-1.6.3/lib/spring/application.rb:131:in `block in run'
<gems-path>/spring-1.6.3/lib/spring/application.rb:125:in `loop'
<gems-path>/spring-1.6.3/lib/spring/application.rb:125:in `run'
<gems-path>/spring-1.6.3/lib/spring/application/boot.rb:18:in `<top (required)>'
<ruby-path>/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
<ruby-path>/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
-e:1:in `<main>'
Running via Spring preloader in process 25001
loading simplecov
[...]
Coverage report generated for RSpec to <project-path>/coverage. 212 / 368 LOC (57.61%) covered.

蒂亚!

4

1 回答 1

1

我手头的 Rails 项目有这个问题。(直到现在我才注意到,因为我总是使用 rake 作为 RSpec + Cucumber 套件的一部分运行规范,并且 rspec rake 任务分叉,它逃避了 spring 完成的类加载。使用rake spec不会是你的一个好的解决方案问题,因为分叉否定了弹簧的速度提高。)该项目不使用设计。在那个项目中,这个问题是由两件事引起的:

  1. 在测试环境中急切加载类

    config.eager_load = true在我的config/environments/test.rb(以便未使用的类会出现在覆盖结果中)。我把它改成了false. spring rspec覆盖范围有所改善,但仍不是应有的水平。

    请注意,注释掉config.eager_load = true叶子eager_load nil,它被视为true. 不得不说清楚false

    另请注意,simplecov 还建议设置config.serve_static_files = false. 这对我的项目没有任何影响。

  2. 工厂女工

    加载模型时的回溯包括以下几行:

    <gems path>/factory_girl-4.7.0/lib/factory_girl/find_definitions.rb:15:in `find_definitions'
    <gems path>/factory_girl_rails-4.7.0/lib/factory_girl_rails/railtie.rb:21:in `block in <class:Railtie>'
    

    我从 factory_girl_rails 中评论了以下内容lib/factory_girl_rails/railtie.rb

    # config.after_initialize do
    #   FactoryGirl.find_definitions
    # 
    #   if defined?(Spring)
    #     Spring.after_fork { FactoryGirl.reload }
    #   end
    # end
    

    并在之后立即添加FactoryGirl.find_definitions到我的。然后正确收集覆盖范围。rails_helper.rbrequire 'simplecov'spring rspec

    这部分问题似乎是 factory_girl 设计的结果。我不知道比修改 factory_girl 更好的解决方案。

请注意,类似的问题是由其他类型的早期类加载引起的。有时解决方案是在 config/spring.rb 中启动 simplecov。这对我的项目不起作用。如果您不急于加载测试或使用 factory_girl 这可能是您的解决方案。

于 2016-08-16T15:52:33.967 回答