From af72f6343b56c3cf7b6fb90aab9f6da60d68bd15 Mon Sep 17 00:00:00 2001 From: Jon Rowe Date: Mon, 4 Dec 2023 21:49:51 +0000 Subject: [PATCH] Remove usage of ammeter exist matcher --- .../rspec/channel/channel_generator_spec.rb | 10 +- .../controller/controller_generator_spec.rb | 146 +++-- .../rspec/feature/feature_generator_spec.rb | 30 +- .../generator/generator_generator_spec.rb | 11 +- .../rspec/helper/helper_generator_spec.rb | 15 +- .../rspec/install/install_generator_spec.rb | 2 +- .../rspec/job/job_generator_spec.rb | 17 +- .../rspec/mailbox/mailbox_generator_spec.rb | 10 +- .../rspec/mailer/mailer_generator_spec.rb | 70 ++- .../rspec/model/model_generator_spec.rb | 6 +- .../rspec/scaffold/scaffold_generator_spec.rb | 531 +++++++++++------- .../rspec/system/system_generator_spec.rb | 15 +- spec/support/generators.rb | 64 +-- 13 files changed, 551 insertions(+), 376 deletions(-) diff --git a/spec/generators/rspec/channel/channel_generator_spec.rb b/spec/generators/rspec/channel/channel_generator_spec.rb index 96c5deaada..03c2f79256 100644 --- a/spec/generators/rspec/channel/channel_generator_spec.rb +++ b/spec/generators/rspec/channel/channel_generator_spec.rb @@ -5,13 +5,11 @@ RSpec.describe Rspec::Generators::ChannelGenerator, type: :generator, skip: !RSpec::Rails::FeatureCheck.has_action_cable_testing? do setup_default_destination - describe 'the generated files' do - before { run_generator %w[chat] } + before { run_generator %w[chat] } - subject { file("spec/channels/chat_channel_spec.rb") } + subject(:channel_spec) { file("spec/channels/chat_channel_spec.rb") } - it { is_expected.to exist } - it { is_expected.to contain(/require 'rails_helper'/) } - it { is_expected.to contain(/describe ChatChannel, #{type_metatag(:channel)}/) } + it "generates a channel spec file" do + expect(channel_spec).to contain(/require 'rails_helper'/).and(contain(/describe ChatChannel, #{type_metatag(:channel)}/)) end end diff --git a/spec/generators/rspec/controller/controller_generator_spec.rb b/spec/generators/rspec/controller/controller_generator_spec.rb index 3885194abf..3724c49e20 100644 --- a/spec/generators/rspec/controller/controller_generator_spec.rb +++ b/spec/generators/rspec/controller/controller_generator_spec.rb @@ -6,18 +6,17 @@ setup_default_destination describe 'request specs' do - subject { file('spec/requests/posts_spec.rb') } + subject(:filename) { file('spec/requests/posts_spec.rb') } describe 'generated by default' do before do run_generator %w[posts] end - describe 'the spec' do - it { is_expected.to exist } - it { is_expected.to contain(/require 'rails_helper'/) } - it { is_expected.to contain(/^RSpec.describe "Posts", #{type_metatag(:request)}/) } - it { is_expected.to contain('pending') } + it 'includes the standard boilerplate' do + expect(filename).to contain(/require 'rails_helper'/) + .and(contain(/^RSpec.describe "Posts", #{type_metatag(:request)}/)) + .and(contain('pending')) end end @@ -25,7 +24,10 @@ before do run_generator %w[posts --no-request_specs] end - it { is_expected.not_to exist } + + it 'skips the file' do + expect(File.exist?(filename)).to be false + end end describe 'with actions' do @@ -33,22 +35,24 @@ run_generator %w[posts index custom_action] end - it { is_expected.to exist } - it { is_expected.to contain('get "/posts/index"') } - it { is_expected.to contain('get "/posts/custom_action"') } + it 'includes the standard boilerplate' do + expect(filename).to contain('get "/posts/index"') + .and(contain('get "/posts/custom_action"')) + end end describe 'with namespace and actions' do - subject { file('spec/requests/admin/external/users_spec.rb') } + subject(:filename) { file('spec/requests/admin/external/users_spec.rb') } before do run_generator %w[admin::external::users index custom_action] end - it { is_expected.to exist } - it { is_expected.to contain(/^RSpec.describe "Admin::External::Users", #{type_metatag(:request)}/) } - it { is_expected.to contain('get "/admin/external/users/index"') } - it { is_expected.to contain('get "/admin/external/users/custom_action"') } + it 'includes the standard boilerplate' do + expect(filename).to contain(/^RSpec.describe "Admin::External::Users", #{type_metatag(:request)}/) + .and(contain('get "/admin/external/users/index"')) + .and(contain('get "/admin/external/users/custom_action"')) + end end end @@ -58,18 +62,27 @@ before do run_generator %w[posts index show --no-view-specs] end + describe 'index.html.erb' do - subject { file('spec/views/posts/index.html.erb_spec.rb') } - it { is_expected.not_to exist } + subject(:filename) { file('spec/views/posts/index.html.erb_spec.rb') } + + it 'skips the file' do + expect(File.exist?(filename)).to be false + end end end + describe 'with no actions' do before do run_generator %w[posts] end + describe 'index.html.erb' do - subject { file('spec/views/posts/index.html.erb_spec.rb') } - it { is_expected.not_to exist } + subject(:filename) { file('spec/views/posts/index.html.erb_spec.rb') } + + it 'skips the file' do + expect(File.exist?(filename)).to be false + end end end @@ -79,8 +92,11 @@ end describe 'index.html.erb' do - subject { file('spec/views/posts/index.html._spec.rb') } - it { is_expected.not_to exist } + subject(:filename) { file('spec/views/posts/index.html._spec.rb') } + + it 'skips the file' do + expect(File.exist?(filename)).to be false + end end end end @@ -90,46 +106,62 @@ before do run_generator %w[posts index show] end + describe 'index.html.erb' do - subject { file('spec/views/posts/index.html.erb_spec.rb') } - it { is_expected.to exist } - it { is_expected.to contain(/require 'rails_helper'/) } - it { is_expected.to contain(/^RSpec.describe "posts\/index.html.erb", #{type_metatag(:view)}/) } + subject(:filename) { file('spec/views/posts/index.html.erb_spec.rb') } + + it 'includes the standard boilerplate' do + expect(filename).to contain(/require 'rails_helper'/) + .and(contain(/^RSpec.describe "posts\/index.html.erb", #{type_metatag(:view)}/)) + end end + describe 'show.html.erb' do - subject { file('spec/views/posts/show.html.erb_spec.rb') } - it { is_expected.to exist } - it { is_expected.to contain(/require 'rails_helper'/) } - it { is_expected.to contain(/^RSpec.describe "posts\/show.html.erb", #{type_metatag(:view)}/) } + subject(:filename) { file('spec/views/posts/show.html.erb_spec.rb') } + + it 'includes the standard boilerplate' do + expect(filename).to contain(/require 'rails_helper'/) + .and(contain(/^RSpec.describe "posts\/show.html.erb", #{type_metatag(:view)}/)) + end end end + describe 'with haml' do before do run_generator %w[posts index --template_engine haml] end + describe 'index.html.haml' do - subject { file('spec/views/posts/index.html.haml_spec.rb') } - it { is_expected.to exist } - it { is_expected.to contain(/require 'rails_helper'/) } - it { is_expected.to contain(/^RSpec.describe "posts\/index.html.haml", #{type_metatag(:view)}/) } + subject(:filename) { file('spec/views/posts/index.html.haml_spec.rb') } + + it 'includes the standard boilerplate' do + expect(filename).to contain(/require 'rails_helper'/) + .and(contain(/^RSpec.describe "posts\/index.html.haml", #{type_metatag(:view)}/)) + end end end end describe 'are removed' do - subject { run_generator %w[posts], behavior: :revoke } - it { is_expected.to match('remove spec/views/posts') } + subject(:output) { run_generator %w[posts], behavior: :revoke } + + it 'will remove the file' do + expect(output).to match('remove spec/views/posts') + end end end describe 'routing spec' do - subject { file('spec/routing/posts_routing_spec.rb') } + subject(:filename) { file('spec/routing/posts_routing_spec.rb') } describe 'with no flag' do before do run_generator %w[posts seek and destroy] end - it { is_expected.not_to exist } + + it 'skips the file' do + expect(File.exist?(filename)).to be false + end end describe 'with --routing-specs flag' do @@ -137,17 +169,22 @@ before do run_generator %w[posts --routing-specs] end - it { is_expected.not_to exist } + + it 'skips the file' do + expect(File.exist?(filename)).to be false + end end describe 'with action parameter' do before { run_generator %w[posts seek --routing-specs] } - it { is_expected.to contain(/require 'rails_helper'/) } - it { is_expected.to contain(/^RSpec.describe 'PostsController', #{type_metatag(:routing)}/) } - it { is_expected.to contain(/describe 'routing'/) } - it { is_expected.to contain(/it 'routes to #seek'/) } - it { is_expected.to contain(/expect\(get: "\/posts\/seek"\).to route_to\("posts#seek"\)/) } + it 'includes the standard boilerplate' do + expect(filename).to contain(/require 'rails_helper'/) + .and(contain(/^RSpec.describe 'PostsController', #{type_metatag(:routing)}/)) + .and(contain(/describe 'routing'/)) + .and(contain(/it 'routes to #seek'/)) + .and(contain(/expect\(get: "\/posts\/seek"\).to route_to\("posts#seek"\)/)) + end end end @@ -155,15 +192,18 @@ before do run_generator %w[posts seek and destroy --no-routing_specs] end - it { is_expected.not_to exist } + + it 'skips the file' do + expect(File.exist?(filename)).to be false + end end end describe 'controller specs' do - subject { file('spec/controllers/posts_controller_spec.rb') } + subject(:filename) { file('spec/controllers/posts_controller_spec.rb') } - describe 'are not generated' do - it { is_expected.not_to exist } + it 'are not generated' do + expect(File.exist?(filename)).to be false end describe 'with --controller-specs flag' do @@ -172,9 +212,10 @@ end describe 'the spec' do - it { is_expected.to exist } - it { is_expected.to contain(/require 'rails_helper'/) } - it { is_expected.to contain(/^RSpec.describe PostsController, #{type_metatag(:controller)}/) } + it 'includes the standard boilerplate' do + expect(filename).to contain(/require 'rails_helper'/) + .and(contain(/^RSpec.describe PostsController, #{type_metatag(:controller)}/)) + end end end @@ -182,7 +223,10 @@ before do run_generator %w[posts --no-controller-specs] end - it { is_expected.not_to exist } + + it 'are skipped' do + expect(File.exist?(filename)).to be false + end end end end diff --git a/spec/generators/rspec/feature/feature_generator_spec.rb b/spec/generators/rspec/feature/feature_generator_spec.rb index 352bf76179..7149c1e1b8 100644 --- a/spec/generators/rspec/feature/feature_generator_spec.rb +++ b/spec/generators/rspec/feature/feature_generator_spec.rb @@ -10,16 +10,14 @@ before do run_generator %w[posts] end + describe 'the spec' do subject(:feature_spec) { file('spec/features/posts_spec.rb') } - it "exists" do - expect(feature_spec).to exist - end - it "contains 'rails_helper'" do - expect(feature_spec).to contain(/require 'rails_helper'/) - end - it "contains the feature" do - expect(feature_spec).to contain(/^RSpec.feature "Posts", #{type_metatag(:feature)}/) + + it 'includes the standard boilerplate' do + expect( + feature_spec + ).to contain(/require 'rails_helper'/).and(contain(/^RSpec.feature "Posts", #{type_metatag(:feature)}/)) end end end @@ -28,12 +26,11 @@ before do run_generator %w[folder/posts] end + describe 'the spec' do subject(:feature_spec) { file('spec/features/folder/posts_spec.rb') } - it "exists" do - expect(feature_spec).to exist - end - it "contains the feature" do + + it 'includes the standard boilerplate' do expect(feature_spec).to contain(/^RSpec.feature "Folder::Posts", #{type_metatag(:feature)}/) end end @@ -43,11 +40,10 @@ before do run_generator %w[posts --singularize] end + describe 'the spec' do subject(:feature_spec) { file('spec/features/post_spec.rb') } - it "exists with the appropriate filename" do - expect(feature_spec).to exist - end + it "contains the singularized feature" do expect(feature_spec).to contain(/^RSpec.feature "Post", #{type_metatag(:feature)}/) end @@ -58,10 +54,12 @@ before do run_generator %w[posts --no-feature-specs] end + describe "the spec" do subject(:feature_spec) { file('spec/features/posts_spec.rb') } + it "does not exist" do - expect(feature_spec).to_not exist + expect(File.exist?(feature_spec)).to be false end end end diff --git a/spec/generators/rspec/generator/generator_generator_spec.rb b/spec/generators/rspec/generator/generator_generator_spec.rb index 2d416e0bba..79ea4b51b8 100644 --- a/spec/generators/rspec/generator/generator_generator_spec.rb +++ b/spec/generators/rspec/generator/generator_generator_spec.rb @@ -9,14 +9,9 @@ before do run_generator %w[posts] end - it "creates the spec file by default" do - expect(generator_spec).to exist - end - it "contains 'rails_helper in the spec file'" do - expect(generator_spec).to contain(/require 'rails_helper'/) - end - it "includes the generator type in the metadata" do - expect(generator_spec).to contain(/^RSpec.describe "Posts", #{type_metatag(:generator)}/) + + it "include the standard boilerplate" do + expect(generator_spec).to contain(/require 'rails_helper'/).and(contain(/^RSpec.describe "Posts", #{type_metatag(:generator)}/)) end end end diff --git a/spec/generators/rspec/helper/helper_generator_spec.rb b/spec/generators/rspec/helper/helper_generator_spec.rb index c628891c6d..271bb35b7b 100644 --- a/spec/generators/rspec/helper/helper_generator_spec.rb +++ b/spec/generators/rspec/helper/helper_generator_spec.rb @@ -5,22 +5,25 @@ RSpec.describe Rspec::Generators::HelperGenerator, type: :generator do setup_default_destination - subject { file('spec/helpers/posts_helper_spec.rb') } + subject(:helper_spec) { file('spec/helpers/posts_helper_spec.rb') } + describe 'generated by default' do before do run_generator %w[posts] end - describe 'the spec' do - it { is_expected.to exist } - it { is_expected.to contain(/require 'rails_helper'/) } - it { is_expected.to contain(/^RSpec.describe PostsHelper, #{type_metatag(:helper)}/) } + it 'includes the standard boilerplate' do + expect(helper_spec).to contain(/require 'rails_helper'/).and(contain(/^RSpec.describe PostsHelper, #{type_metatag(:helper)}/)) end end + describe 'skipped with a flag' do before do run_generator %w[posts --no-helper_specs] end - it { is_expected.not_to exist } + + it 'does not create the helper spec' do + expect(File.exist?(helper_spec)).to be false + end end end diff --git a/spec/generators/rspec/install/install_generator_spec.rb b/spec/generators/rspec/install/install_generator_spec.rb index ec4a6db846..65b4244271 100644 --- a/spec/generators/rspec/install/install_generator_spec.rb +++ b/spec/generators/rspec/install/install_generator_spec.rb @@ -55,7 +55,7 @@ def filter_rails_from_backtrace it "generates .rspec" do run_generator - expect(file('.rspec')).to exist + expect(File.exist?(file('.rspec'))).to be true end it "generates spec/spec_helper.rb" do diff --git a/spec/generators/rspec/job/job_generator_spec.rb b/spec/generators/rspec/job/job_generator_spec.rb index 529fa91db3..98cb5ed5d0 100644 --- a/spec/generators/rspec/job/job_generator_spec.rb +++ b/spec/generators/rspec/job/job_generator_spec.rb @@ -8,23 +8,22 @@ describe 'the generated files' do before { run_generator [file_name] } + subject(:job_spec) { file('spec/jobs/user_job_spec.rb') } + context 'with file_name without job as suffix' do let(:file_name) { 'user' } - subject { file('spec/jobs/user_job_spec.rb') } - it { is_expected.to exist } - it { is_expected.to contain(/require 'rails_helper'/) } - it { is_expected.to contain(/describe UserJob, #{type_metatag(:job)}/) } + it 'creates the standard boiler plate' do + expect(job_spec).to contain(/require 'rails_helper'/).and(contain(/describe UserJob, #{type_metatag(:job)}/)) + end end context 'with file_name with job as suffix' do let(:file_name) { 'user_job' } - subject { file('spec/jobs/user_job_spec.rb') } - - it { is_expected.to exist } - it { is_expected.to contain(/require 'rails_helper'/) } - it { is_expected.to contain(/describe UserJob, #{type_metatag(:job)}/) } + it 'creates the standard boiler plate' do + expect(job_spec).to contain(/require 'rails_helper'/).and(contain(/describe UserJob, #{type_metatag(:job)}/)) + end end end end diff --git a/spec/generators/rspec/mailbox/mailbox_generator_spec.rb b/spec/generators/rspec/mailbox/mailbox_generator_spec.rb index cee01ad39f..74e72d37fa 100644 --- a/spec/generators/rspec/mailbox/mailbox_generator_spec.rb +++ b/spec/generators/rspec/mailbox/mailbox_generator_spec.rb @@ -8,10 +8,12 @@ describe 'the generated files' do before { run_generator %w[forwards] } - subject { file('spec/mailboxes/forwards_mailbox_spec.rb') } + subject(:mailbox_spec) { file('spec/mailboxes/forwards_mailbox_spec.rb') } - it { is_expected.to exist } - it { is_expected.to contain(/require 'rails_helper'/) } - it { is_expected.to contain(/describe ForwardsMailbox, #{type_metatag(:mailbox)}/) } + it 'generates the file' do + expect( + mailbox_spec + ).to contain(/require 'rails_helper'/).and contain(/describe ForwardsMailbox, #{type_metatag(:mailbox)}/) + end end end diff --git a/spec/generators/rspec/mailer/mailer_generator_spec.rb b/spec/generators/rspec/mailer/mailer_generator_spec.rb index 7c15e6184f..1dea0bc055 100644 --- a/spec/generators/rspec/mailer/mailer_generator_spec.rb +++ b/spec/generators/rspec/mailer/mailer_generator_spec.rb @@ -6,25 +6,36 @@ setup_default_destination describe 'mailer spec' do - subject { file('spec/mailers/posts_spec.rb') } + subject(:filename) { file('spec/mailers/posts_spec.rb') } + describe 'a spec is created for each action' do before do run_generator %w[posts index show] end - it { is_expected.to exist } - it { is_expected.to contain(/require "rails_helper"/) } - # Rails 5+ automatically appends Mailer to the provided constant so we do too - it { is_expected.to contain(/^RSpec.describe PostsMailer, #{type_metatag(:mailer)}/) } - it { is_expected.to contain(/describe "index" do/) } - it { is_expected.to contain(/describe "show" do/) } + + it "includes the standard boilerplate" do + # Rails 5+ automatically appends Mailer to the provided constant so we do too + expect( + filename + ).to( + contain(/require "rails_helper"/) + .and(contain(/^RSpec.describe PostsMailer, #{type_metatag(:mailer)}/)) + .and(contain(/describe "index" do/)) + .and(contain(/describe "show" do/)) + ) + end end + describe 'creates placeholder when no actions specified' do before do run_generator %w[posts] end - it { is_expected.to exist } - it { is_expected.to contain(/require "rails_helper"/) } - it { is_expected.to contain(/pending "add some examples to \(or delete\)/) } + + it "includes the standard boilerplate" do + expect( + filename + ).to contain(/require "rails_helper"/).and(contain(/pending "add some examples to \(or delete\)/)) + end end end @@ -32,28 +43,41 @@ before do run_generator %w[posts index show] end + describe 'index' do - subject { file('spec/fixtures/posts/index') } - it { is_expected.to exist } - it { is_expected.to contain(/Posts#index/) } + subject(:filename) { file('spec/fixtures/posts/index') } + + it "includes the standard boilerplate" do + expect(filename).to contain(/Posts#index/) + end end + describe 'show' do - subject { file('spec/fixtures/posts/show') } - it { is_expected.to exist } - it { is_expected.to contain(/Posts#show/) } + subject(:filename) { file('spec/fixtures/posts/show') } + + it "includes the standard boilerplate" do + expect(filename).to contain(/Posts#show/) + end end end describe 'a preview is generated for each action', skip: !RSpec::Rails::FeatureCheck.has_action_mailer_preview? do - subject { file('spec/mailers/previews/posts_preview.rb') } before do run_generator %w[posts index show] end - it { is_expected.to exist } - it { is_expected.to contain(/class PostsPreview < ActionMailer::Preview/) } - it { is_expected.to contain(/def index/) } - it { is_expected.to contain(/PostsMailer.index/) } - it { is_expected.to contain(/def show/) } - it { is_expected.to contain(/PostsMailer.show/) } + + subject(:filename) { file('spec/mailers/previews/posts_preview.rb') } + + it "includes the standard boilerplate" do + expect( + filename + ).to( + contain(/class PostsPreview < ActionMailer::Preview/) + .and(contain(/def index/)) + .and(contain(/PostsMailer.index/)) + .and(contain(/def show/)) + .and(contain(/PostsMailer.show/)) + ) + end end end diff --git a/spec/generators/rspec/model/model_generator_spec.rb b/spec/generators/rspec/model/model_generator_spec.rb index 521170774a..5aacf721c4 100644 --- a/spec/generators/rspec/model/model_generator_spec.rb +++ b/spec/generators/rspec/model/model_generator_spec.rb @@ -22,9 +22,9 @@ end describe 'the fixtures' do - subject { file('spec/fixtures/posts.yml') } - - it { is_expected.not_to exist } + it "will skip the file" do + expect(File.exist?(file('spec/fixtures/posts.yml'))).to be false + end end end end diff --git a/spec/generators/rspec/scaffold/scaffold_generator_spec.rb b/spec/generators/rspec/scaffold/scaffold_generator_spec.rb index 4ca4c4f705..edbc239346 100644 --- a/spec/generators/rspec/scaffold/scaffold_generator_spec.rb +++ b/spec/generators/rspec/scaffold/scaffold_generator_spec.rb @@ -8,46 +8,70 @@ setup_default_destination describe 'standard request specs' do - subject { file('spec/requests/posts_spec.rb') } + subject(:filename) { file('spec/requests/posts_spec.rb') } describe 'with no options' do before { run_generator %w[posts --request_specs] } - it { is_expected.to exist } - it { is_expected.to contain("require 'rails_helper'") } - it { is_expected.to contain(/^RSpec.describe "\/posts", #{type_metatag(:request)}/) } - it { is_expected.to contain('GET /new') } - it { is_expected.to contain(/"redirects to the created post"/) } - it { is_expected.to contain('get post_url(post)') } - it { is_expected.to contain('redirect_to(post_url(Post.last))') } - it { is_expected.to contain(/"redirects to the \w+ list"/) } - - if ::Rails::VERSION::STRING >= '7.0.0' - it { is_expected.to contain(/renders a response with 422 status \(i.e. to display the 'new' template\)/) } - it { is_expected.to contain(/renders a response with 422 status \(i.e. to display the 'edit' template\)/) } - else - it { is_expected.to contain(/renders a successful response \(i.e. to display the 'new' template\)/) } - it { is_expected.to contain(/renders a successful response \(i.e. to display the 'edit' template\)/) } + + it "includes the standard boilerplate" do + expect( + filename + ).to( + contain("require 'rails_helper'") + .and(contain(/^RSpec.describe "\/posts", #{type_metatag(:request)}/)) + .and(contain('GET /new')) + .and(contain(/"redirects to the created post"/)) + .and(contain('get post_url(post)')) + .and(contain('redirect_to(post_url(Post.last))')) + .and(contain(/"redirects to the \w+ list"/)) + ) + + if ::Rails::VERSION::STRING >= '7.0.0' + expect( + filename + ).to( + contain(/renders a response with 422 status \(i.e. to display the 'new' template\)/) + .and(contain(/renders a response with 422 status \(i.e. to display the 'edit' template\)/)) + ) + else + expect( + filename + ).to( + contain(/renders a successful response \(i.e. to display the 'new' template\)/) + .and(contain(/renders a successful response \(i.e. to display the 'edit' template\)/)) + ) + end end end describe 'with --no-request_specs' do before { run_generator %w[posts --no-request_specs] } - it { is_expected.not_to exist } + + it "is skipped" do + expect(File.exist?(filename)).to be false + end end describe 'with --api' do before { run_generator %w[posts --api] } - it { is_expected.to exist } - it { is_expected.to contain(/require 'rails_helper'/) } - it { is_expected.to contain(/^RSpec.describe "\/posts", #{type_metatag(:request)}/) } - it { is_expected.to contain('as: :json') } - it { is_expected.not_to contain('get new_posts_path') } - it { is_expected.not_to contain(/"redirects to\w+"/) } - it { is_expected.to contain('renders a JSON response with the new post') } - it { is_expected.to contain('renders a JSON response with errors for the new post') } - it { is_expected.not_to contain('get edit_posts_path') } - it { is_expected.to contain('renders a JSON response with the post') } - it { is_expected.to contain('renders a JSON response with errors for the post') } + + it "includes the standard boilerplate" do + expect( + filename + ).to( + contain(/require 'rails_helper'/) + .and(contain(/^RSpec.describe "\/posts", #{type_metatag(:request)}/)) + .and(contain('as: :json')) + .and(contain('renders a JSON response with the new post')) + .and(contain('renders a JSON response with errors for the new post')) + .and(contain('renders a JSON response with the post')) + .and(contain('renders a JSON response with errors for the post')) + ) + + expect(filename).not_to contain('get new_posts_path') + expect(filename).not_to contain(/"redirects to\w+"/) + expect(filename).not_to contain('get edit_posts_path') + end end describe 'in an engine' do @@ -55,130 +79,168 @@ in_sub_process do allow_any_instance_of(::Rails::Generators::NamedBase).to receive(:mountable_engine?).and_return(true) run_generator %w[posts --request_specs] - is_expected.to contain('Engine.routes.url_helpers') + + expect(filename).to contain('Engine.routes.url_helpers') end end end end describe 'standard controller spec' do - subject { file('spec/controllers/posts_controller_spec.rb') } + subject(:filename) { file('spec/controllers/posts_controller_spec.rb') } describe 'with --controller_specs' do before { run_generator %w[posts --controller_specs] } - it { is_expected.to contain(/require 'rails_helper'/) } - it { is_expected.to contain(/^RSpec.describe PostsController, #{type_metatag(:controller)}/) } - it { is_expected.to contain(/GET #new/) } - it { is_expected.to contain(/"redirects to the created \w+"/) } - if ::Rails::VERSION::STRING >= '7.0.0' - it { is_expected.to contain(/renders a response with 422 status \(i.e. to display the 'new' template\)/) } - else - it { is_expected.to contain(/returns a success response \(i.e. to display the 'new' template\)/) } - end - it { is_expected.not_to contain(/"renders a JSON response with the new \w+"/) } - it { is_expected.not_to contain(/"renders a JSON response with errors for the new \w+"/) } - - it { is_expected.to contain(/GET #edit/) } - it { is_expected.to contain(/"redirects to the \w+"/) } - if ::Rails::VERSION::STRING >= '7.0.0' - it { is_expected.to contain(/renders a response with 422 status \(i.e. to display the 'edit' template\)/) } - else - it { is_expected.to contain(/returns a success response \(i.e. to display the 'edit' template\)/) } - end - it { is_expected.not_to contain(/"renders a JSON response with the \w+"/) } - it { is_expected.not_to contain(/"renders a JSON response with errors for the \w+"/) } - - it { is_expected.to contain(/"redirects to the \w+ list"/) } + + it "includes the standard boilerplate" do + expect( + filename + ).to( + contain(/require 'rails_helper'/) + .and(contain(/^RSpec.describe PostsController, #{type_metatag(:controller)}/)) + .and(contain(/GET #new/)) + .and(contain(/"redirects to the created \w+"/)) + .and(contain(/GET #edit/)) + .and(contain(/"redirects to the \w+"/)) + .and(contain(/"redirects to the \w+ list"/)) + ) + + if ::Rails::VERSION::STRING >= '7.0.0' + expect(filename).to contain(/renders a response with 422 status \(i.e. to display the 'new' template\)/) + .and(contain(/renders a response with 422 status \(i.e. to display the 'edit' template\)/)) + else + expect(filename).to contain(/returns a success response \(i.e. to display the 'new' template\)/) + .and(contain(/returns a success response \(i.e. to display the 'edit' template\)/)) + end + + expect(filename).not_to contain(/"renders a JSON response with the new \w+"/) + expect(filename).not_to contain(/"renders a JSON response with errors for the new \w+"/) + expect(filename).not_to contain(/"renders a JSON response with the \w+"/) + expect(filename).not_to contain(/"renders a JSON response with errors for the \w+"/) + end end describe 'with no options' do before { run_generator %w[posts] } - it { is_expected.not_to exist } + + it 'skips the file' do + expect(File.exist?(filename)).to be false + end end describe 'with --api' do before { run_generator %w[posts --controller_specs --api] } - it { is_expected.to contain(/require 'rails_helper'/) } - it { is_expected.to contain(/^RSpec.describe PostsController, #{type_metatag(:controller)}/) } - it { is_expected.not_to contain(/GET #new/) } - it { is_expected.not_to contain(/"redirects to the created \w+"/) } - it { is_expected.not_to contain(/display the 'new' template/) } - it { is_expected.to contain(/"renders a JSON response with the new \w+"/) } - it { is_expected.to contain(/"renders a JSON response with errors for the new \w+"/) } - it { is_expected.not_to contain(/GET #edit/) } - it { is_expected.not_to contain(/"redirects to the \w+"/) } - it { is_expected.not_to contain(/display the 'edit' template/) } - it { is_expected.to contain(/"renders a JSON response with the \w+"/) } - it { is_expected.to contain(/"renders a JSON response with errors for the \w+"/) } - - it { is_expected.not_to contain(/"redirects to the \w+ list"/) } + + it "includes the standard boilerplate" do + expect(filename).to contain(/require 'rails_helper'/) + .and(contain(/^RSpec.describe PostsController, #{type_metatag(:controller)}/)) + .and(contain(/"renders a JSON response with the new \w+"/)) + .and(contain(/"renders a JSON response with errors for the new \w+"/)) + .and(contain(/"renders a JSON response with the \w+"/)) + .and(contain(/"renders a JSON response with errors for the \w+"/)) + + expect(filename).not_to contain(/GET #new/) + expect(filename).not_to contain(/"redirects to the created \w+"/) + expect(filename).not_to contain(/display the 'new' template/) + expect(filename).not_to contain(/GET #edit/) + expect(filename).not_to contain(/"redirects to the \w+"/) + expect(filename).not_to contain(/display the 'edit' template/) + expect(filename).not_to contain(/"redirects to the \w+ list"/) + end end end describe 'namespaced request spec' do - subject { file('spec/requests/admin/posts_spec.rb') } + subject(:filename) { file('spec/requests/admin/posts_spec.rb') } describe 'with default options' do before { run_generator %w[admin/posts] } - it { is_expected.to exist } - it { is_expected.to contain(/^RSpec.describe "\/admin\/posts", #{type_metatag(:request)}/) } - it { is_expected.to contain('post admin_posts_url, params: { admin_post: valid_attributes }') } - it { is_expected.to contain('admin_post_url(post)') } - it { is_expected.to contain('Admin::Post.create') } + + it "includes the standard boilerplate" do + expect(filename).to contain(/^RSpec.describe "\/admin\/posts", #{type_metatag(:request)}/) + .and(contain('post admin_posts_url, params: { admin_post: valid_attributes }')) + .and(contain('admin_post_url(post)')) + .and(contain('Admin::Post.create')) + end end describe 'with --model-name' do before { run_generator %w[admin/posts --model-name=post] } - it { is_expected.to contain('post admin_posts_url, params: { post: valid_attributes }') } - it { is_expected.not_to contain('params: { admin_post: valid_attributes }') } - it { is_expected.to contain(' Post.create') } + + it "includes the standard boilerplate" do + expect(filename).to contain('post admin_posts_url, params: { post: valid_attributes }') + .and(contain(' Post.create')) + + expect(filename).not_to contain('params: { admin_post: valid_attributes }') + end end context 'with --api' do describe 'with default options' do before { run_generator %w[admin/posts --api] } - it { is_expected.to contain('params: { admin_post: valid_attributes }') } - it { is_expected.to contain('Admin::Post.create') } + + it "includes the standard boilerplate" do + expect(filename).to contain('params: { admin_post: valid_attributes }') + .and(contain('Admin::Post.create')) + end end describe 'with --model-name' do before { run_generator %w[admin/posts --api --model-name=post] } - it { is_expected.to contain('params: { post: valid_attributes }') } - it { is_expected.not_to contain('params: { admin_post: valid_attributes }') } - it { is_expected.to contain(' Post.create') } + + it "includes the standard boilerplate" do + expect(filename).to contain('params: { post: valid_attributes }') + .and(contain(' Post.create')) + + expect(filename).not_to contain('params: { admin_post: valid_attributes }') + end end end end describe 'namespaced controller spec' do - subject { file('spec/controllers/admin/posts_controller_spec.rb') } + subject(:filename) { file('spec/controllers/admin/posts_controller_spec.rb') } describe 'with default options' do before { run_generator %w[admin/posts --controller_specs] } - it { is_expected.to contain(/^RSpec.describe Admin::PostsController, #{type_metatag(:controller)}/) } - it { is_expected.to contain('post :create, params: {admin_post: valid_attributes}') } - it { is_expected.to contain('Admin::Post.create') } + + it "includes the standard boilerplate" do + expect(filename).to contain(/^RSpec.describe Admin::PostsController, #{type_metatag(:controller)}/) + .and(contain('post :create, params: {admin_post: valid_attributes}')) + .and(contain('Admin::Post.create')) + end end describe 'with --model-name' do before { run_generator %w[admin/posts --model-name=post --controller_specs] } - it { is_expected.to contain('post :create, params: {post: valid_attributes}') } - it { is_expected.not_to contain('params: {admin_post: valid_attributes}') } - it { is_expected.to contain(' Post.create') } + + it "includes the standard boilerplate" do + expect(filename).to contain('post :create, params: {post: valid_attributes}') + .and(contain(' Post.create')) + + expect(filename).not_to contain('params: {admin_post: valid_attributes}') + end end context 'with --api' do describe 'with default options' do before { run_generator %w[admin/posts --api --controller_specs] } - it { is_expected.to contain('post :create, params: {admin_post: valid_attributes}') } - it { is_expected.to contain('Admin::Post.create') } + + it "includes the standard boilerplate" do + expect(filename).to contain('post :create, params: {admin_post: valid_attributes}') + .and(contain('Admin::Post.create')) + end end describe 'with --model-name' do before { run_generator %w[admin/posts --api --model-name=post --controller_specs] } - it { is_expected.to contain('post :create, params: {post: valid_attributes}') } - it { is_expected.not_to contain('params: {admin_post: valid_attributes}') } - it { is_expected.to contain(' Post.create') } + + it "includes the standard boilerplate" do + expect(filename).to contain('post :create, params: {post: valid_attributes}') + .and(contain(' Post.create')) + + expect(filename).not_to contain('params: {admin_post: valid_attributes}') + end end end end @@ -188,70 +250,89 @@ before { run_generator %w[posts] } describe 'edit' do - subject { file("spec/views/posts/edit.html.erb_spec.rb") } - it { is_expected.to exist } - it { is_expected.to contain(/require 'rails_helper'/) } - it { is_expected.to contain(/^RSpec.describe "(.*)\/edit", #{type_metatag(:view)}/) } - it { is_expected.to contain(/assign\(:post, post\)/) } - it { is_expected.to contain(/it "renders the edit (.*) form"/) } + subject(:filename) { file("spec/views/posts/edit.html.erb_spec.rb") } + + it "includes the standard boilerplate" do + expect(filename).to contain(/require 'rails_helper'/) + .and(contain(/^RSpec.describe "(.*)\/edit", #{type_metatag(:view)}/)) + .and(contain(/assign\(:post, post\)/)) + .and(contain(/it "renders the edit (.*) form"/)) + end end describe 'index' do - subject { file("spec/views/posts/index.html.erb_spec.rb") } - it { is_expected.to exist } - it { is_expected.to contain(/require 'rails_helper'/) } - it { is_expected.to contain(/^RSpec.describe "(.*)\/index", #{type_metatag(:view)}/) } - it { is_expected.to contain(/assign\(:posts, /) } - it { is_expected.to contain(/it "renders a list of (.*)"/) } + subject(:filename) { file("spec/views/posts/index.html.erb_spec.rb") } + + it "includes the standard boilerplate" do + expect(filename).to contain(/require 'rails_helper'/) + .and(contain(/^RSpec.describe "(.*)\/index", #{type_metatag(:view)}/)) + .and(contain(/assign\(:posts, /)) + .and(contain(/it "renders a list of (.*)"/)) + end end describe 'new' do - subject { file("spec/views/posts/new.html.erb_spec.rb") } - it { is_expected.to exist } - it { is_expected.to contain(/require 'rails_helper'/) } - it { is_expected.to contain(/^RSpec.describe "(.*)\/new", #{type_metatag(:view)}/) } - it { is_expected.to contain(/assign\(:post, /) } - it { is_expected.to contain(/it "renders new (.*) form"/) } + subject(:filename) { file("spec/views/posts/new.html.erb_spec.rb") } + + it "includes the standard boilerplate" do + expect(filename).to contain(/require 'rails_helper'/) + .and(contain(/^RSpec.describe "(.*)\/new", #{type_metatag(:view)}/)) + .and(contain(/assign\(:post, /)) + .and(contain(/it "renders new (.*) form"/)) + end end describe 'show' do - subject { file("spec/views/posts/show.html.erb_spec.rb") } - it { is_expected.to exist } - it { is_expected.to contain(/require 'rails_helper'/) } - it { is_expected.to contain(/^RSpec.describe "(.*)\/show", #{type_metatag(:view)}/) } - it { is_expected.to contain(/assign\(:post, /) } - it { is_expected.to contain(/it "renders attributes in

"/) } + subject(:filename) { file("spec/views/posts/show.html.erb_spec.rb") } + + it "includes the standard boilerplate" do + expect(filename).to contain(/require 'rails_helper'/) + .and(contain(/^RSpec.describe "(.*)\/show", #{type_metatag(:view)}/)) + .and(contain(/assign\(:post, /)) + .and(contain(/it "renders attributes in

"/)) + end end end describe 'with multiple integer attributes index' do before { run_generator %w[posts upvotes:integer downvotes:integer] } - subject { file("spec/views/posts/index.html.erb_spec.rb") } - it { is_expected.to exist } - it { is_expected.to contain('assert_select cell_selector, text: Regexp.new(2.to_s), count: 2') } - it { is_expected.to contain('assert_select cell_selector, text: Regexp.new(3.to_s), count: 2') } + subject(:filename) { file("spec/views/posts/index.html.erb_spec.rb") } + + it "includes the standard boilerplate" do + expect(filename).to contain('assert_select cell_selector, text: Regexp.new(2.to_s), count: 2') + .and(contain('assert_select cell_selector, text: Regexp.new(3.to_s), count: 2')) + end end describe 'with multiple float attributes index' do before { run_generator %w[posts upvotes:float downvotes:float] } - subject { file("spec/views/posts/index.html.erb_spec.rb") } - it { is_expected.to exist } - it { is_expected.to contain('assert_select cell_selector, text: Regexp.new(2.5.to_s), count: 2') } - it { is_expected.to contain('assert_select cell_selector, text: Regexp.new(3.5.to_s), count: 2') } + subject(:filename) { file("spec/views/posts/index.html.erb_spec.rb") } + + it "includes the standard boilerplate" do + expect(filename).to contain('assert_select cell_selector, text: Regexp.new(2.5.to_s), count: 2') + .and(contain('assert_select cell_selector, text: Regexp.new(3.5.to_s), count: 2')) + end end describe 'with reference attribute' do before { run_generator %w[posts title:string author:references] } + describe 'edit' do - subject { file("spec/views/posts/edit.html.erb_spec.rb") } - it { is_expected.to contain(/assert_select "input\[name=\?\]", "post\[author_id\]/) } - it { is_expected.to contain(/assert_select "input\[name=\?\]", "post\[title\]/) } + subject(:filename) { file("spec/views/posts/edit.html.erb_spec.rb") } + + it "includes the standard boilerplate" do + expect(filename).to contain(/assert_select "input\[name=\?\]", "post\[author_id\]/) + .and(contain(/assert_select "input\[name=\?\]", "post\[title\]/)) + end end describe 'new' do - subject { file("spec/views/posts/new.html.erb_spec.rb") } - it { is_expected.to contain(/assert_select "input\[name=\?\]", "post\[author_id\]"/) } - it { is_expected.to contain(/assert_select "input\[name=\?\]", "post\[title\]/) } + subject(:filename) { file("spec/views/posts/new.html.erb_spec.rb") } + + it "includes the standard boilerplate" do + expect(filename).to contain(/assert_select "input\[name=\?\]", "post\[author_id\]"/) + .and(contain(/assert_select "input\[name=\?\]", "post\[title\]/)) + end end end @@ -259,27 +340,35 @@ before { run_generator %w[admin/posts] } describe 'edit' do - subject { file("spec/views/admin/posts/edit.html.erb_spec.rb") } - it { is_expected.to exist } - it { is_expected.to contain(/assign\(:admin_post, admin_post\)/) } + subject(:filename) { file("spec/views/admin/posts/edit.html.erb_spec.rb") } + + it "includes the standard boilerplate" do + expect(filename).to contain(/assign\(:admin_post, admin_post\)/) + end end describe 'index' do - subject { file("spec/views/admin/posts/index.html.erb_spec.rb") } - it { is_expected.to exist } - it { is_expected.to contain(/assign\(:admin_posts, /) } + subject(:filename) { file("spec/views/admin/posts/index.html.erb_spec.rb") } + + it "includes the standard boilerplate" do + expect(filename).to contain(/assign\(:admin_posts, /) + end end describe 'new' do - subject { file("spec/views/admin/posts/new.html.erb_spec.rb") } - it { is_expected.to exist } - it { is_expected.to contain(/assign\(:admin_post, /) } + subject(:filename) { file("spec/views/admin/posts/new.html.erb_spec.rb") } + + it "includes the standard boilerplate" do + expect(filename).to contain(/assign\(:admin_post, /) + end end describe 'show' do - subject { file("spec/views/admin/posts/show.html.erb_spec.rb") } - it { is_expected.to exist } - it { is_expected.to contain(/assign\(:admin_post, /) } + subject(:filename) { file("spec/views/admin/posts/show.html.erb_spec.rb") } + + it "includes the standard boilerplate" do + expect(filename).to contain(/assign\(:admin_post, /) + end end end @@ -287,50 +376,70 @@ before { run_generator %w[admin/posts --model-name=Post] } describe 'edit' do - subject { file("spec/views/admin/posts/edit.html.erb_spec.rb") } - it { is_expected.to exist } - it { is_expected.to contain(/assign\(:post, post\)/) } + subject(:filename) { file("spec/views/admin/posts/edit.html.erb_spec.rb") } + + it "includes the standard boilerplate" do + expect(filename).to contain(/assign\(:post, post\)/) + end end describe 'index' do - subject { file("spec/views/admin/posts/index.html.erb_spec.rb") } - it { is_expected.to exist } - it { is_expected.to contain(/assign\(:posts, /) } + subject(:filename) { file("spec/views/admin/posts/index.html.erb_spec.rb") } + + it "includes the standard boilerplate" do + expect(filename).to contain(/assign\(:posts, /) + end end describe 'new' do - subject { file("spec/views/admin/posts/new.html.erb_spec.rb") } - it { is_expected.to exist } - it { is_expected.to contain(/assign\(:post, /) } + subject(:filename) { file("spec/views/admin/posts/new.html.erb_spec.rb") } + + it "includes the standard boilerplate" do + expect(filename).to contain(/assign\(:post, /) + end end describe 'show' do - subject { file("spec/views/admin/posts/show.html.erb_spec.rb") } - it { is_expected.to exist } - it { is_expected.to contain(/assign\(:post, /) } + subject(:filename) { file("spec/views/admin/posts/show.html.erb_spec.rb") } + + it "includes the standard boilerplate" do + expect(filename).to contain(/assign\(:post, /) + end end end describe 'with --no-template-engine' do before { run_generator %w[posts --no-template-engine] } describe 'edit' do - subject { file("spec/views/posts/edit.html._spec.rb") } - it { is_expected.not_to exist } + subject(:filename) { file("spec/views/posts/edit.html._spec.rb") } + + it "skips the file" do + expect(File.exist?(filename)).to be false + end end describe 'index' do - subject { file("spec/views/posts/index.html._spec.rb") } - it { is_expected.not_to exist } + subject(:filename) { file("spec/views/posts/index.html._spec.rb") } + + it "skips the file" do + expect(File.exist?(filename)).to be false + end end describe 'new' do - subject { file("spec/views/posts/new.html._spec.rb") } - it { is_expected.not_to exist } + subject(:filename) { file("spec/views/posts/new.html._spec.rb") } + + it "skips the file" do + expect(File.exist?(filename)).to be false + end end describe 'show' do - subject { file("spec/views/posts/show.html._spec.rb") } - it { is_expected.not_to exist } + subject(:filename) { file("spec/views/posts/show.html._spec.rb") } + + it "skips the file" do + expect(File.exist?(filename)).to be false + end end end @@ -338,23 +447,35 @@ before { run_generator %w[posts --api] } describe 'edit' do - subject { file("spec/views/posts/edit.html.erb_spec.rb") } - it { is_expected.not_to exist } + subject(:filename) { file("spec/views/posts/edit.html.erb_spec.rb") } + + it "skips the file" do + expect(File.exist?(filename)).to be false + end end describe 'index' do - subject { file("spec/views/posts/index.html.erb_spec.rb") } - it { is_expected.not_to exist } + subject(:filename) { file("spec/views/posts/index.html.erb_spec.rb") } + + it "skips the file" do + expect(File.exist?(filename)).to be false + end end describe 'new' do - subject { file("spec/views/posts/index.html.erb_spec.rb") } - it { is_expected.not_to exist } + subject(:filename) { file("spec/views/posts/index.html.erb_spec.rb") } + + it "skips the file" do + expect(File.exist?(filename)).to be false + end end describe 'show' do - subject { file("spec/views/posts/index.html.erb_spec.rb") } - it { is_expected.not_to exist } + subject(:filename) { file("spec/views/posts/index.html.erb_spec.rb") } + + it "skips the file" do + expect(File.exist?(filename)).to be false + end end end @@ -362,58 +483,82 @@ before { run_generator %w[posts --no-view-specs] } describe 'edit' do - subject { file("spec/views/posts/edit.html.erb_spec.rb") } - it { is_expected.not_to exist } + subject(:filename) { file("spec/views/posts/edit.html.erb_spec.rb") } + + it "skips the file" do + expect(File.exist?(filename)).to be false + end end describe 'index' do - subject { file("spec/views/posts/index.html.erb_spec.rb") } - it { is_expected.not_to exist } + subject(:filename) { file("spec/views/posts/index.html.erb_spec.rb") } + + it "skips the file" do + expect(File.exist?(filename)).to be false + end end describe 'new' do - subject { file("spec/views/posts/new.html.erb_spec.rb") } - it { is_expected.not_to exist } + subject(:filename) { file("spec/views/posts/new.html.erb_spec.rb") } + + it "skips the file" do + expect(File.exist?(filename)).to be false + end end describe 'show' do - subject { file("spec/views/posts/show.html.erb_spec.rb") } - it { is_expected.not_to exist } + subject(:filename) { file("spec/views/posts/show.html.erb_spec.rb") } + + it "skips the file" do + expect(File.exist?(filename)).to be false + end end end end describe 'routing spec' do - subject { file('spec/routing/posts_routing_spec.rb') } + subject(:filename) { file('spec/routing/posts_routing_spec.rb') } describe 'with default options' do before { run_generator %w[posts] } - it { is_expected.to contain(/require "rails_helper"/) } - it { is_expected.to contain(/^RSpec.describe PostsController, #{type_metatag(:routing)}/) } - it { is_expected.to contain(/describe "routing"/) } - it { is_expected.to contain(/routes to #new/) } - it { is_expected.to contain(/routes to #edit/) } - it { is_expected.to contain('route_to("posts#new")') } + + it 'includes the standard boilerplate' do + expect(filename).to contain(/require "rails_helper"/) + .and(contain(/^RSpec.describe PostsController, #{type_metatag(:routing)}/)) + .and(contain(/describe "routing"/)) + .and(contain(/routes to #new/)) + .and(contain(/routes to #edit/)) + .and(contain('route_to("posts#new")')) + end end describe 'with --no-routing-specs' do before { run_generator %w[posts --no-routing_specs] } - it { is_expected.not_to exist } + + it "skips the file" do + expect(File.exist?(filename)).to be false + end end describe 'with --api' do before { run_generator %w[posts --api] } - it { is_expected.not_to contain(/routes to #new/) } - it { is_expected.not_to contain(/routes to #edit/) } + + it 'skips the right content' do + expect(filename).not_to contain(/routes to #new/) + expect(filename).not_to contain(/routes to #edit/) + end end context 'with a namespaced name' do - subject { file('spec/routing/api/v1/posts_routing_spec.rb') } + subject(:filename) { file('spec/routing/api/v1/posts_routing_spec.rb') } describe 'with default options' do before { run_generator %w[api/v1/posts] } - it { is_expected.to contain(/^RSpec.describe Api::V1::PostsController, #{type_metatag(:routing)}/) } - it { is_expected.to contain('route_to("api/v1/posts#new")') } + + it 'includes the standard boilerplate' do + expect(filename).to contain(/^RSpec.describe Api::V1::PostsController, #{type_metatag(:routing)}/) + .and(contain('route_to("api/v1/posts#new")')) + end end end end diff --git a/spec/generators/rspec/system/system_generator_spec.rb b/spec/generators/rspec/system/system_generator_spec.rb index 0d2fc35474..3aab0fb161 100644 --- a/spec/generators/rspec/system/system_generator_spec.rb +++ b/spec/generators/rspec/system/system_generator_spec.rb @@ -7,19 +7,15 @@ describe "system specs" do subject(:system_spec) { file("spec/system/posts_spec.rb") } + describe "are generated independently from the command line" do before do run_generator %w[posts] end + describe "the spec" do - it "exists" do - expect(system_spec).to exist - end - it "contains 'rails_helper'" do - expect(system_spec).to contain(/require 'rails_helper'/) - end - it "contains the system" do - expect(system_spec).to contain(/^RSpec.describe "Posts", #{type_metatag(:system)}/) + it "contains the standard boilerplate" do + expect(system_spec).to contain(/require 'rails_helper'/).and(contain(/^RSpec.describe "Posts", #{type_metatag(:system)}/)) end end end @@ -28,9 +24,10 @@ before do run_generator %w[posts --no-system-specs] end + describe "the spec" do it "does not exist" do - expect(system_spec).to_not exist + expect(File.exist?(system_spec)).to be false end end end diff --git a/spec/support/generators.rb b/spec/support/generators.rb index f57dc2b885..3f87aec9ea 100644 --- a/spec/support/generators.rb +++ b/spec/support/generators.rb @@ -24,17 +24,19 @@ def self.included(klass) before { run_generator [name, '--fixture'] } describe 'the spec' do - subject { file("spec/models/#{name}_spec.rb") } + subject(:model_spec) { file("spec/models/#{name}_spec.rb") } - it { is_expected.to exist } - it { is_expected.to contain(/require 'rails_helper'/) } - it { is_expected.to contain(/^RSpec.describe #{class_name}, #{type_metatag(:model)}/) } + it 'contains the standard boilerplate' do + expect(model_spec).to contain(/require 'rails_helper'/).and(contain(/^RSpec.describe #{class_name}, #{type_metatag(:model)}/)) + end end describe 'the fixtures' do - subject { file("spec/fixtures/#{name}.yml") } + subject(:filename) { file("spec/fixtures/#{name}.yml") } - it { is_expected.to contain(Regexp.new('# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html')) } + it 'contains the standard boilerplate' do + expect(filename).to contain(Regexp.new('# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html')) + end end end @@ -47,7 +49,7 @@ def self.included(klass) subject(:request_spec) { file('spec/requests/posts_spec.rb') } it "does not create the request spec" do - expect(request_spec).not_to exist + expect(File.exist?(request_spec)).to be false end end @@ -62,27 +64,11 @@ def self.included(klass) let(:name) { %w[posts] } let(:spec_file_name) { 'spec/requests/posts_spec.rb' } - it "creates the request spec" do - expect(request_spec).to exist - end - - it "the generator requires 'rails_helper'" do + it 'contains the standard boilerplate' do expect(request_spec).to contain(/require 'rails_helper'/) - end - - it "the generator describes the provided NAME without monkey " \ - "patching setting the type to `:request`" do - expect(request_spec).to contain( - /^RSpec.describe "Posts", #{type_metatag(:request)}/ - ) - end - - it "the generator includes a sample GET request" do - expect(request_spec).to contain(/describe "GET \/posts"/) - end - - it "the generator sends the GET request to the index path" do - expect(request_spec).to contain(/get posts_index_path/) + .and(contain(/^RSpec.describe "Posts", #{type_metatag(:request)}/)) + .and(contain(/describe "GET \/posts"/)) + .and(contain(/get posts_index_path/)) end end @@ -90,27 +76,11 @@ def self.included(klass) let(:name) { %w[api/posts] } let(:spec_file_name) { 'spec/requests/api/posts_spec.rb' } - it "creates the request spec" do - expect(request_spec).to exist - end - - it "the generator requires 'rails_helper'" do + it 'contains the standard boilerplate' do expect(request_spec).to contain(/require 'rails_helper'/) - end - - it "the generator describes the provided NAME without monkey " \ - "patching setting the type to `:request`" do - expect(request_spec).to contain( - /^RSpec.describe "Api::Posts", #{type_metatag(:request)}/ - ) - end - - it "the generator includes a sample GET request" do - expect(request_spec).to contain(/describe "GET \/api\/posts"/) - end - - it "the generator sends the GET request to the index path" do - expect(request_spec).to contain(/get api_posts_index_path\n/) + .and(contain(/^RSpec.describe "Api::Posts", #{type_metatag(:request)}/)) + .and(contain(/describe "GET \/api\/posts"/)) + .and(contain(/get api_posts_index_path\n/)) end end end