Skip to content

Commit db2aeec

Browse files
authored
Merge pull request #2222 from klyonrad/controller-default-generated-specs
Favor request specs over controller specs when generating a controller
2 parents 7dc567f + 5614581 commit db2aeec

File tree

3 files changed

+81
-6
lines changed

3 files changed

+81
-6
lines changed

lib/generators/rspec/controller/controller_generator.rb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,18 @@ 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, desc: "Generate controller specs"
10+
class_option :request_specs, type: :boolean, default: true, desc: "Generate request specs"
11+
class_option :controller_specs, type: :boolean, default: false, desc: "Generate controller specs"
1112
class_option :view_specs, type: :boolean, default: true, desc: "Generate view specs"
1213
class_option :routing_specs, type: :boolean, default: false, desc: "Generate routing specs"
1314

15+
def generate_request_spec
16+
return unless options[:request_specs]
17+
18+
template 'request_spec.rb',
19+
File.join('spec/requests', class_path, "#{file_name}_request_spec.rb")
20+
end
21+
1422
def generate_controller_spec
1523
return unless options[:controller_specs]
1624

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
require 'rails_helper'
2+
3+
RSpec.describe "<%= class_name.pluralize %>", <%= type_metatag(:request) %> do
4+
<% namespaced_path = regular_class_path.join('/') %>
5+
<% for action in actions -%>
6+
describe "GET /<%= action %>" do
7+
it "returns http success" do
8+
get "<%= "/#{namespaced_path}" if namespaced_path != '' %>/<%= file_name %>/<%= action %>"
9+
expect(response).to have_http_status(:success)
10+
end
11+
end
12+
13+
<% end -%>
14+
end

spec/generators/rspec/controller/controller_generator_spec.rb

Lines changed: 58 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,51 @@
55
RSpec.describe Rspec::Generators::ControllerGenerator, :type => :generator do
66
setup_default_destination
77

8-
describe 'controller specs' do
9-
subject { file('spec/controllers/posts_controller_spec.rb') }
8+
describe 'request specs' do
9+
subject { file('spec/requests/posts_request_spec.rb') }
10+
1011
describe 'generated by default' do
1112
before do
12-
run_generator %w(posts)
13+
run_generator %w[posts]
1314
end
1415

1516
describe 'the spec' do
1617
it { is_expected.to exist }
1718
it { is_expected.to contain(/require 'rails_helper'/) }
18-
it { is_expected.to contain(/^RSpec.describe PostsController, #{type_metatag(:controller)}/) }
19+
it { is_expected.to contain(/^RSpec.describe "Posts", #{type_metatag(:request)}/) }
1920
end
2021
end
22+
2123
describe 'skipped with a flag' do
2224
before do
23-
run_generator %w(posts --no-controller_specs)
25+
run_generator %w[posts --no-request_specs]
2426
end
2527
it { is_expected.not_to exist }
2628
end
29+
30+
31+
describe 'with actions' do
32+
before do
33+
run_generator %w[posts index custom_action]
34+
end
35+
36+
it { is_expected.to exist }
37+
it { is_expected.to contain('get "/posts/index"') }
38+
it { is_expected.to contain('get "/posts/custom_action"') }
39+
end
40+
41+
describe 'with namespace and actions' do
42+
subject { file('spec/requests/admin/external/users_request_spec.rb') }
43+
44+
before do
45+
run_generator %w[admin::external::users index custom_action]
46+
end
47+
48+
it { is_expected.to exist }
49+
it { is_expected.to contain(/^RSpec.describe "Admin::External::Users", #{type_metatag(:request)}/) }
50+
it { is_expected.to contain('get "/admin/external/users/index"') }
51+
it { is_expected.to contain('get "/admin/external/users/custom_action"') }
52+
end
2753
end
2854

2955
describe 'view specs' do
@@ -127,4 +153,31 @@
127153
it { is_expected.not_to exist }
128154
end
129155
end
156+
157+
describe 'controller specs' do
158+
subject { file('spec/controllers/posts_controller_spec.rb') }
159+
160+
describe 'are not generated' do
161+
it { is_expected.not_to exist }
162+
end
163+
164+
describe 'with --controller-specs flag' do
165+
before do
166+
run_generator %w[posts --controller-specs]
167+
end
168+
169+
describe 'the spec' do
170+
it { is_expected.to exist }
171+
it { is_expected.to contain(/require 'rails_helper'/) }
172+
it { is_expected.to contain(/^RSpec.describe PostsController, #{type_metatag(:controller)}/) }
173+
end
174+
end
175+
176+
describe 'with --no-controller_specs flag' do
177+
before do
178+
run_generator %w[posts --no-controller-specs]
179+
end
180+
it { is_expected.not_to exist }
181+
end
182+
end
130183
end

0 commit comments

Comments
 (0)