Skip to content

Commit

Permalink
Create controller generator for routing specs (#1160)
Browse files Browse the repository at this point in the history
  • Loading branch information
David Revelo authored and 00dav00 committed Jun 7, 2019
1 parent 5780b19 commit 666751f
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
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
@@ -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
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 666751f

Please sign in to comment.