Skip to content

Commit 4c83135

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

File tree

3 files changed

+128
-1
lines changed

3 files changed

+128
-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: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,120 @@
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}_path" %>
35+
expect(response).to be_successful
36+
end
37+
end
38+
<% end -%>
39+
40+
describe "GET /new" do
41+
it "renders a successful response" do
42+
get <%= "#{new_helper}_path" %>
43+
expect(response).to be_successful
44+
end
45+
end
46+
47+
describe "POST /create" do
48+
context "with valid parameters" do
49+
it "creates a new <%= class_name %>" do
50+
expect {
51+
post <%= "#{index_helper}_path" %>, params: { <%= ns_file_name %>: valid_attributes }
52+
}.to change(<%= class_name %>, :count).by(1)
53+
end
54+
55+
it "redirects to the created <%= ns_file_name %>" do
56+
expect {
57+
post <%= "#{index_helper}_path" %>, params: { <%= ns_file_name %>: valid_attributes }
58+
}.to redirect_to("<%= ns_table_name %>/#{<%= class_name %>.last}")
59+
end
60+
end
61+
62+
context "with invalid parameters" do
63+
it "does not create a new <%= class_name %>" do
64+
expect {
65+
post <%= "#{index_helper}_path" %>, params: { <%= ns_file_name %>: invalid_attributes }
66+
}.not_to change(<%= class_name %>, :count).by(1)
67+
end
68+
69+
it "renders a successful response (i.e. to display the 'new' template)" do
70+
post <%= "#{index_helper}_path" %>, params: { <%= ns_file_name %>: invalid_attributes }
71+
expect(response).to be_successful
72+
end
73+
end
74+
end
75+
76+
describe "PUT /update" do
77+
context "with valid parameters" do
78+
let(:new_attributes) {
79+
skip("Add a hash of attributes valid for your model")
80+
}
81+
82+
it "updates the requested <%= ns_file_name %>" do
83+
<%= file_name %> = <%= class_name %>.create! valid_attributes
84+
put "/<%= ns_table_name %>/<%= file_name %>.id", params: { <%= ns_file_name %>: new_attributes }
85+
<%= file_name %>.reload
86+
skip("Add assertions for updated state")
87+
end
88+
89+
it "redirects to the <%= ns_file_name %>" do
90+
<%= file_name %> = <%= class_name %>.create! valid_attributes
91+
put "/<%= ns_table_name %>/<%= file_name %>.id", params: { <%= ns_file_name %>: new_attributes }
92+
<%= file_name %>.reload
93+
expect(response).to redirect_to("<%= ns_table_name %>/#{<%= file_name %>.id}")
94+
end
95+
end
96+
97+
context "with invalid parameters" do
98+
it "renders a successful response (i.e. to display the 'edit' template)" do
99+
put "/<%= ns_table_name %>/<%= file_name %>.id", params: { <%= ns_file_name %>: invalid_attributes }
100+
expect(response).to be_successful
101+
end
102+
end
103+
end
104+
105+
describe "DELETE /destroy" do
106+
it "destroys the requested <%= ns_file_name %>" do
107+
<%= file_name %> = <%= class_name %>.create! valid_attributes
108+
expect {
109+
delete delete: "/<%= ns_table_name %>/<%= file_name %>.id"
110+
}.to change(<%= class_name %>, :count).by(-1)
111+
end
112+
113+
it "redirects to the <%= table_name %> list" do
114+
<%= file_name %> = <%= class_name %>.create! valid_attributes
115+
delete: "/<%= ns_table_name %>/<%= file_name %>.id"
116+
expect(response).to redirect_to("<%= ns_table_name %>/#{<%= class_name %>.last}")
117+
end
118+
end
119+
end
120+
<% 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('posts/#{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/posts/#{Admin::Post.last}') }
7280
end
7381

7482
describe 'namespaced controller spec' do

0 commit comments

Comments
 (0)