validates input_options based on input type
This commit is contained in:
@ -1,7 +1,14 @@
|
||||
require 'test_helper'
|
||||
|
||||
class QuestionTest < ActiveSupport::TestCase
|
||||
# test "the truth" do
|
||||
# assert true
|
||||
# end
|
||||
test 'should compact arrays for input_options' do
|
||||
question = Question.new(quiz_id: quizzes(:fed).to_i,
|
||||
question: 'foo',
|
||||
category: 'bar',
|
||||
input_type: 'radio',
|
||||
input_options: ['one', 'two', '', ' ', nil])
|
||||
question.validate
|
||||
|
||||
assert_equal 2, question.input_options.count
|
||||
end
|
||||
end
|
||||
|
6
test/test_helpers/input_options_validatable.rb
Normal file
6
test/test_helpers/input_options_validatable.rb
Normal file
@ -0,0 +1,6 @@
|
||||
class InputOptionsValidatable
|
||||
include ActiveModel::Validations
|
||||
attr_accessor :input_type
|
||||
attr_accessor :input_options
|
||||
validates :input_options, input_options_presence: true
|
||||
end
|
49
test/validators/input_options_presence_validator_test.rb
Normal file
49
test/validators/input_options_presence_validator_test.rb
Normal file
@ -0,0 +1,49 @@
|
||||
require 'test_helper'
|
||||
|
||||
class InputOptionsPresenceValidatorTest < ActiveSupport::TestCase
|
||||
test "should pass when inpute type not radio or checkbox" do
|
||||
obj = InputOptionsValidatable.new
|
||||
obj.input_type = "text"
|
||||
|
||||
assert obj.valid?
|
||||
assert_equal 0, obj.errors.messages[:input_options].count
|
||||
end
|
||||
|
||||
test "should fail when missing options for radio" do
|
||||
obj = InputOptionsValidatable.new
|
||||
obj.input_type = "radio"
|
||||
obj.input_options = nil
|
||||
obj.valid?
|
||||
|
||||
refute obj.errors.messages.empty?, 'needs an error message'
|
||||
assert_match(/provide.*option/i, obj.errors.messages[:input_options].join)
|
||||
end
|
||||
|
||||
test "should pass when provided options for radio" do
|
||||
obj = InputOptionsValidatable.new
|
||||
obj.input_type = "radio"
|
||||
obj.input_options = ['one', 'two', nil]
|
||||
|
||||
assert obj.valid?
|
||||
assert_equal 0, obj.errors.messages[:input_options].count
|
||||
end
|
||||
|
||||
test "should fail when missing options for checkbox" do
|
||||
obj = InputOptionsValidatable.new
|
||||
obj.input_type = "checkbox"
|
||||
obj.input_options = nil
|
||||
obj.valid?
|
||||
|
||||
refute obj.errors.messages.empty?, 'needs an error message'
|
||||
assert_match(/provide.*option/i, obj.errors.messages[:input_options].join)
|
||||
end
|
||||
|
||||
test "should pass when provided options for checkbox" do
|
||||
obj = InputOptionsValidatable.new
|
||||
obj.input_type = "checkbox"
|
||||
obj.input_options = ['one', 'two', nil]
|
||||
|
||||
assert obj.valid?
|
||||
assert_equal 0, obj.errors.messages[:input_options].count
|
||||
end
|
||||
end
|
Reference in New Issue
Block a user