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/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/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/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 %> diff --git a/spec/generators/rspec/scaffold/scaffold_generator_spec.rb b/spec/generators/rspec/scaffold/scaffold_generator_spec.rb index 47db1f535..4638052b4 100644 --- a/spec/generators/rspec/scaffold/scaffold_generator_spec.rb +++ b/spec/generators/rspec/scaffold/scaffold_generator_spec.rb @@ -101,17 +101,70 @@ 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 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 }') } + 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 + + 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 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 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] } + 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