【问题标题】:How is spec/rails_helper.rb different from spec/spec_helper.rb? Do I need it?spec/rails_helper.rb 与 spec/spec_helper.rb 有何不同?我需要吗?
【发布时间】:2014-07-31 11:19:46
【问题描述】:

我正在第二次学习 Rails 教程。当我输入这个

rails generate integration_test static_pages

我得到spec/rails_helper.rbspec/spec_helper.rb 而不仅仅是spec/spec_helper.rb

现在,当我运行我的测试时,它们比我上次执行时更长(更“冗长”)并且更慢。 我想知道这两个文件之间的区别是什么,以及我是否做错了什么。 另外,有没有办法摆脱rails_helper.rb 文件而不搞乱一切?

【问题讨论】:

  • 您的测试产品有哪些以前没有产生的输出? (可能属于一个新问题。)
  • 我不确定术语,但现在测试通过了每个 gem,这给了我一长串我不明白的东西,然后才会出现结果。之前,它只是给出了结果。我会在这里复制它,但它真的很长......
  • 这可能是 RSpec 3 弃用。如果您无法通过搜索或 myronmars.to/n/dev-blog/2014/05/notable-changes-in-rspec-3 找出它们,请将它们放在一个新问题中。
  • 它已修复,我必须从 .rspec 中删除 --warnings

标签: ruby-on-rails testing rspec rspec-rails rspec3


【解决方案1】:

rspec-rails 3 生成spec_helper.rbrails_helper.rbspec_helper.rb 用于不依赖于 Rails 的规范(例如 lib 目录中的类的规范)。 rails_helper.rb 用于依赖于 Rails 的规范(在 Rails 项目中,大部分或全部)。 rails_helper.rb 需要 spec_helper.rb。所以不,不要摆脱rails_helper.rb;在您的规范中要求它(而不是 spec_helper.rb)。

如果您希望您的不依赖于 Rails 的规范强制它们不依赖于 Rails,并在您自己运行它们时尽可能快地运行,您可以要求 spec_helper.rb 而不是 rails_helper.rb在那些。但是在.rspec 中使用-r rails_helper 非常方便,而不是在每个规范文件中都需要一个或另一个助手,因此这肯定是一种流行的方法。

如果您使用的是 spring 预加载器,则每个类只需要加载一次,并且 spring loads classes eagerly even if you only run a single spec that requires spec_helper,因此在某些文件中只需要 spec_helper 并没有多大价值。

来源:https://www.relishapp.com/rspec/rspec-rails/docs/upgrade#default-helper-files

【讨论】:

  • 这很令人困惑。我将添加一个 PR 来更新 rspec-rails 自述文件,以按照您在此处的说明进行拼写。谢谢你的解释。
  • 对于开始使用 rspec 的人来说,这真是一团糟!
【解决方案2】:

您始终可以将所有配置合并到 spec_helper 中,并且只需要在 rails 帮助文件中使用规范帮助程序。

这绝不是“理想的”,因为归根结底,您是手动执行此“重构”,但如果它真的让您感到困扰。只知道这完全取决于您如何构建Rspec.configure

#rails_helper.rb

require 'spec_helper'

#EMPTY FILE

只需将所有特定于 Rails 的设置引入

# spec_helper.rb

# This file is copied to spec/ when you run 'rails generate rspec:install'
require 'spec_helper'
ENV['RAILS_ENV'] ||= 'test'

require File.expand_path('../config/environment', __dir__)

# Prevent database truncation if the environment is production
abort("The Rails environment is running in production mode!") if Rails.env.production?
require 'rspec/rails'
# Add additional requires below this line. Rails is not loaded until this point!

# Requires supporting ruby files with custom matchers and macros, etc, in
# spec/support/ and its subdirectories. Files matching `spec/**/*_spec.rb` are
# run as spec files by default. This means that files in spec/support that end
# in _spec.rb will both be required and run as specs, causing the specs to be
# run twice. It is recommended that you do not name files matching this glob to
# end with _spec.rb. You can configure this pattern with the --pattern
# option on the command line or in ~/.rspec, .rspec or `.rspec-local`.
#
# The following line is provided for convenience purposes. It has the downside
# of increasing the boot-up time by auto-requiring all files in the support
# directory. Alternatively, in the individual `*_spec.rb` files, manually
# require only the support files necessary.
#
# Dir[Rails.root.join('spec', 'support', '**', '*.rb')].each { |f| require f }

# Checks for pending migrations and applies them before tests are run.
# If you are not using ActiveRecord, you can remove these lines.
begin
  ActiveRecord::Migration.maintain_test_schema!
rescue ActiveRecord::PendingMigrationError => e
  puts e.to_s.strip
  exit 1
end
RSpec.configure do |config|

... all our config.whatever_your_heart_desires

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-11-11
    • 2023-03-25
    • 1970-01-01
    • 2012-08-22
    • 2019-08-04
    • 1970-01-01
    • 1970-01-01
    • 2012-04-08
    相关资源
    最近更新 更多