From ed85fe15a3ca58f78be75ddf19c89f37d8ff4abe Mon Sep 17 00:00:00 2001 From: kenzo-tanaka Date: Fri, 19 Nov 2021 10:17:39 +0900 Subject: [PATCH 1/6] Fix request spec template. Replace ns_file_name to singular_table_name. https://github.com/rspec/rspec-rails/issues/2532 --- .../rspec/scaffold/templates/request_spec.rb | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/lib/generators/rspec/scaffold/templates/request_spec.rb b/lib/generators/rspec/scaffold/templates/request_spec.rb index 41a90dfa9..e16283bce 100644 --- a/lib/generators/rspec/scaffold/templates/request_spec.rb +++ b/lib/generators/rspec/scaffold/templates/request_spec.rb @@ -65,12 +65,12 @@ context "with valid parameters" do it "creates a new <%= class_name %>" do expect { - post <%= index_helper %>_url, params: { <%= ns_file_name %>: valid_attributes } + post <%= index_helper %>_url, params: { <%= singular_table_name %>: valid_attributes } }.to change(<%= class_name %>, :count).by(1) end - it "redirects to the created <%= ns_file_name %>" do - post <%= index_helper %>_url, params: { <%= ns_file_name %>: valid_attributes } + it "redirects to the created <%= singular_table_name %>" do + post <%= index_helper %>_url, params: { <%= singular_table_name %>: valid_attributes } expect(response).to redirect_to(<%= show_helper(class_name+".last") %>) end end @@ -78,12 +78,12 @@ context "with invalid parameters" do it "does not create a new <%= class_name %>" do expect { - post <%= index_helper %>_url, params: { <%= ns_file_name %>: invalid_attributes } + post <%= index_helper %>_url, params: { <%= singular_table_name %>: invalid_attributes } }.to change(<%= class_name %>, :count).by(0) end it "renders a successful response (i.e. to display the 'new' template)" do - post <%= index_helper %>_url, params: { <%= ns_file_name %>: invalid_attributes } + post <%= index_helper %>_url, params: { <%= singular_table_name %>: invalid_attributes } expect(response).to be_successful end end @@ -95,14 +95,14 @@ skip("Add a hash of attributes valid for your model") } - it "updates the requested <%= ns_file_name %>" do + it "updates the requested <%= singular_table_name %>" do <%= file_name %> = <%= class_name %>.create! valid_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 + it "redirects to the <%= singular_table_name %>" do <%= file_name %> = <%= class_name %>.create! valid_attributes patch <%= show_helper %>, params: { <%= singular_table_name %>: new_attributes } <%= file_name %>.reload @@ -120,7 +120,7 @@ end describe "DELETE /destroy" do - it "destroys the requested <%= ns_file_name %>" do + it "destroys the requested <%= singular_table_name %>" do <%= file_name %> = <%= class_name %>.create! valid_attributes expect { delete <%= show_helper %> From 33f129241c94ff63c237717178be2e339cc98ea6 Mon Sep 17 00:00:00 2001 From: kenzo-tanaka Date: Fri, 19 Nov 2021 20:45:39 +0900 Subject: [PATCH 2/6] Add namespaced request spec with --model-name. - check if parameter name is not admin_post - check if test contains Post.create instead of Admin::Post.create related: https://github.com/rspec/rspec-rails/pull/2534#issuecomment-973887145 --- .../rspec/scaffold/scaffold_generator_spec.rb | 21 ++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/spec/generators/rspec/scaffold/scaffold_generator_spec.rb b/spec/generators/rspec/scaffold/scaffold_generator_spec.rb index 47db1f535..5d6515c7f 100644 --- a/spec/generators/rspec/scaffold/scaffold_generator_spec.rb +++ b/spec/generators/rspec/scaffold/scaffold_generator_spec.rb @@ -101,11 +101,22 @@ describe 'namespaced request spec' do subject { file('spec/requests/admin/posts_spec.rb') } - 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(post)') } - it { is_expected.to contain('Admin::Post.create') } + + describe 'with no options' do + 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('post admin_posts_url, params: { admin_post: valid_attributes }') } + it { is_expected.to contain('admin_post_url(post)') } + it { is_expected.to contain('Admin::Post.create') } + end + + describe 'with --model-name' do + before { run_generator %w[admin/posts --model-name=post] } + it { is_expected.to contain('post admin_posts_url, params: { post: valid_attributes }') } + it { is_expected.not_to contain('params: { admin_post: valid_attributes }') } + it { is_expected.to contain(' Post.create') } + end end describe 'namespaced controller spec' do From 5dd83b47d5340715ffa7870ec0f10ecb8d41f874 Mon Sep 17 00:00:00 2001 From: kenzo-tanaka Date: Sat, 20 Nov 2021 11:15:13 +0900 Subject: [PATCH 3/6] Fix controller spec template for --model-name option. change ns_file_name to singular_table_name to support --model-name option. related: https://github.com/rspec/rspec-rails/issues/2532 --- .../scaffold/templates/controller_spec.rb | 20 +++++++++---------- .../rspec/scaffold/scaffold_generator_spec.rb | 16 +++++++++++++-- 2 files changed, 24 insertions(+), 12 deletions(-) diff --git a/lib/generators/rspec/scaffold/templates/controller_spec.rb b/lib/generators/rspec/scaffold/templates/controller_spec.rb index 094089a27..879491e75 100644 --- a/lib/generators/rspec/scaffold/templates/controller_spec.rb +++ b/lib/generators/rspec/scaffold/templates/controller_spec.rb @@ -79,19 +79,19 @@ context "with valid params" do it "creates a new <%= class_name %>" do expect { - post :create, params: {<%= ns_file_name %>: valid_attributes}, session: valid_session + post :create, params: {<%= singular_table_name %>: valid_attributes}, session: valid_session }.to change(<%= class_name %>, :count).by(1) end - it "redirects to the created <%= ns_file_name %>" do - post :create, params: {<%= ns_file_name %>: valid_attributes}, session: valid_session + it "redirects to the created <%= singular_table_name %>" do + post :create, params: {<%= singular_table_name %>: valid_attributes}, session: valid_session expect(response).to redirect_to(<%= class_name %>.last) end end context "with invalid params" do it "returns a success response (i.e. to display the 'new' template)" do - post :create, params: {<%= ns_file_name %>: invalid_attributes}, session: valid_session + post :create, params: {<%= singular_table_name %>: invalid_attributes}, session: valid_session expect(response).to be_successful end end @@ -103,16 +103,16 @@ skip("Add a hash of attributes valid for your model") } - it "updates the requested <%= ns_file_name %>" do + it "updates the requested <%= singular_table_name %>" do <%= file_name %> = <%= class_name %>.create! valid_attributes - put :update, params: {id: <%= file_name %>.to_param, <%= ns_file_name %>: new_attributes}, session: valid_session + put :update, params: {id: <%= file_name %>.to_param, <%= singular_table_name %>: new_attributes}, session: valid_session <%= file_name %>.reload skip("Add assertions for updated state") end - it "redirects to the <%= ns_file_name %>" do + it "redirects to the <%= singular_table_name %>" do <%= file_name %> = <%= class_name %>.create! valid_attributes - put :update, params: {id: <%= file_name %>.to_param, <%= ns_file_name %>: valid_attributes}, session: valid_session + put :update, params: {id: <%= file_name %>.to_param, <%= singular_table_name %>: valid_attributes}, session: valid_session expect(response).to redirect_to(<%= file_name %>) end end @@ -120,14 +120,14 @@ context "with invalid params" do it "returns a success response (i.e. to display the 'edit' template)" do <%= file_name %> = <%= class_name %>.create! valid_attributes - put :update, params: {id: <%= file_name %>.to_param, <%= ns_file_name %>: invalid_attributes}, session: valid_session + put :update, params: {id: <%= file_name %>.to_param, <%= singular_table_name %>: invalid_attributes}, session: valid_session expect(response).to be_successful end end end describe "DELETE #destroy" do - it "destroys the requested <%= ns_file_name %>" do + it "destroys the requested <%= singular_table_name %>" do <%= file_name %> = <%= class_name %>.create! valid_attributes expect { delete :destroy, params: {id: <%= file_name %>.to_param}, session: valid_session diff --git a/spec/generators/rspec/scaffold/scaffold_generator_spec.rb b/spec/generators/rspec/scaffold/scaffold_generator_spec.rb index 5d6515c7f..47f2f2858 100644 --- a/spec/generators/rspec/scaffold/scaffold_generator_spec.rb +++ b/spec/generators/rspec/scaffold/scaffold_generator_spec.rb @@ -121,8 +121,20 @@ describe 'namespaced controller spec' do subject { file('spec/controllers/admin/posts_controller_spec.rb') } - before { run_generator %w[admin/posts --controller_specs] } - it { is_expected.to contain(/^RSpec.describe Admin::PostsController, #{type_metatag(:controller)}/) } + + describe 'with no options' do + before { run_generator %w[admin/posts --controller_specs] } + it { is_expected.to contain(/^RSpec.describe Admin::PostsController, #{type_metatag(:controller)}/) } + it { is_expected.to contain('post :create, params: {admin_post: valid_attributes}') } + it { is_expected.to contain('Admin::Post.create') } + end + + describe 'with --model-name' do + before { run_generator %w[admin/posts --model-name=post --controller_specs] } + it { is_expected.to contain('post :create, params: {post: valid_attributes}') } + it { is_expected.not_to contain('params: {admin_post: valid_attributes}') } + it { is_expected.to contain(' Post.create') } + end end describe 'view specs' do From ba672e88c664cf12d3463c9ce5a4e4a62c896dbb Mon Sep 17 00:00:00 2001 From: kenzo-tanaka Date: Sat, 20 Nov 2021 11:41:49 +0900 Subject: [PATCH 4/6] Fix api request spec template for --model-name option. related: https://github.com/rspec/rspec-rails/issues/2532 --- .../scaffold/templates/api_request_spec.rb | 20 +++++++++---------- .../rspec/scaffold/scaffold_generator_spec.rb | 6 ++++++ 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/lib/generators/rspec/scaffold/templates/api_request_spec.rb b/lib/generators/rspec/scaffold/templates/api_request_spec.rb index 3d2bff889..065bbc98e 100644 --- a/lib/generators/rspec/scaffold/templates/api_request_spec.rb +++ b/lib/generators/rspec/scaffold/templates/api_request_spec.rb @@ -56,13 +56,13 @@ it "creates a new <%= class_name %>" do expect { post <%= index_helper %>_url, - params: { <%= ns_file_name %>: valid_attributes }, headers: valid_headers, as: :json + params: { <%= singular_table_name %>: valid_attributes }, headers: valid_headers, as: :json }.to change(<%= class_name %>, :count).by(1) end - it "renders a JSON response with the new <%= ns_file_name %>" do + it "renders a JSON response with the new <%= singular_table_name %>" do post <%= index_helper %>_url, - params: { <%= ns_file_name %>: valid_attributes }, headers: valid_headers, as: :json + params: { <%= singular_table_name %>: valid_attributes }, headers: valid_headers, as: :json expect(response).to have_http_status(:created) expect(response.content_type).to match(a_string_including("application/json")) end @@ -72,13 +72,13 @@ it "does not create a new <%= class_name %>" do expect { post <%= index_helper %>_url, - params: { <%= ns_file_name %>: invalid_attributes }, as: :json + params: { <%= singular_table_name %>: invalid_attributes }, as: :json }.to change(<%= class_name %>, :count).by(0) end - it "renders a JSON response with errors for the new <%= ns_file_name %>" do + it "renders a JSON response with errors for the new <%= singular_table_name %>" do post <%= index_helper %>_url, - params: { <%= ns_file_name %>: invalid_attributes }, headers: valid_headers, as: :json + 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")) end @@ -91,7 +91,7 @@ skip("Add a hash of attributes valid for your model") } - it "updates the requested <%= ns_file_name %>" do + it "updates the requested <%= singular_table_name %>" do <%= file_name %> = <%= class_name %>.create! valid_attributes patch <%= show_helper %>, params: { <%= singular_table_name %>: new_attributes }, headers: valid_headers, as: :json @@ -99,7 +99,7 @@ skip("Add assertions for updated state") end - it "renders a JSON response with the <%= ns_file_name %>" do + it "renders a JSON response with the <%= singular_table_name %>" do <%= file_name %> = <%= class_name %>.create! valid_attributes patch <%= show_helper %>, params: { <%= singular_table_name %>: new_attributes }, headers: valid_headers, as: :json @@ -109,7 +109,7 @@ end context "with invalid parameters" do - it "renders a JSON response with errors for the <%= ns_file_name %>" do + it "renders a JSON response with errors for the <%= singular_table_name %>" do <%= file_name %> = <%= class_name %>.create! valid_attributes patch <%= show_helper %>, params: { <%= singular_table_name %>: invalid_attributes }, headers: valid_headers, as: :json @@ -120,7 +120,7 @@ end describe "DELETE /destroy" do - it "destroys the requested <%= ns_file_name %>" do + it "destroys the requested <%= singular_table_name %>" do <%= file_name %> = <%= class_name %>.create! valid_attributes expect { delete <%= show_helper %>, headers: valid_headers, as: :json diff --git a/spec/generators/rspec/scaffold/scaffold_generator_spec.rb b/spec/generators/rspec/scaffold/scaffold_generator_spec.rb index 47f2f2858..826ad176a 100644 --- a/spec/generators/rspec/scaffold/scaffold_generator_spec.rb +++ b/spec/generators/rspec/scaffold/scaffold_generator_spec.rb @@ -117,6 +117,12 @@ it { is_expected.not_to contain('params: { admin_post: valid_attributes }') } it { is_expected.to contain(' Post.create') } end + + describe 'with --model-name & --api' do + before { run_generator %w[admin/posts --api --model-name=post] } + it { is_expected.to contain('params: { post: valid_attributes }') } + it { is_expected.not_to contain('params: { admin_post: valid_attributes }') } + end end describe 'namespaced controller spec' do From c4585e2aeca3dbed8855fba41afc96e351cefffc Mon Sep 17 00:00:00 2001 From: kenzo-tanaka Date: Sat, 20 Nov 2021 11:45:49 +0900 Subject: [PATCH 5/6] Add test for namespace request spec with --api option --- .../rspec/scaffold/scaffold_generator_spec.rb | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/spec/generators/rspec/scaffold/scaffold_generator_spec.rb b/spec/generators/rspec/scaffold/scaffold_generator_spec.rb index 826ad176a..643b2e8bb 100644 --- a/spec/generators/rspec/scaffold/scaffold_generator_spec.rb +++ b/spec/generators/rspec/scaffold/scaffold_generator_spec.rb @@ -118,10 +118,19 @@ it { is_expected.to contain(' Post.create') } end - describe 'with --model-name & --api' do - before { run_generator %w[admin/posts --api --model-name=post] } - it { is_expected.to contain('params: { post: valid_attributes }') } - it { is_expected.not_to contain('params: { admin_post: valid_attributes }') } + context 'with --api' do + describe 'with default options' do + before { run_generator %w[admin/posts --api] } + it { is_expected.to contain('params: { admin_post: valid_attributes }') } + it { is_expected.to contain('Admin::Post.create') } + end + + describe 'with --model-name' do + before { run_generator %w[admin/posts --api --model-name=post] } + it { is_expected.to contain('params: { post: valid_attributes }') } + it { is_expected.not_to contain('params: { admin_post: valid_attributes }') } + it { is_expected.to contain(' Post.create') } + end end end From 03f1d1a0618b95fc36eaa8b33b91824464a607e3 Mon Sep 17 00:00:00 2001 From: kenzo-tanaka Date: Sat, 20 Nov 2021 12:04:11 +0900 Subject: [PATCH 6/6] Fix api controller template with namespace and --model-name option --- .../scaffold/templates/api_controller_spec.rb | 26 +++++++++--------- .../rspec/scaffold/scaffold_generator_spec.rb | 27 ++++++++++++++----- 2 files changed, 34 insertions(+), 19 deletions(-) diff --git a/lib/generators/rspec/scaffold/templates/api_controller_spec.rb b/lib/generators/rspec/scaffold/templates/api_controller_spec.rb index 47347f5ea..7dc7889bf 100644 --- a/lib/generators/rspec/scaffold/templates/api_controller_spec.rb +++ b/lib/generators/rspec/scaffold/templates/api_controller_spec.rb @@ -64,21 +64,21 @@ context "with valid params" do it "creates a new <%= class_name %>" do expect { - post :create, params: {<%= ns_file_name %>: valid_attributes}, session: valid_session + post :create, params: {<%= singular_table_name %>: valid_attributes}, session: valid_session }.to change(<%= class_name %>, :count).by(1) end - it "renders a JSON response with the new <%= ns_file_name %>" do - post :create, params: {<%= ns_file_name %>: valid_attributes}, session: valid_session + it "renders a JSON response with the new <%= singular_table_name %>" do + post :create, params: {<%= singular_table_name %>: valid_attributes}, session: valid_session expect(response).to have_http_status(:created) expect(response.content_type).to eq('application/json') - expect(response.location).to eq(<%= ns_file_name %>_url(<%= class_name %>.last)) + expect(response.location).to eq(<%= singular_table_name %>_url(<%= class_name %>.last)) end end context "with invalid params" do - it "renders a JSON response with errors for the new <%= ns_file_name %>" do - post :create, params: {<%= ns_file_name %>: invalid_attributes}, session: valid_session + it "renders a JSON response with errors for the new <%= singular_table_name %>" do + post :create, params: {<%= singular_table_name %>: invalid_attributes}, session: valid_session expect(response).to have_http_status(:unprocessable_entity) expect(response.content_type).to eq('application/json') end @@ -91,25 +91,25 @@ skip("Add a hash of attributes valid for your model") } - it "updates the requested <%= ns_file_name %>" do + it "updates the requested <%= singular_table_name %>" do <%= file_name %> = <%= class_name %>.create! valid_attributes - put :update, params: {id: <%= file_name %>.to_param, <%= ns_file_name %>: new_attributes}, session: valid_session + put :update, params: {id: <%= file_name %>.to_param, <%= singular_table_name %>: new_attributes}, session: valid_session <%= file_name %>.reload skip("Add assertions for updated state") end - it "renders a JSON response with the <%= ns_file_name %>" do + it "renders a JSON response with the <%= singular_table_name %>" do <%= file_name %> = <%= class_name %>.create! valid_attributes - put :update, params: {id: <%= file_name %>.to_param, <%= ns_file_name %>: valid_attributes}, session: valid_session + put :update, params: {id: <%= file_name %>.to_param, <%= singular_table_name %>: valid_attributes}, session: valid_session expect(response).to have_http_status(:ok) expect(response.content_type).to eq('application/json') end end context "with invalid params" do - it "renders a JSON response with errors for the <%= ns_file_name %>" do + it "renders a JSON response with errors for the <%= singular_table_name %>" do <%= file_name %> = <%= class_name %>.create! valid_attributes - put :update, params: {id: <%= file_name %>.to_param, <%= ns_file_name %>: invalid_attributes}, session: valid_session + put :update, params: {id: <%= file_name %>.to_param, <%= singular_table_name %>: invalid_attributes}, session: valid_session expect(response).to have_http_status(:unprocessable_entity) expect(response.content_type).to eq('application/json') end @@ -117,7 +117,7 @@ end describe "DELETE #destroy" do - it "destroys the requested <%= ns_file_name %>" do + it "destroys the requested <%= singular_table_name %>" do <%= file_name %> = <%= class_name %>.create! valid_attributes expect { delete :destroy, params: {id: <%= file_name %>.to_param}, session: valid_session diff --git a/spec/generators/rspec/scaffold/scaffold_generator_spec.rb b/spec/generators/rspec/scaffold/scaffold_generator_spec.rb index 643b2e8bb..4638052b4 100644 --- a/spec/generators/rspec/scaffold/scaffold_generator_spec.rb +++ b/spec/generators/rspec/scaffold/scaffold_generator_spec.rb @@ -102,8 +102,8 @@ describe 'namespaced request spec' do subject { file('spec/requests/admin/posts_spec.rb') } - describe 'with no options' do - before { run_generator %w[admin/posts] } + describe 'with default options' do + 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('post admin_posts_url, params: { admin_post: valid_attributes }') } @@ -112,7 +112,7 @@ end describe 'with --model-name' do - before { run_generator %w[admin/posts --model-name=post] } + before { run_generator %w[admin/posts --model-name=post] } it { is_expected.to contain('post admin_posts_url, params: { post: valid_attributes }') } it { is_expected.not_to contain('params: { admin_post: valid_attributes }') } it { is_expected.to contain(' Post.create') } @@ -137,19 +137,34 @@ describe 'namespaced controller spec' do subject { file('spec/controllers/admin/posts_controller_spec.rb') } - describe 'with no options' do - before { run_generator %w[admin/posts --controller_specs] } + describe 'with default options' do + before { run_generator %w[admin/posts --controller_specs] } it { is_expected.to contain(/^RSpec.describe Admin::PostsController, #{type_metatag(:controller)}/) } it { is_expected.to contain('post :create, params: {admin_post: valid_attributes}') } it { is_expected.to contain('Admin::Post.create') } end describe 'with --model-name' do - before { run_generator %w[admin/posts --model-name=post --controller_specs] } + before { run_generator %w[admin/posts --model-name=post --controller_specs] } it { is_expected.to contain('post :create, params: {post: valid_attributes}') } it { is_expected.not_to contain('params: {admin_post: valid_attributes}') } it { is_expected.to contain(' Post.create') } end + + context 'with --api' do + describe 'with default options' do + before { run_generator %w[admin/posts --api --controller_specs] } + it { is_expected.to contain('post :create, params: {admin_post: valid_attributes}') } + it { is_expected.to contain('Admin::Post.create') } + end + + describe 'with --model-name' do + before { run_generator %w[admin/posts --api --model-name=post --controller_specs] } + it { is_expected.to contain('post :create, params: {post: valid_attributes}') } + it { is_expected.not_to contain('params: {admin_post: valid_attributes}') } + it { is_expected.to contain(' Post.create') } + end + end end describe 'view specs' do