Skip to content

Commit 66bb6a7

Browse files
committed
Include rspec:request generator.
This includes a generator for "request" specs. There already exists a generator which creates these specs, but it is called "integration". The existing name comes from the legacy implementations attempting to mirror the Rails test style names for the RSpec equivalent specs. Since these two generators are identical, except for the name, this makes the new `RequestGenerator` a subclass of the existing `IntegrationGenerator`. Alternatively, we could have decided to make `RequestGenerator` the parent class, moving the existing functionality into it. While this is arguably the "proper" way of thinking about the generators, it could potentially break some existing applications; even though these classes are marked `private`. There's no harm for now in keeping semver compliant by making this generator additive. Since the two generators are exactly the same this uses a shared example to keep the specs in-sync. Resolve #1377
1 parent 479d2d0 commit 66bb6a7

File tree

6 files changed

+102
-30
lines changed

6 files changed

+102
-30
lines changed

example_app_generator/generate_stuff.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ def using_source_path(path)
8282
generate('rspec:install')
8383
generate('controller wombats index') # plural
8484
generate('controller welcome index') # singular
85+
generate('rspec:request wombats')
8586
generate('integration_test widgets')
8687
generate('mailer Notifications signup')
8788

lib/generators/rspec/integration/integration_generator.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,12 @@ module Rspec
44
module Generators
55
# @private
66
class IntegrationGenerator < Base
7-
class_option :request_specs, :type => :boolean, :default => true, :desc => "Generate request specs"
7+
# Add a deprecation for this class, before rspec-rails 4, to use the
8+
# `RequestGenerator` instead
9+
class_option :request_specs,
10+
:type => :boolean,
11+
:default => true,
12+
:desc => "Generate request specs"
813

914
def generate_request_spec
1015
return unless options[:request_specs]
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
require 'generators/rspec/integration/integration_generator'
2+
3+
module Rspec
4+
module Generators
5+
# @private
6+
class RequestGenerator < IntegrationGenerator
7+
source_paths << File.expand_path("../../integration/templates", __FILE__)
8+
end
9+
end
10+
end
Lines changed: 4 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,8 @@
1-
require 'spec_helper'
2-
31
# Generators are not automatically loaded by Rails
42
require 'generators/rspec/integration/integration_generator'
3+
require 'support/generators'
54

6-
describe Rspec::Generators::IntegrationGenerator, :type => :generator do
7-
# Tell the generator where to put its output (what it thinks of as Rails.root)
8-
destination File.expand_path("../../../../../tmp", __FILE__)
9-
10-
before { prepare_destination }
11-
12-
describe 'are not generated' do
13-
before do
14-
run_generator %w(posts --no-request-specs)
15-
end
16-
describe 'index.html.erb' do
17-
subject { file('spec/requests/posts_spec.rb') }
18-
it { is_expected.not_to exist }
19-
end
20-
end
21-
22-
describe 'are generated' do
23-
before do
24-
run_generator %w(posts)
25-
end
26-
subject { file('spec/requests/posts_spec.rb') }
27-
it { is_expected.to exist }
28-
it { is_expected.to contain(/require 'rails_helper'/) }
29-
it { is_expected.to contain(/^RSpec.describe \"Posts\", #{type_metatag(:request)}/) }
30-
it { is_expected.to contain(/describe "GET \/posts"/) }
31-
it { is_expected.to contain(/get posts_index_path/) }
32-
end
5+
RSpec.describe Rspec::Generators::IntegrationGenerator, :type => :generator do
6+
setup_default_destination
7+
it_behaves_like "a request spec generator"
338
end
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Generators are not automatically loaded by Rails
2+
require 'generators/rspec/request/request_generator'
3+
require 'support/generators'
4+
5+
RSpec.describe Rspec::Generators::RequestGenerator, :type => :generator do
6+
setup_default_destination
7+
it_behaves_like "a request spec generator"
8+
end

spec/support/generators.rb

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
module RSpec
2+
module Rails
3+
module Specs
4+
module Generators
5+
module Macros
6+
# Tell the generator where to put its output (what it thinks of as
7+
# Rails.root)
8+
def set_default_destination
9+
destination File.expand_path("../../../tmp", __FILE__)
10+
end
11+
12+
def setup_default_destination
13+
set_default_destination
14+
before { prepare_destination }
15+
end
16+
end
17+
18+
def self.included(klass)
19+
klass.extend(Macros)
20+
end
21+
22+
shared_examples_for "a request spec generator" do
23+
describe 'generated with flag `--no-request-specs`' do
24+
before do
25+
run_generator %w(posts --no-request-specs)
26+
end
27+
28+
subject(:request_spec) { file('spec/requests/posts_spec.rb') }
29+
30+
it "does not create the request spec" do
31+
expect(request_spec).not_to exist
32+
end
33+
end
34+
35+
describe 'generated with no flags' do
36+
before do
37+
run_generator %w(posts)
38+
end
39+
40+
subject(:request_spec) { file('spec/requests/posts_spec.rb') }
41+
42+
it "creates the request spec" do
43+
expect(request_spec).to exist
44+
end
45+
46+
it "the generator requires 'rails_helper'" do
47+
expect(request_spec).to contain(/require 'rails_helper'/)
48+
end
49+
50+
it "the generator describes the provided NAME without monkey " \
51+
"patching setting the type to `:request`" do
52+
expect(request_spec).to contain(
53+
/^RSpec.describe \"Posts\", #{type_metatag(:request)}/
54+
)
55+
end
56+
57+
it "the generator includes a sample GET request" do
58+
expect(request_spec).to contain(/describe "GET \/posts"/)
59+
end
60+
61+
it "the generator sends the GET request to the index path" do
62+
expect(request_spec).to contain(/get posts_index_path/)
63+
end
64+
end
65+
end
66+
end
67+
end
68+
end
69+
end
70+
71+
RSpec.configure do |config|
72+
config.include RSpec::Rails::Specs::Generators, :type => :generator
73+
end

0 commit comments

Comments
 (0)