Skip to content

Add request generator #1378

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 25, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ Enhancements:
(Gabe Martin-Dempesy, #1361)
* Include `RSpec::Rails::FixtureSupport` into example groups using metadata
`:use_fixtures => true`. (Aaron Kromer, #1372)
* Include `rspec:request` generator for generating request specs; this is an
alias of `rspec:integration` (Aaron Kromer, #1378)

Bug Fixes:

Expand Down
1 change: 1 addition & 0 deletions example_app_generator/generate_stuff.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ def using_source_path(path)
generate('rspec:install')
generate('controller wombats index') # plural
generate('controller welcome index') # singular
generate('rspec:request wombats')
generate('integration_test widgets')
generate('mailer Notifications signup')

Expand Down
7 changes: 6 additions & 1 deletion lib/generators/rspec/integration/integration_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@ module Rspec
module Generators
# @private
class IntegrationGenerator < Base
class_option :request_specs, :type => :boolean, :default => true, :desc => "Generate request specs"
# Add a deprecation for this class, before rspec-rails 4, to use the
# `RequestGenerator` instead
class_option :request_specs,
:type => :boolean,
:default => true,
:desc => "Generate request specs"

def generate_request_spec
return unless options[:request_specs]
Expand Down
10 changes: 10 additions & 0 deletions lib/generators/rspec/request/request_generator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
require 'generators/rspec/integration/integration_generator'

module Rspec
module Generators
# @private
class RequestGenerator < IntegrationGenerator
source_paths << File.expand_path("../../integration/templates", __FILE__)
end
end
end
10 changes: 3 additions & 7 deletions spec/generators/rspec/controller/controller_generator_spec.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
require 'spec_helper'

# Generators are not automatically loaded by Rails
require 'generators/rspec/controller/controller_generator'
require 'support/generators'

describe Rspec::Generators::ControllerGenerator, :type => :generator do
# Tell the generator where to put its output (what it thinks of as Rails.root)
destination File.expand_path("../../../../../tmp", __FILE__)

before { prepare_destination }
RSpec.describe Rspec::Generators::ControllerGenerator, :type => :generator do
setup_default_destination

describe 'controller specs' do
subject { file('spec/controllers/posts_controller_spec.rb') }
Expand Down
10 changes: 3 additions & 7 deletions spec/generators/rspec/feature/feature_generator_spec.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
require 'spec_helper'

# Generators are not automatically loaded by rails
require 'generators/rspec/feature/feature_generator'
require 'support/generators'

describe Rspec::Generators::FeatureGenerator, :type => :generator do
# Tell the generator where to put its output (what it thinks of as Rails.root)
destination File.expand_path('../../../../../tmp', __FILE__)

before { prepare_destination }
RSpec.describe Rspec::Generators::FeatureGenerator, :type => :generator do
setup_default_destination

describe 'feature specs' do
describe 'are generated independently from the command line' do
Expand Down
10 changes: 3 additions & 7 deletions spec/generators/rspec/helper/helper_generator_spec.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
require 'spec_helper'

# Generators are not automatically loaded by Rails
require 'generators/rspec/helper/helper_generator'
require 'support/generators'

describe Rspec::Generators::HelperGenerator, :type => :generator do
# Tell the generator where to put its output (what it thinks of as Rails.root)
destination File.expand_path("../../../../../tmp", __FILE__)

before { prepare_destination }
RSpec.describe Rspec::Generators::HelperGenerator, :type => :generator do
setup_default_destination

subject { file('spec/helpers/posts_helper_spec.rb') }
describe 'generated by default' do
Expand Down
7 changes: 3 additions & 4 deletions spec/generators/rspec/install/install_generator_spec.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
require 'spec_helper'
# Generators are not automatically loaded by Rails
require 'generators/rspec/install/install_generator'
require 'support/generators'

RSpec.describe Rspec::Generators::InstallGenerator, :type => :generator do
destination File.expand_path("../../../../../tmp", __FILE__)

def use_active_record_migration
match(/ActiveRecord::Migration\./m)
end
Expand Down Expand Up @@ -32,7 +31,7 @@ def use_transactional_fixtures
match(/config\.use_transactional_fixtures/m)
end

before { prepare_destination }
setup_default_destination

let(:rails_helper) { content_for('spec/rails_helper.rb') }
let(:spec_helper) { content_for('spec/spec_helper.rb') }
Expand Down
33 changes: 4 additions & 29 deletions spec/generators/rspec/integration/integration_generator_spec.rb
Original file line number Diff line number Diff line change
@@ -1,33 +1,8 @@
require 'spec_helper'

# Generators are not automatically loaded by Rails
require 'generators/rspec/integration/integration_generator'
require 'support/generators'

describe Rspec::Generators::IntegrationGenerator, :type => :generator do
# Tell the generator where to put its output (what it thinks of as Rails.root)
destination File.expand_path("../../../../../tmp", __FILE__)

before { prepare_destination }

describe 'are not generated' do
before do
run_generator %w(posts --no-request-specs)
end
describe 'index.html.erb' do
subject { file('spec/requests/posts_spec.rb') }
it { is_expected.not_to exist }
end
end

describe 'are generated' do
before do
run_generator %w(posts)
end
subject { file('spec/requests/posts_spec.rb') }
it { is_expected.to exist }
it { is_expected.to contain(/require 'rails_helper'/) }
it { is_expected.to contain(/^RSpec.describe \"Posts\", #{type_metatag(:request)}/) }
it { is_expected.to contain(/describe "GET \/posts"/) }
it { is_expected.to contain(/get posts_index_path/) }
end
RSpec.describe Rspec::Generators::IntegrationGenerator, :type => :generator do
setup_default_destination
it_behaves_like "a request spec generator"
end
8 changes: 2 additions & 6 deletions spec/generators/rspec/job/job_generator_spec.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
require 'spec_helper'

# Generators are not automatically loaded by Rails
require 'generators/rspec/job/job_generator'
require 'support/generators'

RSpec.describe Rspec::Generators::JobGenerator, :type => :generator, :skip => !RSpec::Rails::FeatureCheck.has_active_job? do
# Tell the generator where to put its output (what it thinks of as Rails.root)
destination File.expand_path('../../../../../tmp', __FILE__)

before { prepare_destination }
setup_default_destination

describe 'the generated files' do
before { run_generator %w(user) }
Expand Down
10 changes: 3 additions & 7 deletions spec/generators/rspec/mailer/mailer_generator_spec.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
require 'spec_helper'

# Generators are not automatically loaded by Rails
require 'generators/rspec/mailer/mailer_generator'
require 'support/generators'

describe Rspec::Generators::MailerGenerator, :type => :generator do
# Tell the generator where to put its output (what it thinks of as Rails.root)
destination File.expand_path("../../../../../tmp", __FILE__)

before { prepare_destination }
RSpec.describe Rspec::Generators::MailerGenerator, :type => :generator do
setup_default_destination

describe 'mailer spec' do
subject { file('spec/mailers/posts_spec.rb') }
Expand Down
10 changes: 3 additions & 7 deletions spec/generators/rspec/model/model_generator_spec.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
require 'spec_helper'

# Generators are not automatically loaded by Rails
require 'generators/rspec/model/model_generator'
require 'support/generators'

describe Rspec::Generators::ModelGenerator, :type => :generator do
# Tell the generator where to put its output (what it thinks of as Rails.root)
destination File.expand_path("../../../../../tmp", __FILE__)

before { prepare_destination }
RSpec.describe Rspec::Generators::ModelGenerator, :type => :generator do
setup_default_destination

it 'runs both the model and fixture tasks' do
gen = generator %w(posts)
Expand Down
10 changes: 4 additions & 6 deletions spec/generators/rspec/observer/observer_generator_spec.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
require 'spec_helper'

# Generators are not automatically loaded by Rails
require 'generators/rspec/observer/observer_generator'
require 'support/generators'

describe Rspec::Generators::ObserverGenerator, :type => :generator do
# Tell the generator where to put its output (what it thinks of as Rails.root)
destination File.expand_path("../../../../../tmp", __FILE__)
RSpec.describe Rspec::Generators::ObserverGenerator, :type => :generator do
setup_default_destination

subject { file('spec/models/posts_observer_spec.rb') }

before do
prepare_destination
run_generator %w(posts)
end

Expand Down
8 changes: 8 additions & 0 deletions spec/generators/rspec/request/request_generator_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Generators are not automatically loaded by Rails
require 'generators/rspec/request/request_generator'
require 'support/generators'

RSpec.describe Rspec::Generators::RequestGenerator, :type => :generator do
setup_default_destination
it_behaves_like "a request spec generator"
end
10 changes: 4 additions & 6 deletions spec/generators/rspec/scaffold/scaffold_generator_spec.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
require 'spec_helper'

# Generators are not automatically loaded by Rails
require 'generators/rspec/scaffold/scaffold_generator'
require 'support/generators'

describe Rspec::Generators::ScaffoldGenerator, :type => :generator do
destination File.expand_path("../../../../../tmp", __FILE__)

before { prepare_destination }
RSpec.describe Rspec::Generators::ScaffoldGenerator, :type => :generator do
setup_default_destination

describe 'standard controller spec' do
subject { file('spec/controllers/posts_controller_spec.rb') }
Expand Down
10 changes: 3 additions & 7 deletions spec/generators/rspec/view/view_generator_spec.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
require 'spec_helper'

# Generators are not automatically loaded by Rails
require 'generators/rspec/view/view_generator'
require 'support/generators'

describe Rspec::Generators::ViewGenerator, :type => :generator do
# Tell the generator where to put its output (what it thinks of as Rails.root)
destination File.expand_path("../../../../../tmp", __FILE__)

before { prepare_destination }
RSpec.describe Rspec::Generators::ViewGenerator, :type => :generator do
setup_default_destination

describe 'with default template engine' do
it 'generates a spec for the supplied action' do
Expand Down
73 changes: 73 additions & 0 deletions spec/support/generators.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
module RSpec
module Rails
module Specs
module Generators
module Macros
# Tell the generator where to put its output (what it thinks of as
# Rails.root)
def set_default_destination
destination File.expand_path("../../../tmp", __FILE__)
end

def setup_default_destination
set_default_destination
before { prepare_destination }
end
end

def self.included(klass)
klass.extend(Macros)
end

shared_examples_for "a request spec generator" do
describe 'generated with flag `--no-request-specs`' do
before do
run_generator %w(posts --no-request-specs)
end

subject(:request_spec) { file('spec/requests/posts_spec.rb') }

it "does not create the request spec" do
expect(request_spec).not_to exist
end
end

describe 'generated with no flags' do
before do
run_generator %w(posts)
end

subject(:request_spec) { file('spec/requests/posts_spec.rb') }

it "creates the request spec" do
expect(request_spec).to exist
end

it "the generator requires 'rails_helper'" do
expect(request_spec).to contain(/require 'rails_helper'/)
end

it "the generator describes the provided NAME without monkey " \
"patching setting the type to `:request`" do
expect(request_spec).to contain(
/^RSpec.describe \"Posts\", #{type_metatag(:request)}/
)
end

it "the generator includes a sample GET request" do
expect(request_spec).to contain(/describe "GET \/posts"/)
end

it "the generator sends the GET request to the index path" do
expect(request_spec).to contain(/get posts_index_path/)
end
end
end
end
end
end
end

RSpec.configure do |config|
config.include RSpec::Rails::Specs::Generators, :type => :generator
end