diff --git a/lib/generators/rspec/scaffold/scaffold_generator.rb b/lib/generators/rspec/scaffold/scaffold_generator.rb index 1264bb209..23987555f 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 9b93a8082..3d2bff889 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 8b70dd401..1dd78ffda 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 @@ -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 @@ -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 18e4ca911..47db1f535 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