简体   繁体   English

spec/rails_helper.rb 与 spec/spec_helper.rb 有何不同? 我需要吗?

[英]How is spec/rails_helper.rb different from spec/spec_helper.rb? Do I need it?

I am doing the Rails Tutorial for the second time.我是第二次做 Rails 教程。 When I enter this当我输入这个

rails generate integration_test static_pages

I get spec/rails_helper.rb and spec/spec_helper.rb instead of just spec/spec_helper.rb我得到spec/rails_helper.rbspec/spec_helper.rb而不仅仅是spec/spec_helper.rb

Now when I run my tests, they are longer (more "verbose") and slower than when I did this last time.现在,当我运行我的测试时,它们比我上次运行时更长(更“详细”)和更慢。 I am wondering what the difference between the two files is, and if I did something wrong.我想知道这两个文件之间的区别是什么,以及我是否做错了什么。 Also, is there a way to get rid of the rails_helper.rb file without messing everything up?另外,有没有办法摆脱rails_helper.rb文件而不会弄乱一切?

rspec-rails 3 generates spec_helper.rb and rails_helper.rb . rspec-rails 3 生成spec_helper.rbrails_helper.rb spec_helper.rb is for specs which don't depend on Rails (such as specs for classes in the lib directory). spec_helper.rb用于不依赖于 Rails 的规范(例如 lib 目录中的类的规范)。 rails_helper.rb is for specs which do depend on Rails (in a Rails project, most or all of them). rails_helper.rb用于依赖于 Rails 的规范(在 Rails 项目中,大部分或全部)。 rails_helper.rb requires spec_helper.rb . rails_helper.rb需要spec_helper.rb So no, don't get rid of rails_helper.rb ;所以不,不要摆脱rails_helper.rb require it (and not spec_helper.rb ) in your specs.在您的规范中需要它(而不是spec_helper.rb )。

If you want your non-Rails-dependent specs to enforce that they're non-Rails-dependent, and to run as fast as possible when you run them by themselves, you could require spec_helper.rb rather than rails_helper.rb in those.如果你希望你的非 Rails 依赖的规范强制它们是非 Rails 依赖的,并且在你自己运行它们时尽可能快地运行,你可以在那些中需要spec_helper.rb而不是rails_helper.rb But it's very convenient to -r rails_helper in your .rspec rather than requiring one helper or the other in each spec file, so that is sure to be a popular approach.但它是非常方便-r rails_helper.rspec而不需要一个帮手或其他每个规格文件,这样肯定会成为一种流行的做法。

If you're using the spring preloader, each class only needs to be loaded once, and spring loads classes eagerly even if you only run a single spec that requires spec_helper , so there isn't as much value in requiring only spec_helper in some files.如果您使用的弹簧预加载,每个班只需要加载一次,并且弹簧加载类热切,即使你只运行需要一个规范spec_helper ,所以没有尽可能多的价值,仅需要在spec_helper在一些文件.

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

You can always combine all your configs into the spec_helper and only require the spec helper int he rails helper file.您始终可以将所有配置组合到 spec_helper 中,并且只需要在 rails 帮助文件中使用规范帮助文件。

It is by no means "ideal" since at the end of the day, you are manually doing this "refactor" but IF it really bothers you.这绝不是“理想的”,因为在一天结束时,您正在手动进行此“重构”,但如果它真的困扰您。 just know thats it totally up to you how to structure the Rspec.configure只知道这完全取决于您如何构建Rspec.configure

#rails_helper.rb

require 'spec_helper'

#EMPTY FILE

and just bring in all the rails specific setup in只需引入所有 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在spec_helper.rb中指定自定义格式化程序? - How do I specify a custom formatter in spec_helper.rb? RSpec - 找不到spec / spec_helper.rb - RSpec - Can't find spec/spec_helper.rb RSpec:在spec_helper.rb中包含一个自定义帮助程序模块 - RSpec: Include a custom helper module in spec_helper.rb 在spec_helper.rb中进行哪些更改以运行我的规格? - what changes to be made in spec_helper.rb to run my specs? spec_helper.rb中的“警告:已经初始化的常数” - “warning: already initialized constant” in spec_helper.rb 无法从Rspec spec_helper.rb文件加载种子数据 - Unable to load seed data from Rspec spec_helper.rb file 如何将“config.include FactoryBot::Syntax::Methods”添加到 spec_helper.rb 中的 rspec 配置块? - How to add "config.include FactoryBot::Syntax::Methods" to rspec config block in spec_helper.rb? 如何在spec_helper.rb中为Capybara Webkit设置ignore_ssl_errors选项 - How to set the ignore_ssl_errors option for Capybara Webkit in spec_helper.rb rspec首先运行哪个? spec_helper.rb或db:test:prepare - Which one does rspec run first? spec_helper.rb or db:test:prepare 困惑 - spec_helper.rb:94:在` <top (required)> &#39;:未初始化的常量Shoulda(NameError) - Confused - spec_helper.rb:94:in `<top (required)>': uninitialized constant Shoulda (NameError)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM