Skip to content

Commit a3273ec

Browse files
author
David Revelo
committed
Create controller generator for routing specs (#1160)
1 parent d94443d commit a3273ec

File tree

3 files changed

+60
-2
lines changed

3 files changed

+60
-2
lines changed

lib/generators/rspec/controller/controller_generator.rb

Lines changed: 10 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,13 @@ 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 unless options[:routing_specs]
36+
37+
template 'routing_spec.rb',
38+
File.join('spec/routing', class_path, "#{file_name}_routing_spec.rb")
39+
end
3240
end
3341
end
3442
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 { run_generator %w(posts seek and destroy) }
99+
100+
it { is_expected.not_to exist }
101+
end
102+
103+
describe 'with --routing-specs flag' do
104+
describe 'without action parameter' do
105+
before { run_generator %w(posts --routing-specs) }
106+
107+
it { is_expected.to contain(/require 'rails_helper'/) }
108+
it { is_expected.to contain(/^RSpec.describe PostsController, #{type_metatag(:routing)}/) }
109+
it { is_expected.to contain(/describe 'routing'/) }
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+
``
119+
it { is_expected.to contain(/it 'routes to #seek'/) }
120+
it { is_expected.to contain(/expect\(:get => "\/posts\/seek"\).to route_to\("posts#seek"\)/) }
121+
end
122+
end
123+
124+
describe 'with --no-routing-specs flag' do
125+
before { run_generator %w(posts seek and destroy --no-routing_specs) }
126+
127+
it { is_expected.not_to exist }
128+
end
129+
end
93130
end

0 commit comments

Comments
 (0)