Skip to content

Commit

Permalink
Merge pull request #2134 from 00dav00/routing_specs_for_controller_ge…
Browse files Browse the repository at this point in the history
…nerator

Create controller generator for routing specs
  • Loading branch information
JonRowe authored Jun 8, 2019
2 parents 5780b19 + 666751f commit 9c6d8f3
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 2 deletions.
13 changes: 11 additions & 2 deletions lib/generators/rspec/controller/controller_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ class ControllerGenerator < Base
argument :actions, :type => :array, :default => [], :banner => "action action"

class_option :template_engine, :desc => "Template engine to generate view files"
class_option :controller_specs, :type => :boolean, :default => true
class_option :view_specs, :type => :boolean, :default => true
class_option :controller_specs, :type => :boolean, :default => true, :desc => "Generate controller specs"
class_option :view_specs, :type => :boolean, :default => true, :desc => "Generate view specs"
class_option :routing_specs, :type => :boolean, :default => false, :desc => "Generate routing specs"

def generate_controller_spec
return unless options[:controller_specs]
Expand All @@ -29,6 +30,14 @@ def generate_view_specs
File.join("spec", "views", file_path, "#{@action}.html.#{options[:template_engine]}_spec.rb")
end
end

def generate_routing_spec
return if actions.empty?
return unless options[:routing_specs]

template 'routing_spec.rb',
File.join('spec/routing', class_path, "#{file_name}_routing_spec.rb")
end
end
end
end
13 changes: 13 additions & 0 deletions lib/generators/rspec/controller/templates/routing_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
require 'rails_helper'

<% module_namespacing do -%>
RSpec.describe '<%= class_name %>Controller', <%= type_metatag(:routing) %> do
describe 'routing' do
<% for action in actions -%>
it 'routes to #<%= action %>' do
expect(:get => "/<%= class_name.underscore %>/<%= action %>").to route_to("<%= class_name.underscore %>#<%= action %>")
end
<% end -%>
end
end
<% end -%>
37 changes: 37 additions & 0 deletions spec/generators/rspec/controller/controller_generator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,41 @@
end
end
end

describe 'routing spec' do
subject { file('spec/routing/posts_routing_spec.rb') }

describe 'with no flag' do
before do
run_generator %w(posts seek and destroy)
end
it { is_expected.not_to exist }
end

describe 'with --routing-specs flag' do
describe 'without action parameter' do
before do
run_generator %w(posts --routing-specs)
end
it { is_expected.not_to exist }
end

describe 'with action parameter' do
before { run_generator %w(posts seek --routing-specs) }

it { is_expected.to contain(/require 'rails_helper'/) }
it { is_expected.to contain(/^RSpec.describe 'PostsController', #{type_metatag(:routing)}/) }
it { is_expected.to contain(/describe 'routing'/) }
it { is_expected.to contain(/it 'routes to #seek'/) }
it { is_expected.to contain(/expect\(:get => "\/posts\/seek"\).to route_to\("posts#seek"\)/) }
end
end

describe 'with --no-routing-specs flag' do
before do
run_generator %w(posts seek and destroy --no-routing_specs)
end
it { is_expected.not_to exist }
end
end
end

0 comments on commit 9c6d8f3

Please sign in to comment.