From 5468bee50e14b86168e45e724f4fb2e2b274e8bc Mon Sep 17 00:00:00 2001 From: PedroAugustoRamalhoDuarte Date: Sun, 11 Apr 2021 12:53:02 -0300 Subject: [PATCH 1/2] Refactor show_helper in request scaffold generators --- .../rspec/scaffold/scaffold_generator.rb | 4 ++++ .../rspec/scaffold/templates/api_request_spec.rb | 10 +++++----- .../rspec/scaffold/templates/request_spec.rb | 14 +++++++------- .../rspec/scaffold/scaffold_generator_spec.rb | 2 +- 4 files changed, 17 insertions(+), 13 deletions(-) diff --git a/lib/generators/rspec/scaffold/scaffold_generator.rb b/lib/generators/rspec/scaffold/scaffold_generator.rb index 1264bb2096..23987555f2 100644 --- a/lib/generators/rspec/scaffold/scaffold_generator.rb +++ b/lib/generators/rspec/scaffold/scaffold_generator.rb @@ -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 diff --git a/lib/generators/rspec/scaffold/templates/api_request_spec.rb b/lib/generators/rspec/scaffold/templates/api_request_spec.rb index 9b93a8082c..3d2bff8899 100644 --- a/lib/generators/rspec/scaffold/templates/api_request_spec.rb +++ b/lib/generators/rspec/scaffold/templates/api_request_spec.rb @@ -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 @@ -93,7 +93,7 @@ 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") @@ -101,7 +101,7 @@ 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")) @@ -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")) @@ -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 diff --git a/lib/generators/rspec/scaffold/templates/request_spec.rb b/lib/generators/rspec/scaffold/templates/request_spec.rb index 8b70dd4016..5f5e948f03 100644 --- a/lib/generators/rspec/scaffold/templates/request_spec.rb +++ b/lib/generators/rspec/scaffold/templates/request_spec.rb @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/spec/generators/rspec/scaffold/scaffold_generator_spec.rb b/spec/generators/rspec/scaffold/scaffold_generator_spec.rb index 18e4ca911d..47db1f535a 100644 --- a/spec/generators/rspec/scaffold/scaffold_generator_spec.rb +++ b/spec/generators/rspec/scaffold/scaffold_generator_spec.rb @@ -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 From 749aefa6e8c951d60eeb55cb09d198bc559e189e Mon Sep 17 00:00:00 2001 From: PedroAugustoRamalhoDuarte Date: Sun, 11 Apr 2021 13:32:49 -0300 Subject: [PATCH 2/2] Removes unused .tr call in edit_helper --- lib/generators/rspec/scaffold/templates/request_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/generators/rspec/scaffold/templates/request_spec.rb b/lib/generators/rspec/scaffold/templates/request_spec.rb index 5f5e948f03..1dd78ffda9 100644 --- a/lib/generators/rspec/scaffold/templates/request_spec.rb +++ b/lib/generators/rspec/scaffold/templates/request_spec.rb @@ -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