sajad torkamani

Install gem

Add to Gemfile:

group :development, :test do
  gem 'rspec-rails', '~> 5.1', '>= 5.1.1'
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
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
  puts e.to_s.strip
  exit 1
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/