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

后端 未结 2 1041
伪装坚强ぢ
伪装坚强ぢ 2020-11-30 21:10

I am doing the Rails Tutorial for the second time. When I enter this

rails generate integration_test static_pages

I get spec/rails_h

2条回答
  •  生来不讨喜
    2020-11-30 21:34

    You can always combine all your configs into the spec_helper and only require the spec helper int he rails helper file.

    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

    #rails_helper.rb
    
    require 'spec_helper'
    
    #EMPTY FILE
    
    

    and just bring in all the rails specific setup in

    # 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
    

提交回复
热议问题