ruby-on-rails-3 – 使用Spork运行RSpec测试后,SimpleCov报告不会在Rails 3应用程序中生成

前端之家收集整理的这篇文章主要介绍了ruby-on-rails-3 – 使用Spork运行RSpec测试后,SimpleCov报告不会在Rails 3应用程序中生成前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我刚安装了 SimpleCov gem以在我的Rails 3.2.6应用程序上生成代码覆盖率报告,它与RSpec一起工作很好,而不是Spork.我可以通过运行rspec –no-drb spec /来获得所需的正确报告,但我还想使用rspec spec /来运行Spork.

鉴于有人在这方面取得了成功,我的设置似乎有误.我有read through the setup instructions以及GitHub issue声称可以修复Spork用户,但仍然没有运气.我想知道是否有人可以提供他们可以用作参考的工作规范/ spec_helper.rb文件的完整示例,因为广泛的谷歌搜索只发现了片段.根据其他网站的建议,我尝试将config / environments / test.rb中的config.cache_classes从默认的true更改为false!(ENV [‘DRB’] ==’true’),没有运气.

作为参考,我就是这样设置的:

的Gemfile

  1. group :development,:test do
  2. # ...
  3. gem 'rspec-rails','2.10.1'
  4. end
  5.  
  6. group :test do
  7. # ...
  8. gem 'spork','0.9.0'
  9. gem 'simplecov','0.6.4',require: false
  10. end

的.spec

  1. --colour
  2. --drb

spec / spec_helper.rb(根据GitHub issue更改)

  1. require 'simplecov'
  2. SimpleCov.start 'rails'
  3.  
  4. require 'rubygems'
  5. require 'spork'
  6.  
  7. Spork.prefork do
  8. unless ENV['DRB']
  9. require 'simplecov'
  10. SimpleCov.start 'rails'
  11. end
  12.  
  13. ENV["RAILS_ENV"] ||= 'test'
  14. require File.expand_path("../../config/environment",__FILE__)
  15. require 'rspec/rails'
  16. require 'rspec/autorun'
  17.  
  18. Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
  19.  
  20. RSpec.configure do |config|
  21. config.mock_with :rspec
  22. config.fixture_path = "#{::Rails.root}/spec/fixtures"
  23. config.use_transactional_fixtures = true
  24. config.infer_base_class_for_anonymous_controllers = false
  25. end
  26. end
  27.  
  28. Spork.each_run do
  29. if ENV['DRB']
  30. require 'simplecov'
  31. SimpleCov.start 'rails'
  32. end
  33. end

我已经尝试评论/更改此文件的前两个SimpleCov语句和Spork块内的Simplecov语句,但似乎无法找到有效的组合.

我错过了什么?我还需要更改其他文件吗?

解决方法

我设法得到一个正常工作的spec / spec_helper.rb配置,只需使用$rspec spec /命令正确执行SimpleCov,感谢 a comment on the Github issue将我发送到 this blog entryits example spec/spec_helper.rb.所有这些工作的原因都包含在(非常详细!)中博客文章.将SampleApp替换为您的应用程序的名称.

投机/ spec_helper.rb

  1. require 'rubygems'
  2. require 'spork'
  3.  
  4. Spork.prefork do
  5. unless ENV['DRB']
  6. require 'simplecov'
  7. SimpleCov.start 'rails'
  8. end
  9.  
  10. require 'rails/application'
  11. require Rails.root.join("config/application")
  12.  
  13. ENV["RAILS_ENV"] ||= 'test'
  14. require 'rspec/rails'
  15. require 'rspec/autorun'
  16.  
  17. RSpec.configure do |config|
  18. config.mock_with :rspec
  19. config.fixture_path = "#{::Rails.root}/spec/fixtures"
  20. config.use_transactional_fixtures = false
  21.  
  22. config.before :each do
  23. if Capybara.current_driver == :rack_test
  24. DatabaseCleaner.strategy = :transaction
  25. else
  26. DatabaseCleaner.strategy = :truncation
  27. end
  28. DatabaseCleaner.start
  29. end
  30.  
  31. config.after do
  32. DatabaseCleaner.clean
  33. end
  34.  
  35. config.infer_base_class_for_anonymous_controllers = false
  36. end
  37. end
  38.  
  39. Spork.each_run do
  40. if ENV['DRB']
  41. require 'simplecov'
  42. SimpleCov.start 'rails'
  43. SampleApp::Application.initialize!
  44. class SampleApp::Application
  45. def initialize!; end
  46. end
  47. end
  48.  
  49. Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
  50. end

