Skip to content

Commit 9fee6b5

Browse files
committed
Apply current config for Rspec and move extensions to support directory
Further, this commit also configures custom groups for SimpleCov, enriching the report generated with further details.
1 parent 7e3e279 commit 9fee6b5

7 files changed

Lines changed: 84 additions & 53 deletions

File tree

spec/rails_helper.rb

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
# frozen_string_literal: true
22

33
# This file is copied to spec/ when you run 'rails generate rspec:install'
4-
ENV['RAILS_ENV'] ||= 'test'
54
require 'spec_helper'
6-
require File.expand_path('../config/environment', __dir__)
7-
5+
ENV['RAILS_ENV'] ||= 'test'
6+
require_relative '../config/environment'
87
# Prevent database truncation if the environment is production
98
abort('The Rails environment is running in production mode!') if Rails.env.production?
109

1110
# Add additional requires below this line. Rails is not loaded until this point!
1211
require 'rspec/rails'
13-
require 'pundit/matchers'
12+
require 'pundit/rspec'
1413
require 'webmock/rspec'
1514

1615
# Requires supporting ruby files with custom matchers and macros, etc, in
@@ -26,45 +25,45 @@
2625
# directory. Alternatively, in the individual `*_spec.rb` files, manually
2726
# require only the support files necessary.
2827
#
29-
Dir[Rails.root.join('spec/support/**/*.rb')].each {|f| require f }
28+
Rails.root.glob('spec/support/**/*.rb').sort.each {|f| require f }
3029

31-
# Checks for pending migrations before tests are run.
32-
# If you are not using ActiveRecord, you can remove this line.
33-
ActiveRecord::Migration.maintain_test_schema!
30+
# Checks for pending migrations and applies them before tests are run.
31+
# If you are not using ActiveRecord, you can remove these lines.
32+
begin
33+
ActiveRecord::Migration.maintain_test_schema!
34+
rescue ActiveRecord::PendingMigrationError => e
35+
abort e.to_s.strip
36+
end
3437

3538
RSpec.configure do |config|
3639
# Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
3740
config.fixture_paths = [Rails.root.join('spec/fixtures')]
3841

42+
# If you're not using ActiveRecord, or you'd prefer not to run each of your
43+
# examples within a transaction, remove the following line or assign false
44+
# instead of true.
45+
config.use_transactional_fixtures = false
46+
47+
# You can uncomment this line to turn off ActiveRecord support entirely.
48+
# config.use_active_record = false
49+
3950
# RSpec Rails can automatically mix in different behaviours to your tests
4051
# based on their file location, for example enabling you to call `get` and
4152
# `post` in specs under `spec/controllers`.
4253
#
4354
# You can disable this behaviour by removing the line below, and instead
4455
# explicitly tag your specs with their type, e.g.:
4556
#
46-
# RSpec.describe UsersController, :type => :controller do
57+
# RSpec.describe UsersController, type: :controller do
4758
# # ...
4859
# end
4960
#
5061
# The different available types are documented in the features, such as in
51-
# https://relishapp.com/rspec/rspec-rails/docs
62+
# https://rspec.info/features/6-0/rspec-rails
5263
config.infer_spec_type_from_file_location!
5364

54-
config.include Devise::Test::ControllerHelpers, type: :controller
55-
config.include Devise::Test::ControllerHelpers, type: :view
56-
config.include Devise::Test::IntegrationHelpers, type: :request
57-
58-
config.include FactoryBot::Syntax::Methods
59-
config.include Authentication, type: :feature
60-
config.include WaitForAjax, type: :feature
65+
# Filter lines from Rails gems in backtraces.
66+
config.filter_rails_from_backtrace!
67+
# arbitrary gems may also be filtered via:
68+
# config.filter_gems_from_backtrace("gem name")
6169
end
62-
63-
Shoulda::Matchers.configure do |config|
64-
config.integrate do |with|
65-
with.test_framework :rspec
66-
with.library :rails
67-
end
68-
end
69-
70-
WebMock.disable_net_connect!(allow_localhost: true)

spec/spec_helper.rb

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11
# frozen_string_literal: true
22

3+
# SimpleCov configuration *must* be loaded before any of the application code is required.
4+
unless ENV.fetch('RUBYMINE_SIMPLECOV_COVERAGE_PATH', nil)
5+
require 'simplecov'
6+
SimpleCov.start('rails') do
7+
add_group 'Errors', 'app/errors'
8+
add_group 'Policies', 'app/policies'
9+
add_group 'Services', 'app/services'
10+
add_group 'Validators', 'app/validators'
11+
end
12+
end
13+
314
# This file was generated by the `rails generate rspec:install` command. Conventionally, all
415
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
516
# The generated `.rspec` file contains `--require spec_helper` which will cause
@@ -19,16 +30,6 @@
1930
#
2031
# See https://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
2132

