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) }

0 commit comments

Comments
 (0)