Skip to content

Commit ba2d66a

Browse files
authored
Merge pull request #2492 from PedroAugustoRamalhoDuarte/main
Fixes requests specs generated by scaffold with namespace resource
2 parents 3874bab + 749aefa commit ba2d66a

File tree

4 files changed

+18
-14
lines changed

4 files changed

+18
-14
lines changed

lib/generators/rspec/scaffold/scaffold_generator.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,10 @@ def template_file(folder:, suffix: '')
127127
def banner
128128
self.class.banner
129129
end
130+
131+
def show_helper(resource_name = file_name)
132+
"#{singular_route_name}_url(#{resource_name})"
133+
end
130134
end
131135
end
132136
end

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
describe "GET /show" do
4747
it "renders a successful response" do
4848
<%= file_name %> = <%= class_name %>.create! valid_attributes
49-
get <%= show_helper.tr('@', '') %>, as: :json
49+
get <%= show_helper %>, as: :json
5050
expect(response).to be_successful
5151
end
5252
end
@@ -93,15 +93,15 @@
9393
9494
it "updates the requested <%= ns_file_name %>" do
9595
<%= file_name %> = <%= class_name %>.create! valid_attributes
96-
patch <%= show_helper.tr('@', '') %>,
96+
patch <%= show_helper %>,
9797
params: { <%= singular_table_name %>: new_attributes }, headers: valid_headers, as: :json
9898
<%= file_name %>.reload
9999
skip("Add assertions for updated state")
100100
end
101101
102102
it "renders a JSON response with the <%= ns_file_name %>" do
103103
<%= file_name %> = <%= class_name %>.create! valid_attributes
104-
patch <%= show_helper.tr('@', '') %>,
104+
patch <%= show_helper %>,
105105
params: { <%= singular_table_name %>: new_attributes }, headers: valid_headers, as: :json
106106
expect(response).to have_http_status(:ok)
107107
expect(response.content_type).to match(a_string_including("application/json"))
@@ -111,7 +111,7 @@
111111
context "with invalid parameters" do
112112
it "renders a JSON response with errors for the <%= ns_file_name %>" do
113113
<%= file_name %> = <%= class_name %>.create! valid_attributes
114-
patch <%= show_helper.tr('@', '') %>,
114+
patch <%= show_helper %>,
115115
params: { <%= singular_table_name %>: invalid_attributes }, headers: valid_headers, as: :json
116116
expect(response).to have_http_status(:unprocessable_entity)
117117
expect(response.content_type).to match(a_string_including("application/json"))
@@ -123,7 +123,7 @@
123123
it "destroys the requested <%= ns_file_name %>" do
124124
<%= file_name %> = <%= class_name %>.create! valid_attributes
125125
expect {
126-
delete <%= show_helper.tr('@', '') %>, headers: valid_headers, as: :json
126+
delete <%= show_helper %>, headers: valid_headers, as: :json
127127
}.to change(<%= class_name %>, :count).by(-1)
128128
end
129129
end

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
describe "GET /show" do
4242
it "renders a successful response" do
4343
<%= file_name %> = <%= class_name %>.create! valid_attributes
44-
get <%= show_helper.tr('@', '') %>
44+
get <%= show_helper %>
4545
expect(response).to be_successful
4646
end
4747
end
@@ -56,7 +56,7 @@
5656
describe "GET /edit" do
5757
it "renders a successful response" do
5858
<%= file_name %> = <%= class_name %>.create! valid_attributes
59-
get <%= edit_helper.tr('@','') %>
59+
get <%= edit_helper %>
6060
expect(response).to be_successful
6161
end
6262
end
@@ -71,7 +71,7 @@
7171
7272
it "redirects to the created <%= ns_file_name %>" do
7373
post <%= index_helper %>_url, params: { <%= ns_file_name %>: valid_attributes }
74-
expect(response).to redirect_to(<%= show_helper.gsub("\@#{file_name}", class_name+".last") %>)
74+
expect(response).to redirect_to(<%= show_helper(class_name+".last") %>)
7575
end
7676
end
7777
@@ -97,14 +97,14 @@
9797
9898
it "updates the requested <%= ns_file_name %>" do
9999
<%= file_name %> = <%= class_name %>.create! valid_attributes
100-
patch <%= show_helper.tr('@', '') %>, params: { <%= singular_table_name %>: new_attributes }
100+
patch <%= show_helper %>, params: { <%= singular_table_name %>: new_attributes }
101101
<%= file_name %>.reload
102102
skip("Add assertions for updated state")
103103
end
104104
105105
it "redirects to the <%= ns_file_name %>" do
106106
<%= file_name %> = <%= class_name %>.create! valid_attributes
107-
patch <%= show_helper.tr('@', '') %>, params: { <%= singular_table_name %>: new_attributes }
107+
patch <%= show_helper %>, params: { <%= singular_table_name %>: new_attributes }
108108
<%= file_name %>.reload
109109
expect(response).to redirect_to(<%= singular_table_name %>_url(<%= file_name %>))
110110
end
@@ -113,7 +113,7 @@
113113
context "with invalid parameters" do
114114
it "renders a successful response (i.e. to display the 'edit' template)" do
115115
<%= file_name %> = <%= class_name %>.create! valid_attributes
116-
patch <%= show_helper.tr('@', '') %>, params: { <%= singular_table_name %>: invalid_attributes }
116+
patch <%= show_helper %>, params: { <%= singular_table_name %>: invalid_attributes }
117117
expect(response).to be_successful
118118
end
119119
end
@@ -123,13 +123,13 @@
123123
it "destroys the requested <%= ns_file_name %>" do
124124
<%= file_name %> = <%= class_name %>.create! valid_attributes
125125
expect {
126-
delete <%= show_helper.tr('@', '') %>
126+
delete <%= show_helper %>
127127
}.to change(<%= class_name %>, :count).by(-1)
128128
end
129129
130130
it "redirects to the <%= table_name %> list" do
131131
<%= file_name %> = <%= class_name %>.create! valid_attributes
132-
delete <%= show_helper.tr('@', '') %>
132+
delete <%= show_helper %>
133133
expect(response).to redirect_to(<%= index_helper %>_url)
134134
end
135135
end

spec/generators/rspec/scaffold/scaffold_generator_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@
104104
before { run_generator %w[admin/posts] }
105105
it { is_expected.to exist }
106106
it { is_expected.to contain(/^RSpec.describe "\/admin\/posts", #{type_metatag(:request)}/) }
107-
it { is_expected.to contain('admin_post_url(admin_post)') }
107+
it { is_expected.to contain('admin_post_url(post)') }
108108
it { is_expected.to contain('Admin::Post.create') }
109109
end
110110

0 commit comments

Comments
 (0)