Skip to content

Commit

Permalink
Merge pull request #2719 from rspec/refactor-generator-specs
Browse files Browse the repository at this point in the history
  • Loading branch information
JonRowe committed Dec 4, 2023
2 parents b109337 + af72f63 commit 4e87304
Show file tree
Hide file tree
Showing 13 changed files with 551 additions and 376 deletions.
10 changes: 4 additions & 6 deletions spec/generators/rspec/channel/channel_generator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
146 changes: 95 additions & 51 deletions spec/generators/rspec/controller/controller_generator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,49 +6,53 @@
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

describe 'skipped with a flag' do
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
before do
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

Expand All @@ -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

Expand All @@ -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
Expand All @@ -90,80 +106,104 @@
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
describe 'without action parameter' do
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

describe 'with --no-routing-specs flag' do
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
Expand All @@ -172,17 +212,21 @@
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

describe 'with --no-controller_specs flag' do
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
30 changes: 14 additions & 16 deletions spec/generators/rspec/feature/feature_generator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
11 changes: 3 additions & 8 deletions spec/generators/rspec/generator/generator_generator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Loading

0 comments on commit 4e87304

Please sign in to comment.