From cd4a32123b7d155044d0a5480da735fa4cd5ca32 Mon Sep 17 00:00:00 2001 From: jacobtoddmathes Date: Fri, 16 Feb 2024 23:57:45 -0600 Subject: [PATCH 1/2] updating readme, tweaking controller logic --- README.md | 2 + rails_root/.rubocop.yml | 1 + .../survey_responses_controller.rb | 4 +- .../spec/requests/survey_responses_spec.rb | 85 ++++++++----------- 4 files changed, 41 insertions(+), 51 deletions(-) diff --git a/README.md b/README.md index 7d9a1cc..6937b7d 100644 --- a/README.md +++ b/README.md @@ -3,6 +3,8 @@ [![Maintainability](https://api.codeclimate.com/v1/badges/62f4dd4fb092b4211973/maintainability)](https://codeclimate.com/repos/65caed0abc0d27237b1794c9/maintainability) ![rubocop](https://img.shields.io/endpoint?url=https://gist.githubusercontent.com/barnden/c7b2d5e19079e12445b300407e383294/raw/badge.json) +**All project files in rails_root** + Links - Deployed Application - https://elrc-app-dfcfc7cd862b.herokuapp.com/ diff --git a/rails_root/.rubocop.yml b/rails_root/.rubocop.yml index 55bde9e..0272825 100644 --- a/rails_root/.rubocop.yml +++ b/rails_root/.rubocop.yml @@ -2,6 +2,7 @@ # ignore migration files AllCops: + DisabledByDefault: false Exclude: - 'db/**/*' - 'config/**/*' diff --git a/rails_root/app/controllers/survey_responses_controller.rb b/rails_root/app/controllers/survey_responses_controller.rb index 759ef0d..450f77d 100644 --- a/rails_root/app/controllers/survey_responses_controller.rb +++ b/rails_root/app/controllers/survey_responses_controller.rb @@ -48,13 +48,15 @@ def edit # POST /survey_responses or /survey_responses.json def create - if survey_response_params.values.any? { |value| value.nil? || value.empty? } + # If any of the survey_response_params values are nil, then the form is invalid + if survey_response_params.values.any? { |value| value.nil? || value.empty? || survey_response_params.keys.length != SurveyResponse.column_names.length - 3 } respond_to do |format| format.html do redirect_to new_survey_response_url, notice: 'invalid form', status: :unprocessable_entity end format.json { render json: { error: 'invalid form' }, status: :unprocessable_entity } end + else @survey_response = SurveyResponse.new(survey_response_params) diff --git a/rails_root/spec/requests/survey_responses_spec.rb b/rails_root/spec/requests/survey_responses_spec.rb index 3328564..8adcd5c 100644 --- a/rails_root/spec/requests/survey_responses_spec.rb +++ b/rails_root/spec/requests/survey_responses_spec.rb @@ -2,25 +2,9 @@ require 'rails_helper' -# This spec was generated by rspec-rails when you ran the scaffold generator. -# It demonstrates how one might use RSpec to test the controller code that -# was generated by Rails when you ran the scaffold generator. -# -# It assumes that the implementation code is generated by the rails scaffold -# generator. If you are using any extension libraries to generate different -# controller code, this generated spec may or may not pass. -# -# It only uses APIs available in rails and/or rspec-rails. There are a number -# of tools you can use to make these specs even more expressive, but we're -# sticking to rails and rspec-rails APIs to keep things simple and stable. - # rubocop:disable Metrics/BlockLength RSpec.describe '/survey_responses', type: :request do - # This should return the minimal set of attributes required to create a valid - # SurveyResponse. As you add validations to SurveyResponse, be sure to - # adjust the attributes here as well. let(:valid_attributes) do - # skip('Add a hash of attributes valid for your model') { user_id: 1, leads_by_example: 1, @@ -35,8 +19,6 @@ end let(:invalid_attributes) do - # skip('Add a hash of attributes invalid for your model') - # any value in form is null { user_id: 1, leads_by_example: 1, @@ -50,60 +32,73 @@ } end + let(:new_attributes) do + { + leads_by_example: 5 + } + end + + let!(:survey_response) { SurveyResponse.create! valid_attributes } + describe 'GET /index' do + before { get survey_responses_url } + it 'renders a successful response' do - SurveyResponse.create! valid_attributes - get survey_responses_url expect(response).to be_successful end end describe 'GET /show' do + before { get survey_response_url(survey_response) } + it 'renders a successful response' do - survey_response = SurveyResponse.create! valid_attributes - get survey_response_url(survey_response) expect(response).to be_successful end end describe 'GET /new' do + before { get new_survey_response_url } + it 'renders a successful response' do - get new_survey_response_url expect(response).to be_successful end end describe 'GET /edit' do + before { get edit_survey_response_url(survey_response) } + it 'renders a successful response' do - survey_response = SurveyResponse.create! valid_attributes - get edit_survey_response_url(survey_response) expect(response).to be_successful end end describe 'POST /create' do context 'with valid parameters' do + def post_request + post survey_responses_url, params: { survey_response: valid_attributes } + end + it 'creates a new SurveyResponse' do - expect do - post survey_responses_url, params: { survey_response: valid_attributes } - end.to change(SurveyResponse, :count).by(1) + expect { post_request }.to change(SurveyResponse, :count).by(1) end it 'redirects to the created survey_response' do - post survey_responses_url, params: { survey_response: valid_attributes } + post_request expect(response).to redirect_to(survey_response_url(SurveyResponse.last)) end end context 'with invalid parameters' do + def post_request + post survey_responses_url, params: { survey_response: invalid_attributes } + end + it 'does not create a new SurveyResponse' do - expect do - post survey_responses_url, params: { survey_response: invalid_attributes } - end.to_not change(SurveyResponse, :count) + expect { post_request }.to_not change(SurveyResponse, :count) end it 'returns a failure response (i.e., to display the "new" template)' do - post survey_responses_url, params: { survey_response: invalid_attributes } + post_request expect(response).to have_http_status(:unprocessable_entity) end end @@ -111,34 +106,26 @@ describe 'PATCH /update' do context 'with valid parameters' do - let(:new_attributes) do - # skip('Add a hash of attributes valid for your model') - - { - leads_by_example: 5 - } + before do + patch survey_response_url(survey_response), params: { survey_response: new_attributes } + survey_response.reload end it 'updates the requested survey_response' do - survey_response = SurveyResponse.create! valid_attributes - patch survey_response_url(survey_response), params: { survey_response: new_attributes } - survey_response.reload - # skip('Add assertions for updated state') expect(survey_response.leads_by_example).to eq(5) end it 'redirects to the survey_response' do - survey_response = SurveyResponse.create! valid_attributes - patch survey_response_url(survey_response), params: { survey_response: new_attributes } - survey_response.reload expect(response).to redirect_to(survey_response_url(survey_response)) end end context 'with invalid parameters' do - it "renders a response with 422 status (i.e. to display the 'edit' template)" do - survey_response = SurveyResponse.create! valid_attributes + before do patch survey_response_url(survey_response), params: { survey_response: invalid_attributes } + end + + it "renders a response with 422 status (i.e. to display the 'edit' template)" do expect(response).to have_http_status(:unprocessable_entity) end end @@ -146,14 +133,12 @@ describe 'DELETE /destroy' do it 'destroys the requested survey_response' do - survey_response = SurveyResponse.create! valid_attributes expect do delete survey_response_url(survey_response) end.to change(SurveyResponse, :count).by(-1) end it 'redirects to the survey_responses list' do - survey_response = SurveyResponse.create! valid_attributes delete survey_response_url(survey_response) expect(response).to redirect_to(survey_responses_url) end From 2ae844f8c82a3c687a6e3882fbda9e0643f47fb6 Mon Sep 17 00:00:00 2001 From: jacobtoddmathes Date: Sat, 17 Feb 2024 00:16:25 -0600 Subject: [PATCH 2/2] fixing cucumber duplication bug --- .../features/step_definitions/stepdefs.rb | 2 +- .../spec/requests/survey_responses_spec.rb | 85 +++++++++++-------- 2 files changed, 51 insertions(+), 36 deletions(-) diff --git a/rails_root/features/step_definitions/stepdefs.rb b/rails_root/features/step_definitions/stepdefs.rb index 8699089..e48d5a3 100644 --- a/rails_root/features/step_definitions/stepdefs.rb +++ b/rails_root/features/step_definitions/stepdefs.rb @@ -49,7 +49,7 @@ When('I try to create model instances') do post survey_profiles_url, survey_profile: @survey_profiles_attributes - post survey_responses_url, survey_response: @survey_profiles_attributes + post survey_responses_url, survey_response: @survey_responses_attributes end Given('I have a set of valid attributes') do diff --git a/rails_root/spec/requests/survey_responses_spec.rb b/rails_root/spec/requests/survey_responses_spec.rb index 8adcd5c..3328564 100644 --- a/rails_root/spec/requests/survey_responses_spec.rb +++ b/rails_root/spec/requests/survey_responses_spec.rb @@ -2,9 +2,25 @@ require 'rails_helper' +# This spec was generated by rspec-rails when you ran the scaffold generator. +# It demonstrates how one might use RSpec to test the controller code that +# was generated by Rails when you ran the scaffold generator. +# +# It assumes that the implementation code is generated by the rails scaffold +# generator. If you are using any extension libraries to generate different +# controller code, this generated spec may or may not pass. +# +# It only uses APIs available in rails and/or rspec-rails. There are a number +# of tools you can use to make these specs even more expressive, but we're +# sticking to rails and rspec-rails APIs to keep things simple and stable. + # rubocop:disable Metrics/BlockLength RSpec.describe '/survey_responses', type: :request do + # This should return the minimal set of attributes required to create a valid + # SurveyResponse. As you add validations to SurveyResponse, be sure to + # adjust the attributes here as well. let(:valid_attributes) do + # skip('Add a hash of attributes valid for your model') { user_id: 1, leads_by_example: 1, @@ -19,6 +35,8 @@ end let(:invalid_attributes) do + # skip('Add a hash of attributes invalid for your model') + # any value in form is null { user_id: 1, leads_by_example: 1, @@ -32,73 +50,60 @@ } end - let(:new_attributes) do - { - leads_by_example: 5 - } - end - - let!(:survey_response) { SurveyResponse.create! valid_attributes } - describe 'GET /index' do - before { get survey_responses_url } - it 'renders a successful response' do + SurveyResponse.create! valid_attributes + get survey_responses_url expect(response).to be_successful end end describe 'GET /show' do - before { get survey_response_url(survey_response) } - it 'renders a successful response' do + survey_response = SurveyResponse.create! valid_attributes + get survey_response_url(survey_response) expect(response).to be_successful end end describe 'GET /new' do - before { get new_survey_response_url } - it 'renders a successful response' do + get new_survey_response_url expect(response).to be_successful end end describe 'GET /edit' do - before { get edit_survey_response_url(survey_response) } - it 'renders a successful response' do + survey_response = SurveyResponse.create! valid_attributes + get edit_survey_response_url(survey_response) expect(response).to be_successful end end describe 'POST /create' do context 'with valid parameters' do - def post_request - post survey_responses_url, params: { survey_response: valid_attributes } - end - it 'creates a new SurveyResponse' do - expect { post_request }.to change(SurveyResponse, :count).by(1) + expect do + post survey_responses_url, params: { survey_response: valid_attributes } + end.to change(SurveyResponse, :count).by(1) end it 'redirects to the created survey_response' do - post_request + post survey_responses_url, params: { survey_response: valid_attributes } expect(response).to redirect_to(survey_response_url(SurveyResponse.last)) end end context 'with invalid parameters' do - def post_request - post survey_responses_url, params: { survey_response: invalid_attributes } - end - it 'does not create a new SurveyResponse' do - expect { post_request }.to_not change(SurveyResponse, :count) + expect do + post survey_responses_url, params: { survey_response: invalid_attributes } + end.to_not change(SurveyResponse, :count) end it 'returns a failure response (i.e., to display the "new" template)' do - post_request + post survey_responses_url, params: { survey_response: invalid_attributes } expect(response).to have_http_status(:unprocessable_entity) end end @@ -106,26 +111,34 @@ def post_request describe 'PATCH /update' do context 'with valid parameters' do - before do - patch survey_response_url(survey_response), params: { survey_response: new_attributes } - survey_response.reload + let(:new_attributes) do + # skip('Add a hash of attributes valid for your model') + + { + leads_by_example: 5 + } end it 'updates the requested survey_response' do + survey_response = SurveyResponse.create! valid_attributes + patch survey_response_url(survey_response), params: { survey_response: new_attributes } + survey_response.reload + # skip('Add assertions for updated state') expect(survey_response.leads_by_example).to eq(5) end it 'redirects to the survey_response' do + survey_response = SurveyResponse.create! valid_attributes + patch survey_response_url(survey_response), params: { survey_response: new_attributes } + survey_response.reload expect(response).to redirect_to(survey_response_url(survey_response)) end end context 'with invalid parameters' do - before do - patch survey_response_url(survey_response), params: { survey_response: invalid_attributes } - end - it "renders a response with 422 status (i.e. to display the 'edit' template)" do + survey_response = SurveyResponse.create! valid_attributes + patch survey_response_url(survey_response), params: { survey_response: invalid_attributes } expect(response).to have_http_status(:unprocessable_entity) end end @@ -133,12 +146,14 @@ def post_request describe 'DELETE /destroy' do it 'destroys the requested survey_response' do + survey_response = SurveyResponse.create! valid_attributes expect do delete survey_response_url(survey_response) end.to change(SurveyResponse, :count).by(-1) end it 'redirects to the survey_responses list' do + survey_response = SurveyResponse.create! valid_attributes delete survey_response_url(survey_response) expect(response).to redirect_to(survey_responses_url) end