progress on #27: test coverage over 96%

This commit is contained in:
Mark Moser 2016-08-16 17:09:40 -05:00
parent c7111ad7ad
commit 14bbd301ed
8 changed files with 125 additions and 11 deletions

View File

@ -109,10 +109,4 @@ class QuizController < ApplicationController
saved: params.key?(:save), saved: params.key?(:save),
submitted: params.key?(:submit)) submitted: params.key?(:submit))
end end
def process_live_code_text
@answer.update(answer: answer_params[:live_code_text].to_h,
saved: params.key?(:save),
submitted: params.key?(:submit))
end
end end

View File

@ -37,6 +37,9 @@ class Candidate < ApplicationRecord
"#{my_status.progress}%" "#{my_status.progress}%"
end end
# FIXME: This feels wrong here. Mail deliveries should be in controller.
# Privatize in QuizController
# also, bang methods in ruby do an action and replace, or return nil
def complete! def complete!
if update_attributes(completed: true) if update_attributes(completed: true)
CandidateMailer.submitted(self).deliver_now CandidateMailer.submitted(self).deliver_now

View File

@ -55,6 +55,14 @@ class CandidateControllerTest < ActionDispatch::IntegrationTest
assert_redirected_to thankyou_path assert_redirected_to thankyou_path
end end
test 'should reset session' do
setup_auth candidates(:dawn)
get thankyou_path
assert :success
assert session[:test_id].nil?
end
test "should get summary if complete but not submitted" do test "should get summary if complete but not submitted" do
setup_auth candidates(:dawn) setup_auth candidates(:dawn)

View File

@ -16,13 +16,75 @@ class QuizControllerTest < ActionDispatch::IntegrationTest
assert_redirected_to login_path assert_redirected_to login_path
end end
# TODO: test models/candidate.complete! == false
# test 'should gracefully fail on summary submit' do
# # if the mailers fail, should go to thank you still
# end
test "should redirect to saved on save" do
setup_auth candidates(:dawn)
qid = questions(:fed5).id
post post_answer_path, params: { save: 'Save', answer: { question_id: qid, radio: 'an option' } }
assert_redirected_to saved_path
assert session[:test_id].present?
end
test "should redirect to next question on next" do
setup_auth candidates(:roy)
qid = questions(:fed3).id
params = { submit: 'Next', answer: { question_id: qid, live_code: { text: 'stuff' } } }
post post_answer_path, params: params
assert_redirected_to question_path
assert session[:test_id].present?
end
test "should get flash message on bad radio response" do test "should get flash message on bad radio response" do
setup_auth candidates(:martha) setup_auth candidates(:dawn)
qid = questions(:fed1).id qid = questions(:fed5).id
post post_answer_path, params: { answer: { question_id: qid, radio: nil } } post post_answer_path, params: { answer: { question_id: qid, radio: nil } }
assert_response :success assert_response :success
assert session[:test_id].present? assert session[:test_id].present?
assert_equal qid, flash[:error] assert_equal qid, flash[:error]
end end
test "should get flash message on bad text response" do
setup_auth candidates(:dawn)
qid = questions(:fed4).id
post post_answer_path, params: { answer: { question_id: qid, text: nil } }
assert_response :success
assert session[:test_id].present?
assert_equal qid, flash[:error]
end
test "should process checkbox" do
setup_auth candidates(:dawn)
qid = questions(:fed10).id
post post_answer_path, params: { answer: { question_id: qid, checkbox: 'an-option' } }
assert_response :success
assert session[:test_id].present?
end
test 'should handle XHR update and complete progress' do
setup_auth candidates(:peggy)
qid = questions(:fed10).id
post post_answer_path, xhr: true, params: { answer: { question_id: qid, checkbox: ['an-option'] } }
assert_response :success
assert_match(/updated successfully/, JSON.parse(@response.body)['message'])
assert_equal 100, JSON.parse(@response.body)['progress']
end
test 'should handle XHR fail' do
setup_auth candidates(:peggy)
qid = questions(:fed10).id
post post_answer_path, xhr: true, params: { answer: { question_id: qid, checkbox: nil } }
assert_response 400
assert_match(/select.*answer/i, JSON.parse(@response.body).join)
end
end end

View File

@ -11,6 +11,14 @@ class RecruiterControllerTest < ActionDispatch::IntegrationTest
assert_response :success assert_response :success
end end
test 'should logout and reset session' do
setup_auth
get recruiter_logout_path
assert :success
assert session[:user].nil?
end
test "should require auth or redirect" do test "should require auth or redirect" do
get recruiter_url get recruiter_url
assert_redirected_to recruiter_login_path assert_redirected_to recruiter_login_path
@ -55,8 +63,8 @@ class RecruiterControllerTest < ActionDispatch::IntegrationTest
end end
test "should create new candidate" do test "should create new candidate" do
# recruiter = users(:recruiter)
setup_auth setup_auth
assert_difference("ActionMailer::Base.deliveries.size", 2) do assert_difference("ActionMailer::Base.deliveries.size", 2) do
assert_difference("Candidate.count") do assert_difference("Candidate.count") do
post create_candidate_path, params: { candidate: post create_candidate_path, params: { candidate:
@ -66,4 +74,17 @@ class RecruiterControllerTest < ActionDispatch::IntegrationTest
assert_redirected_to recruiter_path assert_redirected_to recruiter_path
assert flash[:notice] assert flash[:notice]
end end
test "should fail creation with message" do
setup_auth
assert_difference("ActionMailer::Base.deliveries.size", 0) do
assert_difference("Candidate.count", 0) do
post create_candidate_path, params: { candidate:
{ name: 'new name', email: 'test@mailinatorcom', experience: '0-3', quiz_id: quizzes(:fed).id } }
end
end
assert :success
assert_match(/failed.*save/i, flash[:error])
end
end end

View File

@ -52,4 +52,12 @@ class ReviewControllerTest < ActionDispatch::IntegrationTest
get review_test_url(candidates(:richard).test_hash) get review_test_url(candidates(:richard).test_hash)
assert_response :success assert_response :success
end end
test 'should logout and reset session' do
setup_auth
get review_logout_path
assert :success
assert session[:user].nil?
end
end end

View File

@ -0,0 +1,17 @@
require 'test_helper'
class SkillConfigTest < ActiveSupport::TestCase
test "verify sample file exists" do
assert File.exist? "#{Rails.root}/config/application.yml.sample"
end
test "verify config file exists" do
assert File.exist? "#{Rails.root}/config/application.yml"
end
test 'config can load and return proper values' do
skonfig = SkillConfig.new
assert_equal 'localhost', skonfig.mysql_host
end
end

View File

@ -3,8 +3,9 @@ ENV['RAILS_ENV'] ||= 'test'
# https://github.com/colszowka/simplecov # https://github.com/colszowka/simplecov
require 'simplecov' require 'simplecov'
SimpleCov.start 'rails' do SimpleCov.start 'rails' do
add_group 'Models', ['app/models', 'app/validators'] add_group 'Models', %w(app/models app/validators)
add_group 'Services & Workers', ['app/workers', 'app/services'] add_group 'Services & Workers', %w(app/workers app/services)
add_group "Jobs", 'app/jobs'
end end
require File.expand_path('../../config/environment', __FILE__) require File.expand_path('../../config/environment', __FILE__)