Skip to content

Commit 0993605

Browse files
committed
Update RSpec setup instructions and templates
1 parent cf71a3c commit 0993605

File tree

1 file changed

+53
-39
lines changed

1 file changed

+53
-39
lines changed

ruby_on_rails/rspec.md

Lines changed: 53 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -27,57 +27,72 @@ You should know exactly why you are adding each one of them and why is necessary
2727

2828
* Install rspec via `rails generate rspec:install`
2929
* Create a bin stub with `bundle binstubs rspec-core`
30-
* At the top of the `spec/spec_helper.rb`
30+
* Replace the entire contents of `spec/spec_helper.rb` with:
3131

3232
```ruby
33-
require 'simplecov'
34-
SimpleCov.start "rails" do
35-
add_filter "app/channels/application_cable/channel.rb"
36-
add_filter "app/channels/application_cable/connection.rb"
37-
add_filter "app/jobs/application_job.rb"
38-
add_filter "app/mailers/application_mailer.rb"
39-
add_filter "app/models/application_record.rb"
40-
add_filter ".semaphore-cache"
41-
enable_coverage :branch
42-
minimum_coverage line: 100, branch: 100
33+
# Run code coverage and exclude files with less than 5 lines of code
34+
unless ENV['NO_COVERAGE']
35+
require 'simplecov'
36+
SimpleCov.start 'rails' do
37+
add_filter 'app/channels/application_cable/channel.rb'
38+
add_filter 'app/channels/application_cable/connection.rb'
39+
add_filter 'app/jobs/application_job.rb'
40+
add_filter 'app/mailers/application_mailer.rb'
41+
add_filter 'app/models/application_record.rb'
42+
add_filter '.semaphore-cache'
43+
enable_coverage :branch
44+
minimum_coverage line: 100, branch: 100
45+
end
4346
end
44-
```
45-
46-
to run code coverage and exclude files with less then 5 lines of code.
4747

48-
* Inside `spec/spec_helper.rb` we suggest you to uncomment/enable the following:
49-
50-
```ruby
51-
config.disable_monkey_patching!
52-
config.default_formatter = 'doc' if config.files_to_run.one?
53-
config.profile_examples = 5
54-
config.order = :random
55-
Kernel.srand config.seed
56-
57-
config.define_derived_metadata do |meta|
58-
meta[:aggregate_failures] = true
48+
RSpec.configure do |config|
49+
config.expect_with :rspec do |expectations|
50+
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
51+
end
52+
config.mock_with :rspec do |mocks|
53+
mocks.verify_partial_doubles = true
54+
end
55+
56+
config.run_all_when_everything_filtered = true
57+
58+
# We suggest you to also keep the following enabled:
59+
config.disable_monkey_patching!
60+
config.default_formatter = 'doc' if config.files_to_run.one?
61+
config.profile_examples = 5
62+
config.order = :random
63+
Kernel.srand config.seed
64+
65+
config.define_derived_metadata do |meta|
66+
meta[:aggregate_failures] = true
67+
end
5968
end
6069
```
6170

62-
Please check the [spec_helper template](../templates/spec/spec_helper.rb)
63-
64-
* Add the configurations:
71+
* Replace the entire contents of `spec/rails_helper.rb` with:
6572

6673
```rb
67-
# spec/rails_helper.rb:
68-
69-
# after `require 'rspec/rails'`
74+
ENV['RAILS_ENV'] ||= 'test'
75+
require 'spec_helper'
76+
require_relative '../config/environment'
77+
# Prevent database truncation if the environment is production
78+
abort('The Rails environment is running in production mode!') if Rails.env.production?
79+
require 'rspec/rails'
7080
require 'capybara/rspec'
7181
require 'capybara/rails'
7282
require 'selenium/webdriver'
7383
require 'super_diff/rspec-rails'
7484

75-
Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f }
85+
ActiveRecord::Migration.maintain_test_schema!
7686

77-
# ... (omitted configs here)
87+
Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f }
7888

7989
RSpec.configure do |config|
80-
# ... (omitted configs here)
90+
config.include FactoryBot::Syntax::Methods
91+
config.include ActiveSupport::Testing::TimeHelpers
92+
config.include JavaScriptErrorReporter, type: :system, js: true
93+
94+
config.use_transactional_fixtures = true
95+
config.infer_spec_type_from_file_location!
8196

8297
config.before do |example|
8398
ActionMailer::Base.deliveries.clear
@@ -93,6 +108,10 @@ RSpec.configure do |config|
93108
driven_by :rack_test
94109
end
95110

111+
config.before(:all, type: :system) do
112+
Capybara.server = :puma, { Silent: true }
113+
end
114+
96115
config.before(:each, type: :system, js: true) do
97116
driven_by ENV['SELENIUM_DRIVER']&.to_sym || :selenium_chrome_headless
98117
Capybara.page.current_window.resize_to(1280, 800)
@@ -115,7 +134,6 @@ end
115134

116135
```
117136

118-
Please check the full [rails_helper template](../templates/spec/rails_helper.rb) to compare.
119137

120138
* Add the line `bundle exec parallel_rspec` to `bin/check`
121139

@@ -158,7 +176,3 @@ nctl get applications --project={PROJECT_NAME}
158176
## Javascript error reporter
159177

160178
* Create the module [`spec/support/javascript_error_reporter.rb`](../templates/spec/support/javascript_error_reporter.rb)
161-
162-
* Verify that `config.include JavaScriptErrorReporter, type: :system, js: true` is in your [`rails_helper.rb`](../templates/spec/rails_helper.rb)
163-
164-
Please check the [rails_helper template](../templates/spec/rails_helper.rb) to compare.

0 commit comments

Comments
 (0)