22-
unless ENV.fetch('RUBYMINE_SIMPLECOV_COVERAGE_PATH', nil)
23-
require 'simplecov'
24-
SimpleCov.start('rails')
25-
end
26-
27-
RSpec::Matchers.define_negated_matcher :avoid_change, :change
28-
RSpec::Matchers.define_negated_matcher :not_include, :include
29-
RSpec::Matchers.define_negated_matcher :not_have_attributes, :have_attributes
30-
RSpec::Matchers.define_negated_matcher :not_eql, :eql
31-
3233
RSpec.configure do |config|
3334
# rspec-expectations config goes here. You can use an alternate
3435
# assertion/expectation library such as wrong or the stdlib/minitest
@@ -42,31 +43,33 @@
4243
# ...rather than:
4344
# # => "be bigger than 2"
4445
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
45-
46-
# Enable only the newer, non-monkey-patching expect syntax.
47-
# For more details, see:
48-
# - https://rspec.info/blog/2012/06/rspecs-new-expectation-syntax/
49-
expectations.syntax = :expect
5046
end
5147

5248
# rspec-mocks config goes here. You can use an alternate test double
5349
# library (such as bogus or mocha) by changing the `mock_with` option here.
5450
config.mock_with :rspec do |mocks|
55-
# Enable only the newer, non-monkey-patching expect syntax.
56-
mocks.syntax = :expect
57-
5851
# Prevents you from mocking or stubbing a method that does not exist on
5952
# a real object. This is generally recommended, and will default to
6053
# `true` in RSpec 4.
6154
mocks.verify_partial_doubles = true
6255
end
6356

64-
# These two settings work together to allow you to limit a spec run
65-
# to individual examples or groups you care about by tagging them with
66-
# `:focus` metadata. When nothing is tagged with `:focus`, all examples
67-
# get run.
68-
# config.filter_run :focus
69-
# config.run_all_when_everything_filtered = true
57+
# This option will default to `:apply_to_host_groups` in RSpec 4 (and will
58+
# have no way to turn it off -- the option exists only for backwards
59+
# compatibility in RSpec 3). It causes shared context metadata to be
60+
# inherited by the metadata hash of host groups and examples, rather than
61+
# triggering implicit auto-inclusion in groups with matching metadata.
62+
config.shared_context_metadata_behavior = :apply_to_host_groups
63+
64+
# The settings below are suggested to provide a good initial experience
65+
# with RSpec, but feel free to customize to your heart's content.
66+
67+
# This allows you to limit a spec run to individual examples or groups
68+
# you care about by tagging them with `:focus` metadata. When nothing
69+
# is tagged with `:focus`, all examples get run. RSpec also provides
70+
# aliases for `it`, `describe`, and `context` that include `:focus`
71+
# metadata: `fit`, `fdescribe` and `fcontext`, respectively.
72+
config.filter_run_when_matching :focus
7073

7174
# Allows RSpec to persist some state between runs in order to support
7275
# the `--only-failures` and `--next-failure` CLI options. We recommend
@@ -75,9 +78,7 @@
7578

7679
# Limits the available syntax to the non-monkey patched syntax that is
7780
# recommended. For more details, see:
78-
# - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
79-
# - http://www.teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
80-
# - http://myronmars.to/n/dev-blog/2014/05/notable-changes-in-rspec-3#new__config_option_to_disable_rspeccore_monkey_patching
81+
# https://rspec.info/features/3-12/rspec-core/configuration/zero-monkey-patching-mode/
8182
config.disable_monkey_patching!
8283

8384
# Many RSpec users commonly either run the entire suite or an individual

spec/support/authentication.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,7 @@ def sign_in_with_js_driver(user, password)
1313
expect(page).to have_content(I18n.t('devise.sessions.signed_in'))
1414
end
1515
end
16+
17+
RSpec.configure do |config|
18+
config.include Authentication, type: :feature
19+
end

spec/support/devise.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# frozen_string_literal: true
2+
3+
RSpec.configure do |config|
4+
config.include Devise::Test::ControllerHelpers, type: :controller
5+
config.include Devise::Test::ControllerHelpers, type: :view
6+
config.include Devise::Test::IntegrationHelpers, type: :request
7+
end

spec/support/matchers.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# frozen_string_literal: true
2+
3+
RSpec::Matchers.define_negated_matcher :avoid_change, :change
4+
RSpec::Matchers.define_negated_matcher :not_include, :include
5+
RSpec::Matchers.define_negated_matcher :not_have_attributes, :have_attributes
6+
RSpec::Matchers.define_negated_matcher :not_eql, :eql
7+
8+
Shoulda::Matchers.configure do |config|
9+
config.integrate do |with|
10+
with.test_framework :rspec
11+
with.library :rails
12+
end
13+
end

spec/support/wait_for_ajax.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,7 @@ def ajax_requests_finished?
1111
page.evaluate_script('jQuery.active').zero?
1212
end
1313
end
14+
15+
RSpec.configure do |config|
16+
config.include WaitForAjax, type: :feature
17+
end

spec/support/webmock.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# frozen_string_literal: true
2+
3+
WebMock.disable_net_connect!(allow_localhost: true)

0 commit comments

Comments
 (0)