diff --git a/app/controllers/answers_controller.rb b/app/controllers/answers_controller.rb index f85c4dd..b469272 100644 --- a/app/controllers/answers_controller.rb +++ b/app/controllers/answers_controller.rb @@ -3,8 +3,7 @@ respond_to :html ## - # POST /answers - + # PUT/PATCH /[:locale]/answer/[:id] def update # create a new answer based off the passed params logger.debug("RAY: update params=") diff --git a/test/functional/answers_controller_test.rb b/test/functional/answers_controller_test.rb new file mode 100644 index 0000000..a671b57 --- /dev/null +++ b/test/functional/answers_controller_test.rb @@ -0,0 +1,74 @@ +class AnswersControllerTest < ActionDispatch::IntegrationTest + + include Devise::Test::IntegrationHelpers + + setup do + @user = User.last + + scaffold_plan + end + + # PUT/PATCH /[:locale]/answer/[:id] + # ---------------------------------------------------------- + test "should be able to update an answer" do + sign_in @user + + # Test an answer for each Querstion Format + QuestionFormat.all.each do |format| + question = Question.find_by(question_format: format) + template = question.section.phase.template + + plan = Plan.create(title: "Testing Answer For #{format.title}", + template: template) + + referrer = "/#{I18n.locale}/plans/#{plan.id}/phases/#{question.section.phase.id}/edit" + + answer = Answer.create(user: @user, plan: plan, question: question, + text: "#{format.title} Tester") + + if format.option_based + + else + # Try creating one first + form_attributes = {answer: {text: "#{format.title} Tester", + user_id: @user.id, plan_id: plan.id, + question_id: question.id}} + + put_answer(answer, form_attributes, referrer) + + answer = Answer.find_by(user: @user, plan: plan, question: question) + assert_not answer.id.nil?, "expected the answer to have been created and for an id to be present after creating a #{format.title} question!" + + # Try editing it + form_attributes = {answer: {text: "Tested", user_id: answer.user.id, + plan_id: answer.plan.id, + question_id: answer.question.id}} + + put_answer(answer, form_attributes, referrer) + + answer.reload + +puts answer.inspect + + assert_not answer.id.nil?, "expected the answer to have been updated and for an id to be present after creating a #{format.title} question!" + assert_equal "Tested", answer.text, "expected the text to have been updated for a #{format.title} question!" + + end + end + end + + + private + def put_answer(answer, attributes, referrer) + put answer_path(I18n.locale, answer), attributes, {'HTTP_REFERER': referrer} + + assert_equal I18n.t('helpers.project.answer_recorded'), flash[:notice] + assert_response :redirect + + follow_redirects + + assert_response :success + assert_select '.main_page_content h1', Plan.model_name.human.pluralize.titleize + + end +end \ No newline at end of file diff --git a/test/test_helper.rb b/test/test_helper.rb index 5c637e9..548aaf8 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -92,7 +92,8 @@ assert_response :redirect assert_match "#{root_url}", @response.redirect_url - follow_redirect! + follow_redirects + assert_response :success assert_select '.welcome-message h2', I18n.t('welcome_title') end @@ -103,14 +104,18 @@ assert_match "#{root_url}", @response.redirect_url # Sometimes Devise has an intermediary step prior to sending the user to the final destination - while @response.status >= 300 && @response.status < 400 - follow_redirect! - end + follow_redirects assert_response :success assert_select '.main_page_content h1', Plan.model_name.human.pluralize.titleize end + # ---------------------------------------------------------------------- + def follow_redirects + while @response.status >= 300 && @response.status < 400 + follow_redirect! + end + end # UNIT TEST HELPERS # ----------------------------------------------------------------------