Skip to content

Commit

Permalink
Rails generate generator command create spec by default
Browse files Browse the repository at this point in the history
  • Loading branch information
benoittgt committed Jan 22, 2020
1 parent 55515e1 commit ca988b0
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 37 deletions.
14 changes: 1 addition & 13 deletions features/generator_specs/generator_specs.feature
Expand Up @@ -2,21 +2,9 @@ Feature: Generator spec

RSpec spec(s) can be generated when generating application components. For instance, `rails generate model` will also generate an RSpec spec file for the model but you can also write your own generator. See [customizing your workflow](https://guides.rubyonrails.org/generators.html#customizing-your-workflow)

Scenario: Use custom generator without '--generator-specs' arg
Scenario: Use custom generator
When I run `bundle exec rails generate generator my_generator`
Then the features should pass
Then the output should contain:
"""
create lib/generators/my_generator
create lib/generators/my_generator/my_generator_generator.rb
create lib/generators/my_generator/USAGE
create lib/generators/my_generator/templates
invoke rspec
"""

Scenario: Use custom generator with '--generator-specs' arg
When I run `bundle exec rails generate generator my_generator --generator-specs`
Then the features should pass
Then the output should contain:
"""
create lib/generators/my_generator
Expand Down
2 changes: 1 addition & 1 deletion lib/generators/rspec/generator/generator_generator.rb
Expand Up @@ -4,7 +4,7 @@ module Rspec
module Generators
# @private
class GeneratorGenerator < Base
class_option :generator_specs, type: :boolean, default: false, desc: "Generate generator specs"
class_option :generator_specs, type: :boolean, default: true, desc: "Generate generator specs"

def generate_generator_spec
return unless options[:generator_specs]
Expand Down
33 changes: 10 additions & 23 deletions spec/generators/rspec/generator/generator_generator_spec.rb
Expand Up @@ -6,30 +6,17 @@

describe "generator specs" do
subject(:generator_spec) { file("spec/generator/posts_generator_spec.rb") }
describe "are generated independently/can be generated" do
before do
run_generator %w(posts --generator-specs)
end
it "creates the spec file" 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)}/)
end
before do
run_generator %w(posts)
end

describe "are not generated/are skipped by default" do
before do
run_generator %w(posts)
end
describe "the spec" do
it "does not exist" do
expect(generator_spec).to_not exist
end
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)}/)
end
end
end

0 comments on commit ca988b0

Please sign in to comment.