Skip to content

Commit 7d4ed77

Browse files
ConSouJonRowe
authored andcommitted
Add generator spec generator and specs (#2085)
Added generator spec generator
1 parent 7720e7b commit 7d4ed77

File tree

3 files changed

+65
-0
lines changed

3 files changed

+65
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
require 'generators/rspec'
2+
3+
module Rspec
4+
module Generators
5+
# @private
6+
class GeneratorsGenerator < Base
7+
class_option :generator_specs, :type => :boolean, :default => false, :desc => "Generate generator specs"
8+
9+
def generate_generator_spec
10+
return unless options[:generator_specs]
11+
12+
template template_name, File.join('spec/generator', class_path, filename)
13+
end
14+
15+
def template_name
16+
'generator_spec.rb'
17+
end
18+
19+
def filename
20+
"#{table_name}_generator_spec.rb"
21+
end
22+
end
23+
end
24+
end
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
require 'rails_helper'
2+
3+
RSpec.describe "<%= class_name.pluralize %>", <%= type_metatag(:generator) %> do
4+
5+
pending "add some scenarios (or delete) #{__FILE__}"
6+
end
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
require 'generators/rspec/generators/generator_generator'
2+
require 'support/generators'
3+
4+
RSpec.describe Rspec::Generators::GeneratorsGenerator, :type => :generator do
5+
setup_default_destination
6+
7+
describe "generator specs" do
8+
subject(:generator_spec) { file("spec/generator/posts_generator_spec.rb") }
9+
describe "are generated independently/can be generated" do
10+
before do
11+
run_generator %w(posts --generator-specs)
12+
end
13+
it "creates the spec file" do
14+
expect(generator_spec).to exist
15+
end
16+
it "contains 'rails_helper in the spec file'" do
17+
expect(generator_spec).to contain(/require 'rails_helper'/)
18+
end
19+
it "includes the generator type in the metadata" do
20+
expect(generator_spec).to contain(/^RSpec.describe \"Posts\", #{type_metatag(:generator)}/)
21+
end
22+
end
23+
24+
describe "are not generated/are skipped by default" do
25+
before do
26+
run_generator %w(posts)
27+
end
28+
describe "the spec" do
29+
it "does not exist" do
30+
expect(generator_spec).to_not exist
31+
end
32+
end
33+
end
34+
end
35+
end

0 commit comments

Comments
 (0)