Created
March 6, 2011 01:57
-
-
Save jamesbrink/856942 to your computer and use it in GitHub Desktop.
My Basic Unit Test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Phone < ActiveRecord::Base | |
belongs_to :phonable, :polymorphic=>true | |
acts_as_audited | |
validates_presence_of :number | |
before_validation :parse_phone | |
def parse_phone | |
if self && self.number !=nil | |
number = self.number.gsub(/\D/,"") | |
if number.length == 10 | |
self.area_code=number.match(/\d{3}/).to_s | |
self.number = number.match(/\d{7}$/).to_s | |
elsif number.length == 11 | |
self.country_code=number.match(/\d{1}/).to_s | |
self.area_code=number.match(/(?<=\d)\d{3}/).to_s | |
self.number = number.match(/\d{7}$/).to_s | |
elsif number.length == 14 | |
self.country_code=number.match(/\d{4}/).to_s | |
self.area_code=number.match(/(?<=\d{4})\d{3}/).to_s | |
self.number = number.match(/\d{7}$/).to_s | |
else | |
self.number=number | |
end | |
end | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'test_helper' | |
class PhoneTest < ActiveSupport::TestCase | |
test "invalid with empty attributes" do | |
phone = Phone.new | |
assert !phone.valid? | |
assert phone.errors[:number].any? | |
end | |
test "phone number 7 digits" do | |
phone = Phone.new | |
phone.number="123-1234" | |
phone.valid? | |
assert !phone.errors[:number].any? | |
assert (phone.area_code == nil) | |
end | |
test "phone number 10 digits" do | |
phone = Phone.new | |
phone.number="123-123-1234" | |
phone.valid? | |
assert !phone.errors[:number].any? | |
assert !phone.errors[:area_code].any? | |
assert (phone.area_code != nil && phone.area_code.length == 3) | |
end | |
test "phone number 11 digits" do | |
phone = Phone.new | |
phone.number="1-123-123-1234" | |
phone.valid? | |
assert !phone.errors[:number].any? | |
assert !phone.errors[:area_code].any? | |
assert !phone.errors[:country_code].any? | |
assert (phone.area_code !=nil && phone.area_code.length == 3 && phone.country_code != nil && phone.country_code.length ==1) | |
end | |
test "phone number 14 digits" do | |
phone = Phone.new | |
phone.number="1-123-123-123-1234" | |
phone.valid? | |
assert !phone.errors[:number].any? | |
assert !phone.errors[:area_code].any? | |
assert !phone.errors[:country_code].any? | |
assert (phone.area_code !=nil && phone.area_code.length == 3 && phone.country_code != nil && phone.country_code.length ==4) | |
end | |
test "phone number non numeric" do | |
phone = Phone.new | |
phone.number="abcdefghijklmnopqrstuvwxyz" | |
assert !phone.valid? | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment