Skip to content

Commit 0c1b9fe

Browse files
David ReveloDavid Revelo
authored andcommitted
Create controller generator for routing specs (#1160)
1 parent faf1137 commit 0c1b9fe

File tree

3 files changed

+61
-2
lines changed

3 files changed

+61
-2
lines changed

lib/generators/rspec/controller/controller_generator.rb

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@ class ControllerGenerator < Base
77
argument :actions, :type => :array, :default => [], :banner => "action action"
88

99
class_option :template_engine, :desc => "Template engine to generate view files"
10-
class_option :controller_specs, :type => :boolean, :default => true
11-
class_option :view_specs, :type => :boolean, :default => true
10+
class_option :controller_specs, :type => :boolean, :default => true, :desc => "Generate controller specs"
11+
class_option :view_specs, :type => :boolean, :default => true, :desc => "Generate view specs"
12+
class_option :routing_specs, :type => :boolean, :default => false, :desc => "Generate routing specs"
1213

1314
def generate_controller_spec
1415
return unless options[:controller_specs]
@@ -29,6 +30,14 @@ def generate_view_specs
2930
File.join("spec", "views", file_path, "#{@action}.html.#{options[:template_engine]}_spec.rb")
3031
end
3132
end
33+
34+
def generate_routing_spec
35+
return if actions.empty?
36+
return unless options[:routing_specs]
37+
38+
template 'routing_spec.rb',
39+
File.join('spec/routing', class_path, "#{file_name}_routing_spec.rb")
40+
end
3241
end
3342
end
3443
end
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
require 'rails_helper'
2+
3+
<% module_namespacing do -%>
4+
RSpec.describe '<%= class_name %>Controller', <%= type_metatag(:routing) %> do
5+
describe 'routing' do
6+
<% for action in actions -%>
7+
it 'routes to #<%= action %>' do
8+
expect(:get => "/<%= class_name.underscore %>/<%= action %>").to route_to("<%= class_name.underscore %>#<%= action %>")
9+
end
10+
<% end -%>
11+
end
12+
end
13+
<% end -%>

spec/generators/rspec/controller/controller_generator_spec.rb

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,4 +90,41 @@
9090
end
9191
end
9292
end
93+
94+
describe 'routing spec' do
95+
subject { file('spec/routing/posts_routing_spec.rb') }
96+
97+
describe 'with no flag' do
98+
before do
99+
run_generator %w(posts seek and destroy)
100+
end
101+
it { is_expected.not_to exist }
102+
end
103+
104+
describe 'with --routing-specs flag' do
105+
describe 'without action parameter' do
106+
before do
107+
run_generator %w(posts --routing-specs)
108+
end
109+
it { is_expected.not_to exist }
110+
end
111+
112+
describe 'with action parameter' do
113+
before { run_generator %w(posts seek --routing-specs) }
114+
115+
it { is_expected.to contain(/require 'rails_helper'/) }
116+
it { is_expected.to contain(/^RSpec.describe 'PostsController', #{type_metatag(:routing)}/) }
117+
it { is_expected.to contain(/describe 'routing'/) }
118+
it { is_expected.to contain(/it 'routes to #seek'/) }
119+
it { is_expected.to contain(/expect\(:get => "\/posts\/seek"\).to route_to\("posts#seek"\)/) }
120+
end
121+
end
122+
123+
describe 'with --no-routing-specs flag' do
124+
before do
125+
run_generator %w(posts seek and destroy --no-routing_specs)
126+
end
127+
it { is_expected.not_to exist }
128+
end
129+
end
93130
end

0 commit comments

Comments
 (0)