sajad torkamani

Install gem

Add to Gemfile:

group :development, :test do
  gem 'rspec-rails'
end

Run:

./bin/bundle install

Generate binstubs:

./bin/bundle binstubs rspec-core

Delete test/directory

rm -rf test/

Run installer

./bin/rails g rspec:install

Configure spec_helper.rb

Set contents to:

# frozen_string_literal: true

# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
RSpec.configure do |config|
  config.expect_with :rspec do |expectations|
    expectations.include_chain_clauses_in_custom_matcher_descriptions = true
  end

  config.mock_with :rspec do |mocks|
    mocks.verify_partial_doubles = true
  end

  config.shared_context_metadata_behavior = :apply_to_host_groups

  config.filter_run_when_matching :focus

  config.order = :random
  Kernel.srand config.seed
end

Configure rails_helper.rb

Set contents to:

# frozen_string_literal: true

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'

Dir[Rails.root.join('spec', 'support', '**', '*.rb')].each { |f| require f }

# Checks for pending migrations and applies them before tests are run.
begin
  ActiveRecord::Migration.maintain_test_schema!
rescue ActiveRecord::PendingMigrationError => e
  abort e.to_s.strip
end

RSpec.configure do |config|
  config.fixture_path = "#{::Rails.root}/spec/fixtures"

  # 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 = true

  config.infer_spec_type_from_file_location!

  # Filter lines from Rails gems in backtraces.
  config.filter_rails_from_backtrace!
  # arbitrary gems may also be filtered via:
  # config.filter_gems_from_backtrace("gem name")

  config.include AbstractController::Translation
end

Configure .rspec

Set contents to:

--require spec_helper
--fail-fast

We want to add the --fail-fast flag so that we stop running any further tests once a single one fails.

Verify setup

./bin/rspec

This should run RSpec successfully.

(Optional) Run RSpec tests in pre-commit hook.

See this post. You’ll want to add the following hook:

./bin/rspec spec/