From f09f742a5325e51dd0b90f802fa350cf82304bef Mon Sep 17 00:00:00 2001 From: ddbrendan <119946469+ddbrendan@users.noreply.github.com> Date: Thu, 4 Dec 2025 14:55:30 +0100 Subject: [PATCH 1/4] Update RSpec setup guide and templates This time in the correct branch xd --- ruby_on_rails/rspec.md | 92 ++++++++++++++++++++++++------------------ 1 file changed, 53 insertions(+), 39 deletions(-) diff --git a/ruby_on_rails/rspec.md b/ruby_on_rails/rspec.md index d2c3d9d1..b9a9aefc 100644 --- a/ruby_on_rails/rspec.md +++ b/ruby_on_rails/rspec.md @@ -27,57 +27,72 @@ You should know exactly why you are adding each one of them and why is necessary * Install rspec via `rails generate rspec:install` * Create a bin stub with `bundle binstubs rspec-core` -* At the top of the `spec/spec_helper.rb` +* Replace the entire contents of `spec/spec_helper.rb` with: ```ruby - require 'simplecov' - SimpleCov.start "rails" do - add_filter "app/channels/application_cable/channel.rb" - add_filter "app/channels/application_cable/connection.rb" - add_filter "app/jobs/application_job.rb" - add_filter "app/mailers/application_mailer.rb" - add_filter "app/models/application_record.rb" - add_filter ".semaphore-cache" - enable_coverage :branch - minimum_coverage line: 100, branch: 100 + # Run code coverage and exclude files with less than 5 lines of code + unless ENV['NO_COVERAGE'] + require 'simplecov' + SimpleCov.start 'rails' do + add_filter 'app/channels/application_cable/channel.rb' + add_filter 'app/channels/application_cable/connection.rb' + add_filter 'app/jobs/application_job.rb' + add_filter 'app/mailers/application_mailer.rb' + add_filter 'app/models/application_record.rb' + add_filter '.semaphore-cache' + enable_coverage :branch + minimum_coverage line: 100, branch: 100 + end end - ``` - - to run code coverage and exclude files with less then 5 lines of code. -* Inside `spec/spec_helper.rb` we suggest you to uncomment/enable the following: - - ```ruby - config.disable_monkey_patching! - config.default_formatter = 'doc' if config.files_to_run.one? - config.profile_examples = 5 - config.order = :random - Kernel.srand config.seed - - config.define_derived_metadata do |meta| - meta[:aggregate_failures] = true + 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.run_all_when_everything_filtered = true + + # We suggest you to also keep the following enabled: + config.disable_monkey_patching! + config.default_formatter = 'doc' if config.files_to_run.one? + config.profile_examples = 5 + config.order = :random + Kernel.srand config.seed + + config.define_derived_metadata do |meta| + meta[:aggregate_failures] = true + end end ``` - Please check the [spec_helper template](../templates/spec/spec_helper.rb) - -* Add the configurations: +* Replace the entire contents of `spec/rails_helper.rb` with: ```rb -# spec/rails_helper.rb: - -# after `require 'rspec/rails'` +ENV['RAILS_ENV'] ||= 'test' +require 'spec_helper' +require_relative '../config/environment' +# Prevent database truncation if the environment is production +abort('The Rails environment is running in production mode!') if Rails.env.production? +require 'rspec/rails' require 'capybara/rspec' require 'capybara/rails' require 'selenium/webdriver' require 'super_diff/rspec-rails' -Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f } +ActiveRecord::Migration.maintain_test_schema! -# ... (omitted configs here) +Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f } RSpec.configure do |config| - # ... (omitted configs here) + config.include FactoryBot::Syntax::Methods + config.include ActiveSupport::Testing::TimeHelpers + config.include JavaScriptErrorReporter, type: :system, js: true + + config.use_transactional_fixtures = true + config.infer_spec_type_from_file_location! config.before do |example| ActionMailer::Base.deliveries.clear @@ -93,6 +108,10 @@ RSpec.configure do |config| driven_by :rack_test end + config.before(:all, type: :system) do + Capybara.server = :puma, { Silent: true } + end + config.before(:each, type: :system, js: true) do driven_by ENV['SELENIUM_DRIVER']&.to_sym || :selenium_chrome_headless Capybara.page.current_window.resize_to(1280, 800) @@ -115,7 +134,6 @@ end ``` -Please check the full [rails_helper template](../templates/spec/rails_helper.rb) to compare. * Add the line `bundle exec parallel_rspec` to `bin/check` @@ -158,7 +176,3 @@ nctl get applications --project={PROJECT_NAME} ## Javascript error reporter * Create the module [`spec/support/javascript_error_reporter.rb`](../templates/spec/support/javascript_error_reporter.rb) - -* Verify that `config.include JavaScriptErrorReporter, type: :system, js: true` is in your [`rails_helper.rb`](../templates/spec/rails_helper.rb) - -Please check the [rails_helper template](../templates/spec/rails_helper.rb) to compare. From f58f39ad341399962957dba1895dfcf6e70cc1b6 Mon Sep 17 00:00:00 2001 From: Brendan Date: Thu, 4 Dec 2025 15:30:23 +0100 Subject: [PATCH 2/4] Delete unused template files --- templates/spec/rails_helper.rb | 42 ---------------------------------- templates/spec/spec_helper.rb | 41 --------------------------------- 2 files changed, 83 deletions(-) delete mode 100644 templates/spec/rails_helper.rb delete mode 100644 templates/spec/spec_helper.rb diff --git a/templates/spec/rails_helper.rb b/templates/spec/rails_helper.rb deleted file mode 100644 index 95e13db0..00000000 --- a/templates/spec/rails_helper.rb +++ /dev/null @@ -1,42 +0,0 @@ -# frozen_string_literal: true - -ENV['RAILS_ENV'] ||= 'test' -require 'spec_helper' -require_relative '../config/environment' -# Prevent database truncation if the environment is production -abort('The Rails environment is running in production mode!') if Rails.env.production? -require 'rspec/rails' -require 'capybara/rspec' -require 'capybara/rails' -require 'selenium/webdriver' -require 'super_diff/rspec-rails' - -ActiveRecord::Migration.maintain_test_schema! - -Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f } - -RSpec.configure do |config| - config.include FactoryBot::Syntax::Methods - config.include ActiveSupport::Testing::TimeHelpers - config.include JavaScriptErrorReporter, type: :system, js: true - - config.use_transactional_fixtures = true - config.infer_spec_type_from_file_location! - - config.before(:each, type: :system) do - driven_by :rack_test - end - - config.before(:all, type: :system) do - Capybara.server = :puma, { Silent: true } - end - - config.before(:each, :js, type: :system) do - driven_by ENV['SELENIUM_DRIVER']&.to_sym || :selenium_chrome_headless - Capybara.page.current_window.resize_to(1280, 800) - end - - config.after do - I18n.locale = I18n.default_locale - end -end diff --git a/templates/spec/spec_helper.rb b/templates/spec/spec_helper.rb deleted file mode 100644 index f19b6d47..00000000 --- a/templates/spec/spec_helper.rb +++ /dev/null @@ -1,41 +0,0 @@ -# frozen_string_literal: true - -unless ENV['NO_COVERAGE'] - require 'simplecov' - SimpleCov.start 'rails' do - add_filter 'app/channels/application_cable/channel.rb' - add_filter 'app/channels/application_cable/connection.rb' - add_filter 'app/jobs/application_job.rb' - add_filter 'app/mailers/application_mailer.rb' - add_filter 'app/models/application_record.rb' - add_filter '.semaphore-cache' - enable_coverage :branch - minimum_coverage line: 100, branch: 100 - end -end - -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.run_all_when_everything_filtered = true - - config.disable_monkey_patching! - - config.default_formatter = 'doc' if config.files_to_run.one? - - config.profile_examples = 5 - - config.order = :random - - Kernel.srand config.seed - - config.define_derived_metadata do |meta| - meta[:aggregate_failures] = true - end -end From ff32414376772a72ca7d237f95d88c439f79f5f4 Mon Sep 17 00:00:00 2001 From: ddbrendan <119946469+ddbrendan@users.noreply.github.com> Date: Wed, 10 Dec 2025 10:00:42 +0100 Subject: [PATCH 3/4] Refactor RSpec guide to use incremental configuration snippets --- ruby_on_rails/rspec.md | 110 +++++++++++++++++++++++------------------ 1 file changed, 62 insertions(+), 48 deletions(-) diff --git a/ruby_on_rails/rspec.md b/ruby_on_rails/rspec.md index b9a9aefc..be17b57e 100644 --- a/ruby_on_rails/rspec.md +++ b/ruby_on_rails/rspec.md @@ -27,71 +27,85 @@ You should know exactly why you are adding each one of them and why is necessary * Install rspec via `rails generate rspec:install` * Create a bin stub with `bundle binstubs rspec-core` -* Replace the entire contents of `spec/spec_helper.rb` with: - - ```ruby - # Run code coverage and exclude files with less than 5 lines of code - unless ENV['NO_COVERAGE'] - require 'simplecov' - SimpleCov.start 'rails' do - add_filter 'app/channels/application_cable/channel.rb' - add_filter 'app/channels/application_cable/connection.rb' - add_filter 'app/jobs/application_job.rb' - add_filter 'app/mailers/application_mailer.rb' - add_filter 'app/models/application_record.rb' - add_filter '.semaphore-cache' - enable_coverage :branch - minimum_coverage line: 100, branch: 100 - end + +### spec/spec_helper.rb + +Add SimpleCov configuration at the top of the file (before `RSpec.configure`): + +```ruby +# Run code coverage and exclude files with less than 5 lines of code +unless ENV['NO_COVERAGE'] + require 'simplecov' + SimpleCov.start 'rails' do + add_filter 'app/channels/application_cable/channel.rb' + add_filter 'app/channels/application_cable/connection.rb' + add_filter 'app/jobs/application_job.rb' + add_filter 'app/mailers/application_mailer.rb' + add_filter 'app/models/application_record.rb' + add_filter '.semaphore-cache' + enable_coverage :branch + minimum_coverage line: 100, branch: 100 + end +end +``` + +Add the following configuration options inside the `RSpec.configure` block: + +```ruby +RSpec.configure do |config| + # ... existing configuration ... + + config.expect_with :rspec do |expectations| + expectations.include_chain_clauses_in_custom_matcher_descriptions = true end - 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.run_all_when_everything_filtered = true - - # We suggest you to also keep the following enabled: - config.disable_monkey_patching! - config.default_formatter = 'doc' if config.files_to_run.one? - config.profile_examples = 5 - config.order = :random - Kernel.srand config.seed - - config.define_derived_metadata do |meta| - meta[:aggregate_failures] = true - end + config.mock_with :rspec do |mocks| + mocks.verify_partial_doubles = true end - ``` -* Replace the entire contents of `spec/rails_helper.rb` with: + config.run_all_when_everything_filtered = true -```rb -ENV['RAILS_ENV'] ||= 'test' -require 'spec_helper' -require_relative '../config/environment' -# Prevent database truncation if the environment is production -abort('The Rails environment is running in production mode!') if Rails.env.production? -require 'rspec/rails' + config.define_derived_metadata do |meta| + meta[:aggregate_failures] = true + end + + # We suggest you to also unable/uncomment the following: + config.disable_monkey_patching! + config.default_formatter = 'doc' if config.files_to_run.one? + config.profile_examples = 5 + config.order = :random + Kernel.srand config.seed +end +``` + +### spec/rails_helper.rb + +Add the following requires: + +```ruby +# after `require 'rspec/rails'` require 'capybara/rspec' require 'capybara/rails' require 'selenium/webdriver' require 'super_diff/rspec-rails' +``` + +Add the following after the requires (before `RSpec.configure`): -ActiveRecord::Migration.maintain_test_schema! +```ruby +Rails.root.glob("spec/support/**/*.rb").each { |f| require f } +``` -Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f } +Add the following configuration inside the `RSpec.configure` block: +```ruby RSpec.configure do |config| + # ... existing configuration ... + config.include FactoryBot::Syntax::Methods config.include ActiveSupport::Testing::TimeHelpers config.include JavaScriptErrorReporter, type: :system, js: true - config.use_transactional_fixtures = true config.infer_spec_type_from_file_location! config.before do |example| From d3353859aa06f4077299826936a8821648bd2ca4 Mon Sep 17 00:00:00 2001 From: ddbrendan <119946469+ddbrendan@users.noreply.github.com> Date: Wed, 10 Dec 2025 10:38:59 +0100 Subject: [PATCH 4/4] Refactor RSpec config and documentation structure --- ruby_on_rails/rspec.md | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/ruby_on_rails/rspec.md b/ruby_on_rails/rspec.md index be17b57e..09a4f7a5 100644 --- a/ruby_on_rails/rspec.md +++ b/ruby_on_rails/rspec.md @@ -68,16 +68,20 @@ RSpec.configure do |config| config.define_derived_metadata do |meta| meta[:aggregate_failures] = true end - - # We suggest you to also unable/uncomment the following: - config.disable_monkey_patching! - config.default_formatter = 'doc' if config.files_to_run.one? - config.profile_examples = 5 - config.order = :random - Kernel.srand config.seed end ``` +We suggest you to also unable/uncomment the following: + +```ruby +config.disable_monkey_patching! +config.default_formatter = 'doc' if config.files_to_run.one? +config.profile_examples = 5 +config.order = :random +Kernel.srand config.seed +``` + + ### spec/rails_helper.rb Add the following requires: @@ -133,15 +137,16 @@ RSpec.configure do |config| end ``` +### .env.example + ```yml -# .env.example # SELENIUM_DRIVER="selenium_chrome" SELENIUM_DRIVER="selenium_chrome_headless" ``` -```rb -# config/environments/development.rb +### config/environments/development.rb +```rb config.generators do |g| g.test_framework :rspec end