skill-assessment-app/test/validators/answer_format_validator_test.rb

166 lines
4.1 KiB
Ruby
Raw Normal View History

2016-08-01 16:27:17 -05:00
require 'test_helper'
class AnswerValidatable
include ActiveModel::Validations
attr_accessor :answer
attr_accessor :question
validates :answer, answer_format: true
MockQuestion = Struct.new(:input_type)
def initialize input_type
@input_type = input_type
end
def question
MockQuestion.new(@input_type)
end
end
class AnswerFormatValidatorTest < ActiveSupport::TestCase
# input_type RADIO
test "radio should pass with string" do
obj = AnswerValidatable.new('radio')
obj.answer = 'option-1'
assert obj.valid?
assert obj.errors.messages.empty?
end
test "radio should FAIL with nil" do
obj = AnswerValidatable.new('radio')
obj.answer = nil
refute obj.valid?
assert_match(/select.*answer/, obj.errors.messages[:answer][0])
end
test "radio should FAIL with empty string" do
obj = AnswerValidatable.new('radio')
obj.answer = ''
refute obj.valid?
assert_match(/select.*answer/, obj.errors.messages[:answer][0])
end
# input_type TEXT
test "text should PASS with string" do
obj = AnswerValidatable.new('text')
obj.answer = "this is a valid answer"
assert obj.valid?
assert obj.errors.messages.empty?
end
test "text should FAIL with nil" do
obj = AnswerValidatable.new('text')
obj.answer = nil
refute obj.valid?
assert_match(/enter.*answer/, obj.errors.messages[:answer][0])
end
test "text should FAIL with empry string" do
obj = AnswerValidatable.new('text')
obj.answer = " "
refute obj.valid?
assert_match(/enter.*answer/, obj.errors.messages[:answer][0])
end
test "text should FAIL with more than 1000 charactures" do
obj = AnswerValidatable.new('text')
obj.answer = SecureRandom.urlsafe_base64(1001)
refute obj.valid?
assert_match(/char.*limit.*1000.$/, obj.errors.messages[:answer][0])
2016-08-01 16:27:17 -05:00
end
# input_type CHECK BOX
test "checkbox should PASS with populated array" do
obj = AnswerValidatable.new('checkbox')
obj.answer = ["", "", "valid answer"]
assert obj.valid?
assert obj.errors.messages.empty?
end
test "checkbox should FAIL with nil" do
obj = AnswerValidatable.new('checkbox')
obj.answer = nil
refute obj.valid?
assert_match(/select.*answer/, obj.errors.messages[:answer][0])
end
test "checkbox should FAIL with empty string" do
obj = AnswerValidatable.new('checkbox')
obj.answer = " "
refute obj.valid?
assert_match(/select.*answer/, obj.errors.messages[:answer][0])
end
test "checkbox should FAIL with array of empty strings" do
obj = AnswerValidatable.new('checkbox')
obj.answer = ["", "", " "]
refute obj.valid?
assert_match(/select.*answer/, obj.errors.messages[:answer][0])
end
# input_type LIVE CODER
test "live_code should PASS with populated hash" do
obj = AnswerValidatable.new('live_code')
obj.answer = { html: 'this is html', css: '', js: '', text: 'some reasons' }
2016-08-01 16:27:17 -05:00
assert obj.valid?
assert obj.errors.messages.empty?
end
test "live_code should PASS with finish later" do
obj = AnswerValidatable.new('live_code')
obj.answer = { later: "true" }
assert obj.valid?
assert obj.errors.messages.empty?
end
test "live_code should FAIL with nil" do
obj = AnswerValidatable.new('live_code')
obj.answer = nil
refute obj.valid?
assert_match(/write.*code/, obj.errors.messages[:answer][0])
end
test "live_code should FAIL without checking finish later" do
obj = AnswerValidatable.new('live_code')
obj.answer = { later: "" }
2016-08-01 16:27:17 -05:00
refute obj.valid?
assert_match(/come back/, obj.errors.messages[:answer][0])
2016-08-01 16:27:17 -05:00
end
test "live_code should FAIL without values" do
obj = AnswerValidatable.new('live_code')
obj.answer = { later: "", html: "", css: "", js: "", text: "" }
2016-08-01 16:27:17 -05:00
refute obj.valid?
assert_match(/write.*code/, obj.errors.messages[:answer][0])
end
test "live_code should PASS with text only" do
obj = AnswerValidatable.new('live_code')
obj.answer = { later: "", html: "", css: "", js: "", text: "reasons" }
assert obj.valid?
assert obj.errors.messages.empty?
end
2016-08-01 16:27:17 -05:00
end