Skip to content

Commit 7d416f7

Browse files
authored
Update RSpec guide (#428)
* Update RSpec setup guide and templates This time in the correct branch xd * Delete unused template files * Refactor RSpec guide to use incremental configuration snippets * Refactor RSpec config and documentation structure * Replace all single quotes with double quotes * Update RSpec setup guide and templates This time in the correct branch xd * Delete unused template files * Refactor RSpec guide to use incremental configuration snippets * Refactor RSpec config and documentation structure * Replace all single quotes with double quotes * Fix lint
1 parent 2ddd4a1 commit 7d416f7

File tree

3 files changed

+66
-118
lines changed

3 files changed

+66
-118
lines changed

ruby_on_rails/rspec.md

Lines changed: 66 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,15 @@ 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`
3130

32-
```ruby
33-
require 'simplecov'
31+
### spec/spec_helper.rb
32+
33+
Add SimpleCov configuration at the top of the file (before `RSpec.configure`):
34+
35+
```ruby
36+
# Run code coverage and exclude files with less than 5 lines of code
37+
unless ENV["NO_COVERAGE"]
38+
require "simplecov"
3439
SimpleCov.start "rails" do
3540
add_filter "app/channels/application_cable/channel.rb"
3641
add_filter "app/channels/application_cable/connection.rb"
@@ -41,43 +46,70 @@ You should know exactly why you are adding each one of them and why is necessary
4146
enable_coverage :branch
4247
minimum_coverage line: 100, branch: 100
4348
end
44-
```
49+
end
50+
```
4551

46-
to run code coverage and exclude files with less then 5 lines of code.
52+
Add the following configuration options inside the `RSpec.configure` block:
4753

48-
* Inside `spec/spec_helper.rb` we suggest you to uncomment/enable the following:
54+
```ruby
55+
RSpec.configure do |config|
56+
# ... existing configuration ...
4957

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
58+
config.expect_with :rspec do |expectations|
59+
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
60+
end
61+
62+
config.mock_with :rspec do |mocks|
63+
mocks.verify_partial_doubles = true
64+
end
65+
66+
config.run_all_when_everything_filtered = true
5667

5768
config.define_derived_metadata do |meta|
5869
meta[:aggregate_failures] = true
5970
end
60-
```
71+
end
72+
```
6173

62-
Please check the [spec_helper template](../templates/spec/spec_helper.rb)
74+
We suggest you to also unable/uncomment the following:
6375

64-
* Add the configurations:
76+
```ruby
77+
config.disable_monkey_patching!
78+
config.default_formatter = "doc" if config.files_to_run.one?
79+
config.profile_examples = 5
80+
config.order = :random
81+
Kernel.srand config.seed
82+
```
6583

66-
```rb
67-
# spec/rails_helper.rb:
84+
### spec/rails_helper.rb
6885

69-
# after `require 'rspec/rails'`
70-
require 'capybara/rspec'
71-
require 'capybara/rails'
72-
require 'selenium/webdriver'
73-
require 'super_diff/rspec-rails'
86+
Add the following requires:
7487

75-
Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f }
88+
```ruby
89+
# after `require "rspec/rails"`
90+
require "capybara/rspec"
91+
require "capybara/rails"
92+
require "selenium/webdriver"
93+
require "super_diff/rspec-rails"
94+
```
95+
96+
Add the following after the requires (before `RSpec.configure`):
97+
98+
```ruby
99+
Rails.root.glob("spec/support/**/*.rb").each { |f| require f }
100+
```
76101

77-
# ... (omitted configs here)
102+
Add the following configuration inside the `RSpec.configure` block:
78103

104+
```ruby
79105
RSpec.configure do |config|
80-
# ... (omitted configs here)
106+
# ... existing configuration ...
107+
108+
config.include FactoryBot::Syntax::Methods
109+
config.include ActiveSupport::Testing::TimeHelpers
110+
config.include JavaScriptErrorReporter, type: :system, js: true
111+
112+
config.infer_spec_type_from_file_location!
81113

82114
config.before do |example|
83115
ActionMailer::Base.deliveries.clear
@@ -93,30 +125,33 @@ RSpec.configure do |config|
93125
driven_by :rack_test
94126
end
95127

128+
config.before(:all, type: :system) do
129+
Capybara.server = :puma, { Silent: true }
130+
end
131+
96132
config.before(:each, type: :system, js: true) do
97-
driven_by ENV['SELENIUM_DRIVER']&.to_sym || :selenium_chrome_headless
133+
driven_by ENV["SELENIUM_DRIVER"]&.to_sym || :selenium_chrome_headless
98134
Capybara.page.current_window.resize_to(1280, 800)
99135
end
100136
end
101137
```
102138

139+
### .env.example
140+
103141
```yml
104-
# .env.example
105142
# SELENIUM_DRIVER="selenium_chrome"
106143
SELENIUM_DRIVER="selenium_chrome_headless"
107144
```
108145

109-
```rb
110-
# config/environments/development.rb
146+
### config/environments/development.rb
111147

148+
```rb
112149
config.generators do |g|
113150
g.test_framework :rspec
114151
end
115152

116153
```
117154

118-
Please check the full [rails_helper template](../templates/spec/rails_helper.rb) to compare.
119-
120155
* Add the line `bundle exec parallel_rspec` to `bin/check`
121156

122157
> **Note**: If you want to debug a spec, you can simply uncomment the line `SELENIUM_DRIVER` in the .env to not run it headless:
@@ -158,7 +193,3 @@ nctl get applications --project={PROJECT_NAME}
158193
## Javascript error reporter
159194

160195
* 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.

templates/spec/rails_helper.rb

Lines changed: 0 additions & 42 deletions
This file was deleted.

templates/spec/spec_helper.rb

Lines changed: 0 additions & 41 deletions
This file was deleted.

0 commit comments

Comments
 (0)