Skip to content

Commit

Permalink
Merge pull request #2492 from PedroAugustoRamalhoDuarte/main
Browse files Browse the repository at this point in the history
Fixes requests specs generated by scaffold with namespace resource
  • Loading branch information
pirj committed May 2, 2021
2 parents 3874bab + 749aefa commit ba2d66a
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 14 deletions.
4 changes: 4 additions & 0 deletions lib/generators/rspec/scaffold/scaffold_generator.rb
Expand Up @@ -127,6 +127,10 @@ def template_file(folder:, suffix: '')
def banner
self.class.banner
end

def show_helper(resource_name = file_name)
"#{singular_route_name}_url(#{resource_name})"
end
end
end
end
10 changes: 5 additions & 5 deletions lib/generators/rspec/scaffold/templates/api_request_spec.rb
Expand Up @@ -46,7 +46,7 @@
describe "GET /show" do
it "renders a successful response" do
<%= file_name %> = <%= class_name %>.create! valid_attributes
get <%= show_helper.tr('@', '') %>, as: :json
get <%= show_helper %>, as: :json
expect(response).to be_successful
end
end
Expand Down Expand Up @@ -93,15 +93,15 @@
it "updates the requested <%= ns_file_name %>" do
<%= file_name %> = <%= class_name %>.create! valid_attributes
patch <%= show_helper.tr('@', '') %>,
patch <%= show_helper %>,
params: { <%= singular_table_name %>: new_attributes }, headers: valid_headers, as: :json
<%= file_name %>.reload
skip("Add assertions for updated state")
end
it "renders a JSON response with the <%= ns_file_name %>" do
<%= file_name %> = <%= class_name %>.create! valid_attributes
patch <%= show_helper.tr('@', '') %>,
patch <%= show_helper %>,
params: { <%= singular_table_name %>: new_attributes }, headers: valid_headers, as: :json
expect(response).to have_http_status(:ok)
expect(response.content_type).to match(a_string_including("application/json"))
Expand All @@ -111,7 +111,7 @@
context "with invalid parameters" do
it "renders a JSON response with errors for the <%= ns_file_name %>" do
<%= file_name %> = <%= class_name %>.create! valid_attributes
patch <%= show_helper.tr('@', '') %>,
patch <%= show_helper %>,
params: { <%= singular_table_name %>: invalid_attributes }, headers: valid_headers, as: :json
expect(response).to have_http_status(:unprocessable_entity)
expect(response.content_type).to match(a_string_including("application/json"))
Expand All @@ -123,7 +123,7 @@
it "destroys the requested <%= ns_file_name %>" do
<%= file_name %> = <%= class_name %>.create! valid_attributes
expect {
delete <%= show_helper.tr('@', '') %>, headers: valid_headers, as: :json
delete <%= show_helper %>, headers: valid_headers, as: :json
}.to change(<%= class_name %>, :count).by(-1)
end
end
Expand Down
16 changes: 8 additions & 8 deletions lib/generators/rspec/scaffold/templates/request_spec.rb
Expand Up @@ -41,7 +41,7 @@
describe "GET /show" do
it "renders a successful response" do
<%= file_name %> = <%= class_name %>.create! valid_attributes
get <%= show_helper.tr('@', '') %>
get <%= show_helper %>
expect(response).to be_successful
end
end
Expand All @@ -56,7 +56,7 @@
describe "GET /edit" do
it "renders a successful response" do
<%= file_name %> = <%= class_name %>.create! valid_attributes
get <%= edit_helper.tr('@','') %>
get <%= edit_helper %>
expect(response).to be_successful
end
end
Expand All @@ -71,7 +71,7 @@
it "redirects to the created <%= ns_file_name %>" do
post <%= index_helper %>_url, params: { <%= ns_file_name %>: valid_attributes }
expect(response).to redirect_to(<%= show_helper.gsub("\@#{file_name}", class_name+".last") %>)
expect(response).to redirect_to(<%= show_helper(class_name+".last") %>)
end
end
Expand All @@ -97,14 +97,14 @@
it "updates the requested <%= ns_file_name %>" do
<%= file_name %> = <%= class_name %>.create! valid_attributes
patch <%= show_helper.tr('@', '') %>, params: { <%= singular_table_name %>: new_attributes }
patch <%= show_helper %>, params: { <%= singular_table_name %>: new_attributes }
<%= file_name %>.reload
skip("Add assertions for updated state")
end

it "redirects to the <%= ns_file_name %>" do
<%= file_name %> = <%= class_name %>.create! valid_attributes
patch <%= show_helper.tr('@', '') %>, params: { <%= singular_table_name %>: new_attributes }
patch <%= show_helper %>, params: { <%= singular_table_name %>: new_attributes }
<%= file_name %>.reload
expect(response).to redirect_to(<%= singular_table_name %>_url(<%= file_name %>))
end
Expand All @@ -113,7 +113,7 @@
context "with invalid parameters" do
it "renders a successful response (i.e. to display the 'edit' template)" do
<%= file_name %> = <%= class_name %>.create! valid_attributes
patch <%= show_helper.tr('@', '') %>, params: { <%= singular_table_name %>: invalid_attributes }
patch <%= show_helper %>, params: { <%= singular_table_name %>: invalid_attributes }
expect(response).to be_successful
end
end
Expand All @@ -123,13 +123,13 @@
it "destroys the requested <%= ns_file_name %>" do
<%= file_name %> = <%= class_name %>.create! valid_attributes
expect {
delete <%= show_helper.tr('@', '') %>
delete <%= show_helper %>
}.to change(<%= class_name %>, :count).by(-1)
end

it "redirects to the <%= table_name %> list" do
<%= file_name %> = <%= class_name %>.create! valid_attributes
delete <%= show_helper.tr('@', '') %>
delete <%= show_helper %>
expect(response).to redirect_to(<%= index_helper %>_url)
end
end
Expand Down
2 changes: 1 addition & 1 deletion spec/generators/rspec/scaffold/scaffold_generator_spec.rb
Expand Up @@ -104,7 +104,7 @@
before { run_generator %w[admin/posts] }
it { is_expected.to exist }
it { is_expected.to contain(/^RSpec.describe "\/admin\/posts", #{type_metatag(:request)}/) }
it { is_expected.to contain('admin_post_url(admin_post)') }
it { is_expected.to contain('admin_post_url(post)') }
it { is_expected.to contain('Admin::Post.create') }
end

Expand Down

0 comments on commit ba2d66a

Please sign in to comment.