email validation
This commit is contained in:
6
test/test_helpers/email_validatable.rb
Normal file
6
test/test_helpers/email_validatable.rb
Normal file
@ -0,0 +1,6 @@
|
||||
# frozen_string_literal: true
|
||||
class EmailValidatable
|
||||
include ActiveModel::Validations
|
||||
attr_accessor :email
|
||||
validates :email, email_format: true
|
||||
end
|
27
test/validators/email_format_validator_test.rb
Normal file
27
test/validators/email_format_validator_test.rb
Normal file
@ -0,0 +1,27 @@
|
||||
# frozen_string_literal: true
|
||||
require 'test_helper'
|
||||
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
|
Reference in New Issue
Block a user