Skip to content

Commit e692909

Browse files
committed
Merge pull request #1378 from rspec/add-request-generator
Add request generator
2 parents 479d2d0 + 33f42fc commit e692909

File tree

17 files changed

+135
-94
lines changed

17 files changed

+135
-94
lines changed

Changelog.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ Enhancements:
1010
(Gabe Martin-Dempesy, #1361)
1111
* Include `RSpec::Rails::FixtureSupport` into example groups using metadata
1212
`:use_fixtures => true`. (Aaron Kromer, #1372)
13+
* Include `rspec:request` generator for generating request specs; this is an
14+
alias of `rspec:integration` (Aaron Kromer, #1378)
1315

1416
Bug Fixes:
1517

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

spec/generators/rspec/controller/controller_generator_spec.rb

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
1-
require 'spec_helper'
2-
31
# Generators are not automatically loaded by Rails
42
require 'generators/rspec/controller/controller_generator'
3+
require 'support/generators'
54

6-
describe Rspec::Generators::ControllerGenerator, :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 }
5+
RSpec.describe Rspec::Generators::ControllerGenerator, :type => :generator do
6+
setup_default_destination
117

128
describe 'controller specs' do
139
subject { file('spec/controllers/posts_controller_spec.rb') }

spec/generators/rspec/feature/feature_generator_spec.rb

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
1-
require 'spec_helper'
2-
31
# Generators are not automatically loaded by rails
42
require 'generators/rspec/feature/feature_generator'
3+
require 'support/generators'
54

6-
describe Rspec::Generators::FeatureGenerator, :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 }
5+
RSpec.describe Rspec::Generators::FeatureGenerator, :type => :generator do
6+
setup_default_destination
117

128
describe 'feature specs' do
139
describe 'are generated independently from the command line' do

spec/generators/rspec/helper/helper_generator_spec.rb

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
1-
require 'spec_helper'
2-
31
# Generators are not automatically loaded by Rails
42
require 'generators/rspec/helper/helper_generator'
3+
require 'support/generators'
54

6-
describe Rspec::Generators::HelperGenerator, :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 }
5+
RSpec.describe Rspec::Generators::HelperGenerator, :type => :generator do
6+
setup_default_destination
117

128
subject { file('spec/helpers/posts_helper_spec.rb') }
139
describe 'generated by default' do

spec/generators/rspec/install/install_generator_spec.rb

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
require 'spec_helper'
1+
# Generators are not automatically loaded by Rails
22
require 'generators/rspec/install/install_generator'
3+
require 'support/generators'
34

45
RSpec.describe Rspec::Generators::InstallGenerator, :type => :generator do
5-
destination File.expand_path("../../../../../tmp", __FILE__)
6-
76
def use_active_record_migration
87
match(/ActiveRecord::Migration\./m)
98
end
@@ -32,7 +31,7 @@ def use_transactional_fixtures
3231
match(/config\.use_transactional_fixtures/m)
3332
end
3433

35-
before { prepare_destination }
34+
setup_default_destination
3635

3736
let(:rails_helper) { content_for('spec/rails_helper.rb') }
3837
let(:spec_helper) { content_for('spec/spec_helper.rb') }
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

spec/generators/rspec/job/job_generator_spec.rb

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
1-
require 'spec_helper'
2-
31
# Generators are not automatically loaded by Rails
42
require 'generators/rspec/job/job_generator'
3+
require 'support/generators'
54

65
RSpec.describe Rspec::Generators::JobGenerator, :type => :generator, :skip => !RSpec::Rails::FeatureCheck.has_active_job? 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 }
6+
setup_default_destination
117

128
describe 'the generated files' do
139
before { run_generator %w(user) }

spec/generators/rspec/mailer/mailer_generator_spec.rb

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
1-
require 'spec_helper'
2-
31
# Generators are not automatically loaded by Rails
42
require 'generators/rspec/mailer/mailer_generator'
3+
require 'support/generators'
54

6-
describe Rspec::Generators::MailerGenerator, :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 }
5+
RSpec.describe Rspec::Generators::MailerGenerator, :type => :generator do
6+
setup_default_destination
117

128
describe 'mailer spec' do
139
subject { file('spec/mailers/posts_spec.rb') }

spec/generators/rspec/model/model_generator_spec.rb

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
1-
require 'spec_helper'
2-
31
# Generators are not automatically loaded by Rails
42
require 'generators/rspec/model/model_generator'
3+
require 'support/generators'
54

6-
describe Rspec::Generators::ModelGenerator, :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 }
5+
RSpec.describe Rspec::Generators::ModelGenerator, :type => :generator do
6+
setup_default_destination
117

128
it 'runs both the model and fixture tasks' do
139
gen = generator %w(posts)

spec/generators/rspec/observer/observer_generator_spec.rb

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
1-
require 'spec_helper'
2-
31
# Generators are not automatically loaded by Rails
42
require 'generators/rspec/observer/observer_generator'
3+
require 'support/generators'
54

6-
describe Rspec::Generators::ObserverGenerator, :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__)
5+
RSpec.describe Rspec::Generators::ObserverGenerator, :type => :generator do
6+
setup_default_destination
97

108
subject { file('spec/models/posts_observer_spec.rb') }
9+
1110
before do
12-
prepare_destination
1311
run_generator %w(posts)
1412
end
1513

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/generators/rspec/scaffold/scaffold_generator_spec.rb

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
require 'spec_helper'
2-
1+
# Generators are not automatically loaded by Rails
32
require 'generators/rspec/scaffold/scaffold_generator'
3+
require 'support/generators'
44

5-
describe Rspec::Generators::ScaffoldGenerator, :type => :generator do
6-
destination File.expand_path("../../../../../tmp", __FILE__)
7-
8-
before { prepare_destination }
5+
RSpec.describe Rspec::Generators::ScaffoldGenerator, :type => :generator do
6+
setup_default_destination
97

108
describe 'standard controller spec' do
119
subject { file('spec/controllers/posts_controller_spec.rb') }

spec/generators/rspec/view/view_generator_spec.rb

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
1-
require 'spec_helper'
2-
31
# Generators are not automatically loaded by Rails
42
require 'generators/rspec/view/view_generator'
3+
require 'support/generators'
54

6-
describe Rspec::Generators::ViewGenerator, :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 }
5+
RSpec.describe Rspec::Generators::ViewGenerator, :type => :generator do
6+
setup_default_destination
117

128
describe 'with default template engine' do
139
it 'generates a spec for the supplied action' do

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)