From 2eedb9c31e431bbe5f74907ef686a4007dfda515 Mon Sep 17 00:00:00 2001 From: Christian Sutter Date: Fri, 25 Aug 2023 13:51:56 +0000 Subject: [PATCH] Set up RSpec for testing with GOV.UK conventions - Add `rspec-rails` and `govuk_test` gems - Set up RSpec configuration with sensible initial defaults and GOV.UK conventions from Developer Docs --- .gitignore | 6 +++++ .rspec | 1 + Gemfile | 6 +++++ Gemfile.lock | 58 +++++++++++++++++++++++++++++++++++++++++++++ spec/spec_helper.rb | 32 +++++++++++++++++++++++++ 5 files changed, 103 insertions(+) create mode 100644 .rspec create mode 100644 spec/spec_helper.rb diff --git a/.gitignore b/.gitignore index 56ccdfc..2163f59 100644 --- a/.gitignore +++ b/.gitignore @@ -17,3 +17,9 @@ /tmp/pids/* !/tmp/pids/ !/tmp/pids/.keep + +# RSpec run persistence (for e.g. --only-failures) +spec/examples.txt + +# Simplecov +/coverage/* diff --git a/.rspec b/.rspec new file mode 100644 index 0000000..c99d2e7 --- /dev/null +++ b/.rspec @@ -0,0 +1 @@ +--require spec_helper diff --git a/Gemfile b/Gemfile index 1062efe..191fec6 100644 --- a/Gemfile +++ b/Gemfile @@ -11,6 +11,12 @@ gem "activesupport", RAILS_GEMS_VERSION gem "puma", "~> 5.0" +group :test do + gem "simplecov", require: false +end + group :development, :test do gem "debug", platforms: %i[ mri mingw x64_mingw ] + gem "govuk_test" + gem "rspec-rails" end diff --git a/Gemfile.lock b/Gemfile.lock index 418e9a3..f8900e2 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -21,13 +21,32 @@ GEM i18n (>= 1.6, < 2) minitest (>= 5.1) tzinfo (~> 2.0) + addressable (2.8.5) + public_suffix (>= 2.0.2, < 6.0) + brakeman (6.0.1) builder (3.2.4) + capybara (3.39.2) + addressable + matrix + mini_mime (>= 0.1.3) + nokogiri (~> 1.8) + rack (>= 1.6.0) + rack-test (>= 0.6.3) + regexp_parser (>= 1.5, < 3.0) + xpath (~> 3.2) concurrent-ruby (1.2.2) crass (1.0.6) debug (1.8.0) irb (>= 1.5.0) reline (>= 0.3.1) + diff-lcs (1.5.0) + docile (1.4.0) erubi (1.12.0) + govuk_test (3.0.1) + brakeman (>= 5.0.2) + capybara (>= 3.36) + puma + selenium-webdriver (>= 4.0) i18n (1.14.1) concurrent-ruby (~> 1.0) io-console (0.6.0) @@ -36,11 +55,14 @@ GEM loofah (2.21.3) crass (~> 1.0.2) nokogiri (>= 1.12.0) + matrix (0.4.2) method_source (1.0.0) + mini_mime (1.1.5) minitest (5.19.0) nio4r (2.5.9) nokogiri (1.15.4-aarch64-linux) racc (~> 1.4) + public_suffix (5.0.3) puma (5.6.7) nio4r (~> 2.0) racc (1.7.1) @@ -62,11 +84,44 @@ GEM thor (~> 1.0) zeitwerk (~> 2.5) rake (13.0.6) + regexp_parser (2.8.1) reline (0.3.8) io-console (~> 0.5) + rexml (3.2.6) + rspec-core (3.12.2) + rspec-support (~> 3.12.0) + rspec-expectations (3.12.3) + diff-lcs (>= 1.2.0, < 2.0) + rspec-support (~> 3.12.0) + rspec-mocks (3.12.6) + diff-lcs (>= 1.2.0, < 2.0) + rspec-support (~> 3.12.0) + rspec-rails (6.0.3) + actionpack (>= 6.1) + activesupport (>= 6.1) + railties (>= 6.1) + rspec-core (~> 3.12) + rspec-expectations (~> 3.12) + rspec-mocks (~> 3.12) + rspec-support (~> 3.12) + rspec-support (3.12.1) + rubyzip (2.3.2) + selenium-webdriver (4.11.0) + rexml (~> 3.2, >= 3.2.5) + rubyzip (>= 1.2.2, < 3.0) + websocket (~> 1.0) + simplecov (0.22.0) + docile (~> 1.1) + simplecov-html (~> 0.11) + simplecov_json_formatter (~> 0.1) + simplecov-html (0.12.3) + simplecov_json_formatter (0.1.4) thor (1.2.2) tzinfo (2.0.6) concurrent-ruby (~> 1.0) + websocket (1.2.9) + xpath (3.2.0) + nokogiri (~> 1.8) zeitwerk (2.6.11) PLATFORMS @@ -77,8 +132,11 @@ DEPENDENCIES activemodel (= 7.0.7.2) activesupport (= 7.0.7.2) debug + govuk_test puma (~> 5.0) railties (= 7.0.7.2) + rspec-rails + simplecov RUBY VERSION ruby 3.2.2p53 diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb new file mode 100644 index 0000000..9b381b4 --- /dev/null +++ b/spec/spec_helper.rb @@ -0,0 +1,32 @@ +ENV["RAILS_ENV"] ||= "test" + +require "simplecov" +SimpleCov.start "rails" + +require File.expand_path("../../config/environment", __FILE__) +require "rspec/rails" + +Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f } +GovukTest.configure + +# See https://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration +RSpec.configure do |config| + config.default_formatter = "doc" if config.files_to_run.one? + config.disable_monkey_patching! + config.example_status_persistence_file_path = "spec/examples.txt" + config.expose_dsl_globally = false + config.infer_spec_type_from_file_location! + config.profile_examples = 10 + config.shared_context_metadata_behavior = :apply_to_host_groups # Preempting v4 default + + config.order = :random + Kernel.srand config.seed + + config.expect_with :rspec do |expectations| + expectations.include_chain_clauses_in_custom_matcher_descriptions = true # Preempting v4 default + end + + config.mock_with :rspec do |mocks| + mocks.verify_partial_doubles = true # Preempting v4 default + end +end