编辑

如果使用Travis-CI,请不要按原样使用此代码,因为您可能会为Rails:Module(NoMethodError)错误获取未定义的方法“root”.如果你知道如何解决这个问题,请分享.

编辑2

我让Travis CI基本上把所有东西放在Spork.each_run块中,这似乎显着减慢了测试速度.必须有一个更好的方法来做到这一点,或者它似乎不值得为了不必运行$rspec –no-drb规范/一次获得SimpleCov报告…

投机/ spec_helper.rb

  1. require 'rubygems'
  2. require 'spork'
  3.  
  4. Spork.prefork do
  5. unless ENV['DRB']
  6. require 'simplecov'
  7. SimpleCov.start 'rails'
  8. end
  9.  
  10. require 'rails/application'
  11. ENV["RAILS_ENV"] ||= 'test'
  12. end
  13.  
  14. Spork.each_run do
  15. if ENV['DRB']
  16. require 'simplecov'
  17. SimpleCov.start 'rails'
  18. require Rails.root.join("config/application")
  19. SampleApp::Application.initialize!
  20. class SampleApp::Application
  21. def initialize!; end
  22. end
  23. end
  24.  
  25. unless ENV['DRB']
  26. require File.expand_path("../../config/environment",__FILE__)
  27. end
  28.  
  29. require 'rspec/rails'
  30. require 'rspec/autorun'
  31.  
  32. Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
  33.  
  34. RSpec.configure do |config|
  35. config.mock_with :rspec
  36.  
  37. config.fixture_path = "#{::Rails.root}/spec/fixtures"
  38. config.use_transactional_fixtures = false
  39. config.before :each do
  40. if Capybara.current_driver == :rack_test
  41. DatabaseCleaner.strategy = :transaction
  42. else
  43. DatabaseCleaner.strategy = :truncation
  44. end
  45. DatabaseCleaner.start
  46. end
  47.  
  48. config.after do
  49. DatabaseCleaner.clean
  50. end
  51. config.infer_base_class_for_anonymous_controllers = false
  52. end
  53. end

编辑3

在使用这个配置几天之后,它似乎没有像我之前想象的那样减慢速度,所以我会认为这是接受的答案,除非发布更优雅的答案.

编辑4

使用这个配置几个月后,我意识到它比我想象的要慢.部分原因是认为Spork似乎可以减慢测试套件的速度,并且最好是快速迭代聚焦测试,而不是总是运行整个测试套件.以下SO问题和博客文章让我看到下面的spec_helper.rb文件,该文件可以运行带或不带Spork的SimpleCov,运行速度比之前更快,并与Capybara 2.0一起使用.

> Rails project using spork – always have to use spork?
> How to profile RSpec with perftools and bundler?
> Spork.trap_method Jujitsu
> When Spork puts a fork in your Cucumber and a spanner in yor specs
> Crank your specs
> Profiling Spork for faster start-up time
> Sane RSpec config for clean,and slightly faster,specs

