SimpleCov 报告使用 Spork 运行 RSpec 测试后未在 Rails 3 应用程序中生成

2023-11-23

我刚刚安装了简单冠状病毒gem 在我的 Rails 3.2.6 应用程序上生成代码覆盖率报告,它与 RSpec 配合得很好,但与 Spork 配合不好。我可以通过运行获得所需的正确报告rspec --no-drb spec/,但我也想让它们与 Spork 一起运行,只使用rspec spec/.

鉴于有人在这方面取得了成功,看来我的设置可能存在错误。我有仔细阅读设置说明以及GitHub问题据称可以为 Spork 用户提供修复,但仍然没有成功。我想知道是否有人可以提供他们工作的完整示例规范/spec_helper.rb我可以用作参考的文件,因为广泛的谷歌搜索只出现了片段。根据其他网站的建议,我尝试更改config.cache_classes in 配置/环境/test.rb从默认的true to false to !(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_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该文件的语句和里面的 Simplecov 语句Spork块,但似乎无法找到有效的组合。

我缺少什么?还有其他文件需要更改吗?


我设法找到了一份工作规范/spec_helper.rb只需使用以下命令即可正确执行 SimpleCov 的配置$ rspec spec/命令感谢对 Github 问题的评论那把我送到这个博客条目, and 它的例子规范/spec_helper.rb。所有原因why该作品包含在(非常详细!)博客条目中。代替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

Edit

如果你使用特拉维斯-CI,不要按原样使用此代码,因为您可能会得到undefined method 'root' for Rails:Module (NoMethodError)错误。如果您知道如何解决此问题,请分享。

Edit 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

Edit 3

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

Edit 4

使用这个配置几个月后,我开始意识到它is比我想象的要慢。部分原因是人们意识到 Spork 似乎可以减慢测试套件的速度,并且最适合快速迭代的集中测试,而不是总是用它运行整个测试套件。以下问题和博客文章让我了解到规范助手.rb下面的文件,无论有没有 Spork 都可以运行 SimpleCov,运行速度比以前更快,并且可以与 Capybara 2.0 配合使用。

  • 使用 spork 的 Rails 项目 - 总是必须使用 spork?
  • 如何使用 perftools 和捆绑器分析 RSpec?
  • Spork.trap_method 柔术
  • 当 Spork 在你的 Cucumber 上放一把叉子,在你的规格上放一把扳手
  • 调整你的规格
  • 分析 Spork 以加快启动时间
  • Sane RSpec 配置用于干净且稍快的规格

规范/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
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

SimpleCov 报告使用 Spork 运行 RSpec 测试后未在 Rails 3 应用程序中生成 的相关文章

  • 使用 RSpec 测试模块内的类

    所以 我的 ruby 代码中有一个模块 如下所示 module MathStuff class Integer def least factor implementation code end end end 我有一些 RSpec 测试 我
  • RSpec 中出现意外的 nil 变量

    我有一个非常基本的 RSpec 示例 但不起作用 这是代码 require spec helper describe Referral type functionality do describe Affiliate system do b
  • Rails 控制器创建 Model.new 和 Model.create 之间的操作差异

    我正在浏览一些 Rails 3 和 4 教程 并发现了一些我想要的见解 Model new 和 Model create 在创建操作方面有什么区别 我以为你确实使用了create控制器中的保存方法 例如 post Post create p
  • Unit::Test 与 Rspec 之间的区别 [关闭]

    Closed 这个问题是基于意见的 help closed questions 目前不接受答案 我对感兴趣Test Unit and Rspec 有人可以向我解释一下两者之间的主要区别是什么 就它们的运作原理而言 测试 单位更类似于 JUn
  • 让 Selenium 与 Bootstrap 模式淡入淡出配合的建议?

    我正在努力以 BDD 的方式生活 我正在使用 Cucumber 带有 Selenium 并且碰巧在我的应用程序中使用 Twitter Bootstrap 模式 在运行 Cucumber 测试时 我得到了 Selenium WebDriver
  • Capistrano 3 运行每个命令两次(新安装)- 配置问题

    我刚刚完成第一次 Capistrano 安装 大部分内容都保留为默认设置 我配置了我的服务器 其身份验证 远程文件夹以及对 git 存储库的访问 我使用 capistrano 将 php 代码部署到我的服务器 上限分期部署 and 上限生产
  • ActionDispatch::Http::UploadedFile.content_type 在 Rspec 测试中未初始化

    背景 我有一个Book模型与一个cover file通过我的 Rails 控制器之一使用上传的文件设置的属性 我正在使用 Rails v4 0 4 Goal 我想测试是否仅保存具有特定内容类型的文件 我计划创建 Rspec 测试示例Acti
  • Rails - 将模块包含到控制器中,以在视图中使用

    我对 Rails 很陌生 我尝试设置一个要在视图中使用的模块文件 所以我相信正确的行为是将模块定义为控制器中的助手 瞧 它应该可以工作 然而 对我来说情况并非如此 这是结构 lib functions form manager rb 表单管
  • 如何用载波发送文件给用户?

    这是我将文件发送到浏览器的旧代码 def show send file File join Rails root tmp price xls end 但最近我发现 tmp 文件夹不能用作 Heroku 上的持久存储 因此我决定将文件移动到
  • Rails 3:# 的未定义方法“page”

    我无法克服这一点 我知道我读过没有数组的页面方法 但我该怎么办 如果我在控制台中运行 Class all 它会返回 但如果我运行 Class all page 1 则会收到上述错误 有任何想法吗 没有数组没有页面方法 看起来你正在使用kam
  • 如何使用 RSpec & Rails 4 测试子域约束

    我正在尝试编写一个测试子域约束的 控制器测试 但是 我无法让 RSpec 设置子域 并且如果子域不准确则返回错误 我正在使用 Rails 4 2 6 和 RSpec 3 4 路线 rb namespace frontend api do c
  • Rails 3 - 与其自身具有一对一关系的模型 - 我需要belongs_to

    我有一个名为 Person 的模型 它有两个属性 name 和parent person id 一个人总会有一个父母 我应该在模型中使用belongs to吗 如果是的话 这样做有什么好处 class Person lt ActiveRec
  • Rails——自我与@

    我正在关注 Michael Hartl 的 RoR 教程 它涵盖了密码加密的基础知识 这是当前的用户模型 class User lt ActiveRecord Base attr accessor password attr accessi
  • Rails 3 中关联的标记装置已损坏

    升级到 Rails 3 后 引用其他标记的装置 用于关系 的装置将停止工作 夹具标签被解释为字符串 而不是查找具有该名称的实际夹具 Example Dog yml sparky name Sparky owner john Person y
  • Rails 3 ActiveAdmin。如何为关联记录设置默认排序顺序?

    我有一个发货模型和一个发票模型 发票属于装运 所以我添加了一个默认的发货排序顺序 如下所示 config sort order file number desc 但现在我想为发票添加相同的排序顺序 发货表是具有 file number 列的
  • 如何在 rspec 中模拟实例变量

    我有两节课OneClass and AnotherClass class OneClass def initialize args another member AnotherClass new end def my method if a
  • Rails 日志太详细

    如何防止 Rails 记录过多日志 这是我的 production log 文件中的典型跟踪 许多部分 缓存命中 它在开发中很有用 但我不希望在我的生产环境中使用它 Started GET redirected true for 46 19
  • 销毁/删除 Rails 中的数据库

    是否可以从现有应用程序中完全删除数据库和所有迁移记录等 以便我可以从头开始重新设计数据库 通过发行rake T您有以下数据库任务 rake db create Create the database from DATABASE URL or
  • 如何在 Rails rspec 中测试 cookie 过期时间

    在 rspec 中设置 cookie 有很多困惑http relishapp com rspec rspec rails v 2 6 dir controller specs file cookies http relishapp com
  • Windows 上本机 C++ 应用程序中的自动死代码检测?

    背景 我有一个用原生 C 编写的应用程序 花了几年的时间 大约有 60 KLOC 有很多函数和类已经死了 可能有 10 15 就像下面提出的类似的基于 Unix 的问题 我们最近开始对所有新代码进行单元测试 并尽可能将其应用于修改后的代码

随机推荐

  • cx_Freeze 导致 Python 3.7.0 崩溃

    cx Freeze 6 0b1 最新版本 是否支持 Python 3 7 0 我刚刚创建了一个简单的项目 它适用于 Python 3 5 4 但不适用于 Python 3 7 0 它显示 Python 致命错误 initfsencoding
  • TestNG 可以看到我的 Spock (JUnit) 测试结果吗?

    需要让 TestNG 来运行我的 Spock 测试 因为 TestNG 在系统的其余部分中使用 由于 TestNG 支持运行 JUnit 测试 我尝试了以下方法
  • 如何调试“安全句柄已关闭”错误

    我继承的代码不断崩溃 并出现以下错误 根本没有改变 System ObjectDisposedException Safe handle has been closed at Microsoft Win32 UnsafeNativeMeth
  • Spring bean 范围。单例和原型

    假设有两个类ClassA和ClassB 假设 ClassB 依赖于 ClassA 在配置文件中 如果我们将 ClassA 的范围定义为单例 将 ClassB 的范围定义为 Prototype 那么每次创建 ClassA 的 bean 实例时
  • ES2021 (ES12) 中的 WeakRef 和终结器是什么

    我想了解什么是WeakRef和终结器ES2021 with a 真正的简单例子 and Where使用它们 I know WeakRef是一个类 这将允许开发人员创建对对象的弱引用 而终结器或FinalizationRegistry允许您注
  • Django 使用 Allauth 进行其余身份验证

    我已经用 Allauth 实现了 django Rest auth 如果我通过 google 登录 它工作正常access token但有一种情况 某些客户端设备需要通过谷歌登录id token 如果我使用 我会收到错误id token代替
  • 改造:如何在没有内容编码的情况下解析 GZIP 响应:gzip 标头

    我正在尝试处理 GZIP 的服务器响应 响应带有标头 Content Type application x gzip 但没有标题 Content Encoding gzip 如果我使用代理添加该标头 响应就会被很好地解析 我对服务器没有任何
  • 使用 JS 更改 svg 元素的位置

    尝试制作一个在按下按钮时移动的 svg 矩形 现在我只想通过函数修改 x function modX document getElementById rectangle transform translate 295 115 var x 2
  • 进程间通信建议[关闭]

    Closed 这个问题不符合堆栈溢出指南 目前不接受答案 我正在寻找一种轻量级 快速且简单的方法来处理 Linux 计算机上某些程序之间的进程间通信 目前 我正在考虑命名管道 因为它是由操作系统本身提供的 关于性能或可用性有什么注意事项吗
  • 汇编语言 - 如何进行取模?

    x86 汇编中是否有类似模运算符或指令之类的东西 如果您的模数 除数是已知常数 并且您关心性能 请参阅this and this 对于直到运行时才知道的循环不变值 乘法逆甚至是可能的 例如看https libdivide com 但是如果没
  • 展开 折叠 展开 JTree 延迟加载的问题

    我已经使用延迟加载实现了一棵树 第一级节点是在创建树时创建的 而子节点仅在用户展开任何特定节点时创建 数据来自数据库 我们向数据库发出查询以填充子节点 实现了 TreeExpansionListener 并使用 treeExpanded 方
  • boost asio异步等待条件变量

    是否可以对 boost asio 中的条件变量执行异步等待 读取 非阻塞 如果不直接支持任何有关实现的提示 我们将不胜感激 我可以实现一个计时器 甚至每隔几毫秒就触发一次唤醒 但这种方法要差得多 我发现很难相信条件变量同步没有实现 记录 如
  • 正确地将 DateTime 从 C# 插入到 mongodb

    我尝试在 MongoDB 中插入当地时间 var time DateTime Now 03 05 2014 18 30 30 var query new QueryDocument time nowTime collection3 Inse
  • 为什么要使用 urlencode?

    我正在编写一个 Web 应用程序并学习如何对 html 链接进行 urlencode 这里的所有 urlencode 问题 参见下面的标签 都是 如何 问题 我的问题不是 如何 但为什么 即使维基百科的文章也只讨论了它的机制 http en
  • 移动 NumPy 数组中的所有索引

    我有一个像这样的 numpy 数组 x np array 0 1 2 3 4 想要创建一个数组 其中索引 0 中的值位于索引 1 中 索引 1 中的值位于索引 2 中 依此类推 我想要的输出是 y np array 0 0 1 2 3 我猜
  • 在命令行中制作java包

    虽然它可能是推荐使用的 IDE 来编码高级 java 项目 但我个人更喜欢几乎完全运行命令行 使用 gedit 作为文本编辑器 所以请不要只是告诉我 就用 eclipse 吧 或其他什么 P 我的问题是在java中通过命令创建包的方法是什么
  • 使用 group by 进行 SQL 连接的 HQL 版本

    我有两张表 Band 和 Votes Band 有一个名称和一个 id Votes 有一个 Total votes 列和一个名为 band id 的外键 该外键指向 band id 我有很多选票 在不同日期保存 我想要做的是找到每个频段的
  • 如何使用 readline 支持重新安装 ruby​​?

    我已经按照 RVM 的说明安装了 Rubyhttps github com wayneeseguin rvm installation 作为信息 我有所有档案 readline 5 2 tar gz readline 6 2 tar gz
  • 在 java eclipse 控制台中更改颜色

    有没有办法改变eclipse控制台中的文本颜色 我不是在谈论当我进入选项并将颜色从黑色更改为红色时 我的意思是 就像当我启动程序并执行代码时 它会在某个时刻改变颜色 例如 code if a 2 change text color to r
  • SimpleCov 报告使用 Spork 运行 RSpec 测试后未在 Rails 3 应用程序中生成

    我刚刚安装了简单冠状病毒gem 在我的 Rails 3 2 6 应用程序上生成代码覆盖率报告 它与 RSpec 配合得很好 但与 Spork 配合不好 我可以通过运行获得所需的正确报告rspec no drb spec 但我也想让它们与 S