簡體   English   中英

SimpleCov報告在使用Spork運行RSpec測試后沒有在Rails 3應用程序中生成

[英]SimpleCov reports not generating in Rails 3 app after running RSpec tests with Spork

我剛剛安裝了SimpleCov gem來在我的Rails 3.2.6應用程序上生成代碼覆蓋率報告,並且它與RSpec一起工作很好,而不是Spork。 我可以通過運行rspec --no-drb spec/來獲得所需的正確報告,但我也希望通過使用rspec spec/運行Spork。

鑒於有人在這方面取得了成功,我的設置似乎有誤。 我已經閱讀了設置說明以及聲稱為Spork用戶修復的GitHub問題 ,但仍然沒有運氣。 我想知道是否有人可以提供他們可以用作參考的工作規范/ spec_helper.rb文件的完整示例,因為廣泛的谷歌搜索只發現了片段。 根據其他網站的建議,我嘗試將config / environments / test.rb中config.cache_classes從默認的true更改為false !(ENV['DRB'] == 'true') ,沒有運氣。

作為參考,這是我的設置方式:

的Gemfile

group :development, :test do
  # ...
  gem 'rspec-rails', '2.10.1'
end

group :test do
  # ...
  gem 'spork', '0.9.0'
  gem 'simplecov', '0.6.4', require: false
end

的.spec

--colour
--drb

spec / spec_helper.rb (根據GitHub問題更改)

require 'simplecov'
SimpleCov.start 'rails'

require 'rubygems'
require 'spork'

Spork.prefork do
  unless ENV['DRB']
    require 'simplecov'
    SimpleCov.start 'rails'
  end

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

  Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}

  RSpec.configure do |config|
    config.mock_with :rspec
    config.fixture_path = "#{::Rails.root}/spec/fixtures"
    config.use_transactional_fixtures = true
    config.infer_base_class_for_anonymous_controllers = false
  end
end

Spork.each_run do
  if ENV['DRB']
    require 'simplecov'
    SimpleCov.start 'rails'
  end
end

我已經嘗試注釋/更改此文件的前兩個SimpleCov語句和Spork塊內的Simplecov語句,但似乎無法找到有效的組合。

我錯過了什么? 我需要更改其他文件嗎?

我設法得到一個正常工作的spec / spec_helper.rb配置,只需使用$ rspec spec/命令就可以正確執行SimpleCov,這要歸功於Github問題的評論,該問題將我發送到此博客條目其示例spec / spec_helper.rb 為什么這個作品中包含的(非常詳細!)博客中的所有原因。 SampleApp替換為您的應用程序的名稱。

投機/ spec_helper.rb

require 'rubygems'
require 'spork'

Spork.prefork do
  unless ENV['DRB']
    require 'simplecov'
    SimpleCov.start 'rails'
  end

  require 'rails/application'
  require Rails.root.join("config/application")

  ENV["RAILS_ENV"] ||= 'test'
  require 'rspec/rails'
  require 'rspec/autorun'

  RSpec.configure do |config|
    config.mock_with :rspec
    config.fixture_path = "#{::Rails.root}/spec/fixtures"
    config.use_transactional_fixtures = false

    config.before :each do
      if Capybara.current_driver == :rack_test
        DatabaseCleaner.strategy = :transaction
      else
        DatabaseCleaner.strategy = :truncation
      end
      DatabaseCleaner.start
    end

    config.after do
      DatabaseCleaner.clean
    end

    config.infer_base_class_for_anonymous_controllers = false
  end
end

Spork.each_run do
  if ENV['DRB']
    require 'simplecov'
    SimpleCov.start 'rails'
    SampleApp::Application.initialize!
    class SampleApp::Application
      def initialize!; end
    end
  end

  Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
end

編輯

如果您使用Travis-CI ,請不要按原樣使用此代碼,因為您可能會undefined method 'root' for Rails:Module (NoMethodError)錯誤獲取undefined method 'root' for Rails:Module (NoMethodError) 如果你知道如何解決這個問題,請分享。

編輯2

我讓Travis CI基本上把所有東西放在Spork.each_run塊中,這似乎顯着減慢了測試速度。 必須有一個更好的方法來做到這一點,或者它似乎不值得為了不必運行$ rspec --no-drb spec/一次獲得SimpleCov報告...

投機/ spec_helper.rb

require 'rubygems'
require 'spork'

Spork.prefork do
  unless ENV['DRB']
    require 'simplecov'
    SimpleCov.start 'rails'
  end

  require 'rails/application'
  ENV["RAILS_ENV"] ||= 'test'
end

Spork.each_run do
  if ENV['DRB']
    require 'simplecov'
    SimpleCov.start 'rails'
    require Rails.root.join("config/application")
    SampleApp::Application.initialize!
    class SampleApp::Application
      def initialize!; end
    end
  end

  unless ENV['DRB']
    require File.expand_path("../../config/environment", __FILE__)
  end

  require 'rspec/rails'
  require 'rspec/autorun'

  Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}

  RSpec.configure do |config|
    config.mock_with :rspec

    config.fixture_path = "#{::Rails.root}/spec/fixtures"
    config.use_transactional_fixtures = false
    config.before :each do
      if Capybara.current_driver == :rack_test
        DatabaseCleaner.strategy = :transaction
      else
        DatabaseCleaner.strategy = :truncation
      end
      DatabaseCleaner.start
    end

    config.after do
      DatabaseCleaner.clean
    end
    config.infer_base_class_for_anonymous_controllers = false
  end
