加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails-3 – 使用Spork运行RSpec测试后,SimpleCov报告不

发布时间:2020-12-17 04:27:49 所属栏目:百科 来源:网络整理
导读:我刚安装了 SimpleCov gem以在我的Rails 3.2.6应用程序上生成代码覆盖率报告,它与RSpec一起工作很好,而不是Spork.我可以通过运行rspec –no-drb spec /来获得所需的正确报告,但我还想使用rspec spec /来运行Spork. 鉴于有人在这方面取得了成功,我的设置似乎
我刚安装了 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

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 issue更改)

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,感谢 a comment on the Github issue将我发送到 this blog entry和 its example 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,请不要按原样使用此代码,因为您可能会为Rails:Module(NoMethodError)错误获取未定义的方法“root”.如果你知道如何解决这个问题,请分享.

编辑2

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

> 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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读