Skip to content

Commit c4efca1

Browse files
committed
Add content into scaffold request spec
1 parent b9cc606 commit c4efca1

File tree

3 files changed

+144
-1
lines changed

3 files changed

+144
-1
lines changed

lib/generators/rspec/scaffold/scaffold_generator.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def generate_request_spec
3333
controller_class_path,
3434
"#{controller_file_name}_request_spec.rb"
3535
)
36-
template 'controller_spec.rb', template_file
36+
template 'request_spec.rb', template_file
3737
end
3838

3939
def generate_controller_spec
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,136 @@
1+
# frozen_string_literal:true
12

3+
require 'rails_helper'
4+
5+
# This spec was generated by rspec-rails when you ran the scaffold generator.
6+
# It demonstrates how one might use RSpec to specify the controller code that
7+
# was generated by Rails when you ran the scaffold generator.
8+
#
9+
# It assumes that the implementation code is generated by the rails scaffold
10+
# generator. If you are using any extension libraries to generate different
11+
# controller code, this generated spec may or may not pass.
12+
#
13+
# It only uses APIs available in rails and/or rspec-rails. There are a number
14+
# of tools you can use to make these specs even more expressive, but we're
15+
# sticking to rails and rspec-rails APIs to keep things simple and stable.
16+
17+
<% module_namespacing do -%>
18+
RSpec.describe <%= controller_class_name %>Controller, <%= type_metatag(:request) %> do
19+
# This should return the minimal set of attributes required to create a valid
20+
# <%= class_name %>. As you add validations to <%= class_name %>, be sure to
21+
# adjust the attributes here as well.
22+
let(:valid_attributes) {
23+
skip("Add a hash of attributes valid for your model")
24+
}
25+
26+
let(:invalid_attributes) {
27+
skip("Add a hash of attributes invalid for your model")
28+
}
29+
30+
<% unless options[:singleton] -%>
31+
describe "GET /index" do
32+
it "renders a successful response" do
33+
<%= class_name %>.create! valid_attributes
34+
get <%= "#{index_helper}_url" %>
35+
expect(response).to be_successful
36+
end
37+
end
38+
<% end -%>
39+
40+
describe "GET /show" do
41+
it "renders a successful response" do
42+
<%= file_name %> = <%= class_name %>.create! valid_attributes
43+
get <%= "#{singular_table_name}_url(#{file_name})" %>
44+
expect(response).to be_successful
45+
end
46+
end
47+
48+
describe "GET /new" do
49+
it "renders a successful response" do
50+
get <%= "#{new_helper}" %>
51+
expect(response).to be_successful
52+
end
53+
end
54+
55+
describe "GET /edit" do
56+
it "render a successful response" do
57+
<%= file_name %> = <%= class_name %>.create! valid_attributes
58+
get <%= "edit_#{singular_table_name}_url(#{file_name})" %>
59+
expect(response).to be_successful
60+
end
61+
end
62+
63+
describe "POST /create" do
64+
context "with valid parameters" do
65+
it "creates a new <%= class_name %>" do
66+
expect {
67+
post <%= "#{index_helper}_path" %>, params: { <%= ns_file_name %>: valid_attributes }
68+
}.to change(<%= class_name %>, :count).by(1)
69+
end
70+
71+
it "redirects to the created <%= ns_file_name %>" do
72+
post <%= "#{index_helper}_path" %>, params: { <%= ns_file_name %>: valid_attributes }
73+
expect(response).to redirect_to(<%= singular_table_name %>_url(<%= class_name %>.last))
74+
end
75+
end
76+
77+
context "with invalid parameters" do
78+
it "does not create a new <%= class_name %>" do
79+
expect {
80+
post <%= "#{index_helper}_path" %>, params: { <%= ns_file_name %>: invalid_attributes }
81+
}.to change(<%= class_name %>, :count).by(0)
82+
end
83+
84+
it "renders a successful response (i.e. to display the 'new' template)" do
85+
post <%= "#{index_helper}_path" %>, params: { <%= ns_file_name %>: invalid_attributes }
86+
expect(response).to be_successful
87+
end
88+
end
89+
end
90+
91+
describe "PATCH /update" do
92+
context "with valid parameters" do
93+
let(:new_attributes) {
94+
skip("Add a hash of attributes valid for your model")
95+
}
96+
97+
it "updates the requested <%= ns_file_name %>" do
98+
<%= file_name %> = <%= class_name %>.create! valid_attributes
99+
patch <%= "#{singular_table_name}_url(#{file_name})" %>, params: { <%= singular_table_name %>: new_attributes }
100+
<%= file_name %>.reload
101+
skip("Add assertions for updated state")
102+
end
103+
104+
it "redirects to the <%= ns_file_name %>" do
105+
<%= file_name %> = <%= class_name %>.create! valid_attributes
106+
patch <%= "#{singular_table_name}_url(#{file_name})" %>, params: { <%= singular_table_name %>: new_attributes }
107+
<%= file_name %>.reload
108+
expect(response).to redirect_to(<%= singular_table_name %>_url(<%= file_name %>))
109+
end
110+
end
111+
112+
context "with invalid parameters" do
113+
it "renders a successful response (i.e. to display the 'edit' template)" do
114+
<%= file_name %> = <%= class_name %>.create! valid_attributes
115+
patch <%= "#{singular_table_name}_url(#{file_name})" %>, params: { <%= singular_table_name %>: invalid_attributes }
116+
expect(response).to be_successful
117+
end
118+
end
119+
end
120+
121+
describe "DELETE /destroy" do
122+
it "destroys the requested <%= ns_file_name %>" do
123+
<%= file_name %> = <%= class_name %>.create! valid_attributes
124+
expect {
125+
delete <%= "#{singular_table_name}_url(#{file_name})" %>
126+
}.to change(<%= class_name %>, :count).by(-1)
127+
end
128+
129+
it "redirects to the <%= table_name %> list" do
130+
<%= file_name %> = <%= class_name %>.create! valid_attributes
131+
delete <%= "#{singular_table_name}_url(#{file_name})" %>
132+
expect(response).to redirect_to(<%= "#{index_helper}_url" %>)
133+
end
134+
end
135+
end
136+
<% end -%>

spec/generators/rspec/scaffold/scaffold_generator_spec.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@
1111
describe 'with --request_specs' do
1212
before { run_generator %w[posts --request_specs] }
1313
it { is_expected.to exist }
14+
it { is_expected.to contain("require 'rails_helper'") }
15+
it { is_expected.to contain(/^RSpec.describe PostsController, #{type_metatag(:request)}/) }
16+
it { is_expected.to contain('GET /new') }
17+
it { is_expected.to contain(/"redirects to the created post"/) }
18+
it { is_expected.to contain('post_url(Post.last)') }
19+
it { is_expected.to contain(/"redirects to the \w+ list"/) }
1420
end
1521

1622
describe 'with no options' do
@@ -69,6 +75,8 @@
6975
subject { file('spec/requests/admin/posts_request_spec.rb') }
7076
before { run_generator %w[admin/posts --request_specs] }
7177
it { is_expected.to exist }
78+
it { is_expected.to contain(/^RSpec.describe Admin::PostsController, #{type_metatag(:request)}/) }
79+
it { is_expected.to contain('admin_post_url(Admin::Post.last)') }
7280
end
7381

7482
describe 'namespaced controller spec' do

0 commit comments

Comments
 (0)