end

編輯3

在使用這個配置幾天之后,它似乎沒有像我之前想象的那樣減慢速度,所以我認為這是接受的答案,除非發布更優雅的答案。

編輯4

使用這個配置了幾個月之后,我認識到,它我想象要慢。 其中一部分是認識到Spork似乎可以減慢測試套件,並且最好是快速迭代聚焦測試,而不是總是運行整個測試套件。 下面的SO問題和博客文章讓我看到下面的spec_helper.rb文件,該文件可以運行帶有或不帶Spork的SimpleCov,運行速度比之前更快,並且可以與Capybara 2.0一起使用。

投機/ spec_helper.rb

require 'rubygems'
require 'spork'
require 'simplecov'
#uncomment the following line to use spork with the debugger
#require 'spork/ext/ruby-debug'

Spork.prefork do
  ENV["RAILS_ENV"] ||= 'test'
  unless ENV['DRB']
    SimpleCov.start 'rails'
    require File.expand_path("../../config/environment", __FILE__)
  end

  require 'rspec/rails'
  require 'rspec/autorun'
  require 'capybara/rails'
  require 'capybara/rspec'

  # files to preload based on results of Kernel override code below
  # ie they took more than 100 ms to load
  require "sprockets"
  require "sprockets/eco_template"
  require "sprockets/base"
  require "active_record/connection_adapters/postgresql_adapter"
  require "tzinfo"
  require "tilt"
  require "journey"
  require "journey/router"
  require "haml/template"

  RSpec.configure do |config|
    config.mock_with :rspec

    # If you're not using ActiveRecord, or you'd prefer not to run each of your
    # examples within a transaction, remove the following line or assign false
    # instead of true.
    config.use_transactional_fixtures = false

    # If true, the base class of anonymous controllers will be inferred
    # automatically. This will be the default behavior in future versions of
    # rspec-rails.
    config.infer_base_class_for_anonymous_controllers = false

    config.include FactoryGirl::Syntax::Methods

    config.before :suite do
      # PerfTools::CpuProfiler.start("/tmp/rspec_profile")
      DatabaseCleaner.strategy = :transaction
      DatabaseCleaner.clean_with(:truncation)
    end

    # Request specs cannot use a transaction because Capybara runs in a
    # separate thread with a different database connection.
    config.before type: :request do
      DatabaseCleaner.strategy = :truncation
    end

    # Reset so other non-request specs don't have to deal with slow truncation.
    config.after type: :request  do
      DatabaseCleaner.strategy = :transaction
    end

    RESERVED_IVARS = %w(@loaded_fixtures)
    last_gc_run = Time.now

    config.before(:each) do
      GC.disable
    end

    config.before do
      DatabaseCleaner.start
    end

    config.after do
      DatabaseCleaner.clean
    end

    # Release instance variables and trigger garbage collection
    # manually every second to make tests faster
    # http://blog.carbonfive.com/2011/02/02/crank-your-specs/
    config.after(:each) do
      (instance_variables - RESERVED_IVARS).each do |ivar|
        instance_variable_set(ivar, nil)
      end
      if Time.now - last_gc_run > 1.0
        GC.enable
        GC.start
        last_gc_run = Time.now
      end
    end

    config.after :suite do
      # PerfTools::CpuProfiler.stop

      # REPL to query ObjectSpace
      # http://blog.carbonfive.com/2011/02/02/crank-your-specs/
      # while true
      #   '> '.display
      #   begin
      #     puts eval($stdin.gets)
      #   rescue Exception => e
      #     puts e.message
      #   end
      # end
    end
  end

  # Find files to put into preload
  # http://www.opinionatedprogrammer.com/2011/02/profiling-spork-for-faster-start-up-time/
  # module Kernel
  #   def require_with_trace(*args)
  #     start = Time.now.to_f
  #     @indent ||= 0
  #     @indent += 2
  #     require_without_trace(*args)
  #     @indent -= 2
  #     Kernel::puts "#{' '*@indent}#{((Time.now.to_f - start)*1000).to_i} #{args[0]}"
  #   end
  #   alias_method_chain :require, :trace
  # end
end

Spork.each_run do
  # This code will be run each time you run your specs.
  if ENV['DRB']
    SimpleCov.start 'rails'
    SampleApp::Application.initialize!
    class SampleApp::Application
      def initialize!; end
    end
  end

  # Requires supporting ruby files with custom matchers and macros, etc,
  # in spec/support/ and its subdirectories.
  Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
  FactoryGirl.reload
  I18n.backend.reload!
end

我在這個特定版本的Rails應用程序中工作:

  • rails (3.2.8)
  • guard (1.3.2)
  • guard-spork (1.1.0)
  • spork (0.9.2)
  • simplecov (0.6.4)
  • rspec-rails (2.11.0)

spec/spec_helper.rb如下所示:

require "spork"

Spork.prefork do

end

Spork.each_run do
  require "simplecov"
  SimpleCov.start "rails"

  ENV["RAILS_ENV"] ||= "test"
  require File.expand_path("../../config/environment", __FILE__)
  require "rspec/rails"
  require "capybara/rspec"

  Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }

  FactoryGirl.reload
end

雖然從性能角度來看要求Spork.each_run塊中的所有內容可能不太理想,但這在每次測試調用時都能正確運行SimpleCov,並且開銷似乎相對較低。

我希望有所幫助。 祝好運!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM