Sha256: ee012f8b5fe590e5535596e0168440d0de4deba7fca7b3da4a425bff498dee0b

Contents?: true

Size: 1.9 KB

Versions: 4

Compression:

Stored size: 1.9 KB

Contents

require 'test_helper.rb'

describe "Phone Validation" do
  def build_phone_validation phone, attrs = {}
    TestRecord.reset_callbacks(:validate)
    TestRecord.validates :phone, :phone => phone
    TestRecord.new attrs
  end


  describe "when no country is given" do
    it 'should validate format of phone with ###-###-####' do
      subject = build_phone_validation true
      subject.phone = '1-999-999-9999'
      subject.valid?.must_equal true
      subject.errors.size.must_equal 0
    end

    it 'should validate format of phone with ##########' do
      subject = build_phone_validation true
      subject.phone = '19999999999'
      subject.valid?.must_equal true
      subject.errors.size.must_equal 0
    end

    it 'should validate format of phone with ###.###.####' do
      subject = build_phone_validation true
      subject.phone = '1999.999.9999'
      subject.valid?.must_equal true
      subject.errors.size.must_equal 0
    end

    it 'should validate format of phone with ### ### ####' do
      subject = build_phone_validation true
      subject.phone = '1999 999 9999'
      subject.valid?.must_equal true
      subject.errors.size.must_equal 0
    end

    it 'should validate format of phone with (###) ###-####' do
      subject = build_phone_validation true
      subject.phone = '1(999) 999-9999'
      subject.valid?.must_equal true
      subject.errors.size.must_equal 0
    end

  end


  describe "for invalid formats" do
    it "rejects invalid formats" do
      subject = build_phone_validation true
      subject.phone = '999'
      subject.valid?.must_equal false
      subject.errors.size.must_equal 1
    end

    it "generates an error message of type invalid" do
      subject = build_phone_validation true
      subject.phone = '999'
      subject.valid?.must_equal false
      subject.errors[:phone].include?(subject.errors.generate_message(:phone, :invalid)).must_equal true
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
activevalidators-2.1.0 test/validations/phone_test.rb
activevalidators-2.0.2 test/validations/phone_test.rb
activevalidators-2.0.1 test/validations/phone_test.rb
activevalidators-2.0.0 test/validations/phone_test.rb