test suite refactor
This commit is contained in:
25
test/controllers/quiz_controller/get_question_test.rb
Normal file
25
test/controllers/quiz_controller/get_question_test.rb
Normal file
@ -0,0 +1,25 @@
|
||||
# frozen_string_literal: true
|
||||
require 'test_helper'
|
||||
|
||||
class QuizControllerTest < ActionDispatch::IntegrationTest
|
||||
test "should require auth and redirect" do
|
||||
get question_path
|
||||
assert_redirected_to login_path
|
||||
|
||||
get question_path(questions(:fed1).id)
|
||||
assert_redirected_to login_path
|
||||
end
|
||||
|
||||
test "should redirect to next question on next" do
|
||||
auth_candidate candidates(:roy)
|
||||
qid = questions(:fed3).id
|
||||
params = { submit: 'Next', answer: { question_id: qid, answer_hash: { text: 'stuff' } } }
|
||||
post post_answer_path, params: params
|
||||
|
||||
assert_redirected_to question_path
|
||||
assert session[:test_id].present?
|
||||
assert assigns(:question), '@question not present'
|
||||
assert assigns(:answer), '@answer not present'
|
||||
assert assigns(:status), '@status not present'
|
||||
end
|
||||
end
|
71
test/controllers/quiz_controller/post_answer_test.rb
Normal file
71
test/controllers/quiz_controller/post_answer_test.rb
Normal file
@ -0,0 +1,71 @@
|
||||
# frozen_string_literal: true
|
||||
require 'test_helper'
|
||||
|
||||
class QuizControllerTest < ActionDispatch::IntegrationTest
|
||||
test "should redirect to saved on save" do
|
||||
auth_candidate candidates(:dawn)
|
||||
qid = questions(:fed5).id
|
||||
post post_answer_path, params: { save: 'Save', answer: { question_id: qid, answer: 'an option' } }
|
||||
|
||||
assert_redirected_to saved_path
|
||||
assert session[:test_id].present?
|
||||
end
|
||||
|
||||
test "should get flash message on bad radio response" do
|
||||
auth_candidate candidates(:dawn)
|
||||
qid = questions(:fed5).id
|
||||
post post_answer_path, params: { answer: { question_id: qid, answer: nil } }
|
||||
|
||||
assert_response :success
|
||||
assert session[:test_id].present?
|
||||
assert_equal qid, flash[:error]
|
||||
assert assigns(:question), '@question not present'
|
||||
assert assigns(:answer), '@answer not present'
|
||||
end
|
||||
|
||||
test "should get flash message on bad text response" do
|
||||
auth_candidate candidates(:dawn)
|
||||
qid = questions(:fed4).id
|
||||
post post_answer_path, params: { answer: { question_id: qid, answer: nil } }
|
||||
|
||||
assert_response :success
|
||||
assert session[:test_id].present?
|
||||
assert_equal qid, flash[:error]
|
||||
assert assigns(:question), '@question not present'
|
||||
assert assigns(:answer), '@answer not present'
|
||||
end
|
||||
|
||||
test "should process checkbox" do
|
||||
auth_candidate candidates(:dawn)
|
||||
qid = questions(:fed10).id
|
||||
post post_answer_path, params: { answer: { question_id: qid, answer_array: 'an-option' } }
|
||||
|
||||
assert_response :success
|
||||
assert session[:test_id].present?
|
||||
assert assigns(:question), '@question not present'
|
||||
assert assigns(:answer), '@answer not present'
|
||||
end
|
||||
|
||||
test 'should handle XHR update and complete progress' do
|
||||
auth_candidate candidates(:peggy)
|
||||
qid = questions(:fed10).id
|
||||
post post_answer_path, xhr: true, params: { answer: { question_id: qid, answer_array: ['an-option'] } }
|
||||
|
||||
assert_response :success
|
||||
assert_match(/updated successfully/, JSON.parse(@response.body)['message'])
|
||||
assert_equal 100, JSON.parse(@response.body)['progress']
|
||||
assert assigns(:question), '@question not present'
|
||||
assert assigns(:answer), '@answer not present'
|
||||
end
|
||||
|
||||
test 'should handle XHR fail' do
|
||||
auth_candidate candidates(:peggy)
|
||||
qid = questions(:fed10).id
|
||||
post post_answer_path, xhr: true, params: { answer: { question_id: qid, answer_array: [nil] } }
|
||||
|
||||
assert_response 400
|
||||
assert_match(/select.*answer/i, JSON.parse(@response.body).join)
|
||||
assert assigns(:question), '@question not present'
|
||||
assert assigns(:answer), '@answer not present'
|
||||
end
|
||||
end
|
52
test/controllers/quiz_controller/summary_test.rb
Normal file
52
test/controllers/quiz_controller/summary_test.rb
Normal file
@ -0,0 +1,52 @@
|
||||
# frozen_string_literal: true
|
||||
require 'test_helper'
|
||||
|
||||
class QuizControllerTest < ActionDispatch::IntegrationTest
|
||||
include ActiveJob::TestHelper
|
||||
|
||||
test 'summary should require auth and redirect' do
|
||||
get summary_path
|
||||
assert_redirected_to login_path
|
||||
end
|
||||
|
||||
test "should get summary" do
|
||||
auth_candidate candidates :dawn
|
||||
get summary_path
|
||||
|
||||
assert_response :success
|
||||
assert assigns(:quiz), '@quiz not present'
|
||||
end
|
||||
|
||||
test "should get summary if complete but not submitted" do
|
||||
auth_candidate candidates(:dawn)
|
||||
|
||||
get summary_url
|
||||
assert_response :success
|
||||
end
|
||||
|
||||
test "should redirect from summary" do
|
||||
auth_candidate candidates :roy
|
||||
get summary_path
|
||||
|
||||
assert_redirected_to question_path
|
||||
end
|
||||
|
||||
test "should NOT send mailers on submission" do
|
||||
auth_candidate candidates(:dawn)
|
||||
|
||||
assert_enqueued_jobs 0 do
|
||||
post post_summary_path
|
||||
end
|
||||
assert_redirected_to summary_path
|
||||
assert_match 'must complete', flash[:error]
|
||||
end
|
||||
|
||||
test "should send mailers on submission" do
|
||||
auth_candidate candidates(:peggy)
|
||||
|
||||
assert_enqueued_jobs 3 do
|
||||
post post_summary_path
|
||||
end
|
||||
assert_redirected_to thankyou_path
|
||||
end
|
||||
end
|
Reference in New Issue
Block a user