投机/ spec_helper.rb

  1. require 'rubygems'
  2. require 'spork'
  3. require 'simplecov'
  4. #uncomment the following line to use spork with the debugger
  5. #require 'spork/ext/ruby-debug'
  6.  
  7. Spork.prefork do
  8. ENV["RAILS_ENV"] ||= 'test'
  9. unless ENV['DRB']
  10. SimpleCov.start 'rails'
  11. require File.expand_path("../../config/environment",__FILE__)
  12. end
  13.  
  14. require 'rspec/rails'
  15. require 'rspec/autorun'
  16. require 'capybara/rails'
  17. require 'capybara/rspec'
  18.  
  19. # files to preload based on results of Kernel override code below
  20. # ie they took more than 100 ms to load
  21. require "sprockets"
  22. require "sprockets/eco_template"
  23. require "sprockets/base"
  24. require "active_record/connection_adapters/postgresql_adapter"
  25. require "tzinfo"
  26. require "tilt"
  27. require "journey"
  28. require "journey/router"
  29. require "haml/template"
  30.  
  31. RSpec.configure do |config|
  32. config.mock_with :rspec
  33.  
  34. # If you're not using ActiveRecord,or you'd prefer not to run each of your
  35. # examples within a transaction,remove the following line or assign false
  36. # instead of true.
  37. config.use_transactional_fixtures = false
  38.  
  39. # If true,the base class of anonymous controllers will be inferred
  40. # automatically. This will be the default behavior in future versions of
  41. # rspec-rails.
  42. config.infer_base_class_for_anonymous_controllers = false
  43.  
  44. config.include FactoryGirl::Syntax::Methods
  45.  
  46. config.before :suite do
  47. # PerfTools::cpuProfiler.start("/tmp/rspec_profile")
  48. DatabaseCleaner.strategy = :transaction
  49. DatabaseCleaner.clean_with(:truncation)
  50. end
  51.  
  52. # Request specs cannot use a transaction because Capybara runs in a
  53. # separate thread with a different database connection.
  54. config.before type: :request do
  55. DatabaseCleaner.strategy = :truncation
  56. end
  57.  
  58. # Reset so other non-request specs don't have to deal with slow truncation.
  59. config.after type: :request do
  60. DatabaseCleaner.strategy = :transaction
  61. end
  62.  
  63. RESERVED_IVARS = %w(@loaded_fixtures)
  64. last_gc_run = Time.now
  65.  
  66. config.before(:each) do
  67. GC.disable
  68. end
  69.  
  70. config.before do
  71. DatabaseCleaner.start
  72. end
  73.  
  74. config.after do
  75. DatabaseCleaner.clean
  76. end
  77.  
  78. # Release instance variables and trigger garbage collection
  79. # manually every second to make tests faster
  80. # http://blog.carbonfive.com/2011/02/02/crank-your-specs/
  81. config.after(:each) do
  82. (instance_variables - RESERVED_IVARS).each do |ivar|
  83. instance_variable_set(ivar,nil)
  84. end
  85. if Time.now - last_gc_run > 1.0
  86. GC.enable
  87. GC.start
  88. last_gc_run = Time.now
  89. end
  90. end
  91.  
  92. config.after :suite do
  93. # PerfTools::cpuProfiler.stop
  94.  
  95. # REPL to query ObjectSpace
  96. # http://blog.carbonfive.com/2011/02/02/crank-your-specs/
  97. # while true
  98. # '> '.display
  99. # begin
  100. # puts eval($stdin.gets)
  101. # rescue Exception => e
  102. # puts e.message
  103. # end
  104. # end
  105. end
  106. end
  107.  
  108. # Find files to put into preload
  109. # http://www.opinionatedprogrammer.com/2011/02/profiling-spork-for-faster-start-up-time/
  110. # module Kernel
  111. # def require_with_trace(*args)
  112. # start = Time.now.to_f
  113. # @indent ||= 0
  114. # @indent += 2
  115. # require_without_trace(*args)
  116. # @indent -= 2
  117. # Kernel::puts "#{' '*@indent}#{((Time.now.to_f - start)*1000).to_i} #{args[0]}"
  118. # end
  119. # alias_method_chain :require,:trace
  120. # end
  121. end
  122.  
  123. Spork.each_run do
  124. # This code will be run each time you run your specs.
  125. if ENV['DRB']
  126. SimpleCov.start 'rails'
  127. SampleApp::Application.initialize!
  128. class SampleApp::Application
  129. def initialize!; end
  130. end
  131. end
  132.  
  133. # Requires supporting ruby files with custom matchers and macros,etc,# in spec/support/ and its subdirectories.
  134. Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
  135. FactoryGirl.reload
  136. I18n.backend.reload!
  137. end

猜你在找的Ruby相关文章