email validator tests, live coding later error message

This commit is contained in:
Mark Moser 2016-08-01 19:35:10 -05:00
parent f4be785b61
commit f06aed6541
5 changed files with 50 additions and 8 deletions

View File

@ -6,10 +6,9 @@ class Candidate < ApplicationRecord
before_validation(:generate_test_hash, on: :create)
validates_presence_of :name
validates_presence_of :email
validates_presence_of :experience
validates_presence_of :recruiter_id
validates_presence_of :name
validates_presence_of :experience
validates :email, uniqueness: true, presence: true, email_format: true
validates :test_hash, uniqueness: true, presence: true

View File

@ -33,7 +33,11 @@ class AnswerFormatValidator < ActiveModel::EachValidator
def live_code record, attribute, value
return unless value.nil? || value.values.join.blank?
msg = "You must write code in one of the above textareas to progress."
msg = if value.present? && value.keys.count == 1
"Please check that you will come back to complete the code example."
else
"You must write code in one of the above textareas to progress."
end
record.errors[attribute] << (options[:message] || msg)
end
end

View File

@ -9,8 +9,6 @@ class EmailFormatValidator < ActiveModel::EachValidator
(v.strip =~ /^([\w\.%\+\-]+)@([\w\-]+\.)+([\w]{2,})$/i) || v.strip.blank?
end
if results.include?(false)
record.errors[attribute] << (options[:message] || "is not formatted properly")
end
record.errors[attribute] << (options[:message] || "is not formatted properly") if results.include?(false)
end
end

View File

@ -144,7 +144,7 @@ class AnswerFormatValidatorTest < ActiveSupport::TestCase
obj.answer = { "later" => "" }
refute obj.valid?
assert_match(/write.*code/, obj.errors.messages[:answer][0])
assert_match(/come back/, obj.errors.messages[:answer][0])
end
test "live_code should FAIL without values" do

View File

@ -0,0 +1,41 @@
require 'test_helper'
class EmailValidatable
include ActiveModel::Validations
attr_accessor :email
validates :email, email_format: true
end
class EmailFormatValidatorTest < ActiveSupport::TestCase
test "tld length" do
obj = EmailValidatable.new
obj.email = "me@no.yes.x"
refute obj.valid?, 'allowed single length tld'
obj.email = "me@no.yes.co"
assert obj.valid?, 'did not allow tld length 2'
obj.email = "me@no.yes.com"
assert obj.valid?, 'did not allow tld length 3'
obj.email = "me@no.yes.commets"
assert obj.valid?, 'did not allow tld length > 3'
end
test "can handle comma seperated addresses" do
obj = EmailValidatable.new
obj.email = "me@no.yes, me@yes.no"
assert obj.valid?, 'did not allow multiple address [comma seperated]'
end
test "provides proper error message" do
obj = EmailValidatable.new
obj.email = "this is a bad email address"
obj.valid?
refute obj.errors.messages.empty?, 'needs an error message'
assert_match(/not formatted properly/, obj.errors.messages[:email].join)
end
end