Skip to content

Commit 3818055

Browse files
authored
Merge pull request #2585 from taketo1113/request-spec-rails7
Update of requests/controllers specs with invalid params for rails 7.0
2 parents 5fe75e7 + a589cb1 commit 3818055

File tree

3 files changed

+48
-2
lines changed

3 files changed

+48
-2
lines changed

lib/generators/rspec/scaffold/templates/controller_spec.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,17 @@
9090
end
9191
9292
context "with invalid params" do
93+
<% if Rails.version.to_f < 7.0 %>
9394
it "returns a success response (i.e. to display the 'new' template)" do
9495
post :create, params: {<%= singular_table_name %>: invalid_attributes}, session: valid_session
9596
expect(response).to be_successful
9697
end
98+
<% else %>
99+
it "renders a response with 422 status (i.e. to display the 'new' template)" do
100+
post :create, params: {<%= singular_table_name %>: invalid_attributes}, session: valid_session
101+
expect(response).to have_http_status(:unprocessable_entity)
102+
end
103+
<% end %>
97104
end
98105
end
99106
@@ -118,11 +125,19 @@
118125
end
119126
120127
context "with invalid params" do
128+
<% if Rails.version.to_f < 7.0 %>
121129
it "returns a success response (i.e. to display the 'edit' template)" do
122130
<%= file_name %> = <%= class_name %>.create! valid_attributes
123131
put :update, params: {id: <%= file_name %>.to_param, <%= singular_table_name %>: invalid_attributes}, session: valid_session
124132
expect(response).to be_successful
125133
end
134+
<% else %>
135+
it "renders a response with 422 status (i.e. to display the 'edit' template)" do
136+
<%= file_name %> = <%= class_name %>.create! valid_attributes
137+
put :update, params: {id: <%= file_name %>.to_param, <%= singular_table_name %>: invalid_attributes}, session: valid_session
138+
expect(response).to have_http_status(:unprocessable_entity)
139+
end
140+
<% end %>
126141
end
127142
end
128143

lib/generators/rspec/scaffold/templates/request_spec.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,17 @@
8383
}.to change(<%= class_name %>, :count).by(0)
8484
end
8585
86+
<% if Rails.version.to_f < 7.0 %>
8687
it "renders a successful response (i.e. to display the 'new' template)" do
8788
post <%= index_helper %>_url, params: { <%= singular_table_name %>: invalid_attributes }
8889
expect(response).to be_successful
8990
end
91+
<% else %>
92+
it "renders a response with 422 status (i.e. to display the 'new' template)" do
93+
post <%= index_helper %>_url, params: { <%= singular_table_name %>: invalid_attributes }
94+
expect(response).to have_http_status(:unprocessable_entity)
95+
end
96+
<% end %>
9097
end
9198
end
9299
@@ -112,11 +119,19 @@
112119
end
113120
114121
context "with invalid parameters" do
122+
<% if Rails.version.to_f < 7.0 %>
115123
it "renders a successful response (i.e. to display the 'edit' template)" do
116124
<%= file_name %> = <%= class_name %>.create! valid_attributes
117125
patch <%= show_helper %>, params: { <%= singular_table_name %>: invalid_attributes }
118126
expect(response).to be_successful
119127
end
128+
<% else %>
129+
it "renders a response with 422 status (i.e. to display the 'edit' template)" do
130+
<%= file_name %> = <%= class_name %>.create! valid_attributes
131+
patch <%= show_helper %>, params: { <%= singular_table_name %>: invalid_attributes }
132+
expect(response).to have_http_status(:unprocessable_entity)
133+
end
134+
<% end %>
120135
end
121136
end
122137

spec/generators/rspec/scaffold/scaffold_generator_spec.rb

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,14 @@
2020
it { is_expected.to contain('get post_url(post)') }
2121
it { is_expected.to contain('redirect_to(post_url(Post.last))') }
2222
it { is_expected.to contain(/"redirects to the \w+ list"/) }
23+
24+
if ::Rails::VERSION::STRING >= '7.0.0'
25+
it { is_expected.to contain(/renders a response with 422 status \(i.e. to display the 'new' template\)/) }
26+
it { is_expected.to contain(/renders a response with 422 status \(i.e. to display the 'edit' template\)/) }
27+
else
28+
it { is_expected.to contain(/renders a successful response \(i.e. to display the 'new' template\)/) }
29+
it { is_expected.to contain(/renders a successful response \(i.e. to display the 'edit' template\)/) }
30+
end
2331
end
2432

2533
describe 'with --no-request_specs' do
@@ -62,13 +70,21 @@
6270
it { is_expected.to contain(/^RSpec.describe PostsController, #{type_metatag(:controller)}/) }
6371
it { is_expected.to contain(/GET #new/) }
6472
it { is_expected.to contain(/"redirects to the created \w+"/) }
65-
it { is_expected.to contain(/display the 'new' template/) }
73+
if ::Rails::VERSION::STRING >= '7.0.0'
74+
it { is_expected.to contain(/renders a response with 422 status \(i.e. to display the 'new' template\)/) }
75+
else
76+
it { is_expected.to contain(/returns a success response \(i.e. to display the 'new' template\)/) }
77+
end
6678
it { is_expected.not_to contain(/"renders a JSON response with the new \w+"/) }
6779
it { is_expected.not_to contain(/"renders a JSON response with errors for the new \w+"/) }
6880

6981
it { is_expected.to contain(/GET #edit/) }
7082
it { is_expected.to contain(/"redirects to the \w+"/) }
71-
it { is_expected.to contain(/display the 'edit' template/) }
83+
if ::Rails::VERSION::STRING >= '7.0.0'
84+
it { is_expected.to contain(/renders a response with 422 status \(i.e. to display the 'edit' template\)/) }
85+
else
86+
it { is_expected.to contain(/returns a success response \(i.e. to display the 'edit' template\)/) }
87+
end
7288
it { is_expected.not_to contain(/"renders a JSON response with the \w+"/) }
7389
it { is_expected.not_to contain(/"renders a JSON response with errors for the \w+"/) }
7490

0 commit comments

Comments
 (0)