Skip to content

Commit

Permalink
Refactor the validator specs with a new RSpec matcher
Browse files Browse the repository at this point in the history
  • Loading branch information
MarceloCajueiro committed Apr 15, 2012
1 parent 61a0771 commit cfc2698
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 47 deletions.
80 changes: 33 additions & 47 deletions spec/mask_validator_spec.rb
Expand Up @@ -2,105 +2,91 @@

describe MaskValidator do
subject do
Person.new :phone => '(12) 3456-7890',
:fax => '(12) 3456-7890',
:acronym => 'ABC',
:alphanumeric => 'AAA666',
:zip_code => '88900-000',
:birth_date => '13/10/1989',
:birth_time => '10:20',
:birth_year => '2011',
:body_fat => '23,44',
:custom => '989.78',
:identification => '53.86'
end

it "Person should be valid" do
subject.should be_valid
Person.new
end

# validates :phone, :mask => "(99) 9999-9999", :allow_blank => true
context "mask validation to phone" do
it "should be valid with a phone in according of the mask" do
subject.phone = '(48) 9874-4569'
subject.should be_valid
subject.should have_valid_value_to(:phone)
end

it "should not be valid with a phone with a wrong pattern" do
subject.phone = '4852458787'
subject.should_not be_valid
subject.should_not have_valid_value_to(:phone)

subject.phone = '48 9865 4879'
subject.should_not be_valid
subject.should_not have_valid_value_to(:phone)

subject.phone = '(48) 9874-45169'
subject.should_not be_valid
subject.should_not have_valid_value_to(:phone)

subject.phone = '(48)98956698'
subject.should_not be_valid
subject.should_not have_valid_value_to(:phone)
end
end

# validates :acronym, :mask => "***", :allow_nil => true
context "mask validation to acronym" do
it "should be valid with a acronym in according of the mask" do
subject.acronym = '1gd'
subject.should be_valid
subject.should have_valid_value_to(:acronym)

subject.acronym = '666'
subject.should be_valid
subject.should have_valid_value_to(:acronym)

subject.acronym = 'zzz'
subject.should be_valid
subject.should have_valid_value_to(:acronym)
end

it "should not be valid with a acronym with a wrong pattern" do
subject.acronym = '1qw1'
subject.should_not be_valid
subject.should_not have_valid_value_to(:acronym)
end
end

# validates :alphanumeric, :mask => "aaa999"
context "mask validation to alphanumeric" do
it "should be valid with a alphanumeric in according of the mask" do
subject.alphanumeric = 'awe987'
subject.should be_valid
subject.should have_valid_value_to(:alphanumeric)
end

it "should not be valid with a alphanumeric with a wrong pattern" do
subject.alphanumeric = '999999'
subject.should_not be_valid
subject.should_not have_valid_value_to(:alphanumeric)

subject.alphanumeric = 'QQQQQQ'
subject.should_not be_valid
subject.should_not have_valid_value_to(:alphanumeric)

subject.alphanumeric = '666aaaa'
subject.should_not be_valid
subject.should_not have_valid_value_to(:alphanumeric)

subject.alphanumeric = '666AAA'
subject.should_not be_valid
subject.should_not have_valid_value_to(:alphanumeric)
end
end

context "when the attributes type is not a string" do
it "should not be valid with a wrong date format" do
subject.birth_date = "15-12-2009"
subject.should be_invalid
subject.should_not have_valid_value_to(:birth_date)
end

it "should not be valid with a wrong birth year" do
subject.birth_year = 20110
subject.should be_invalid
subject.should_not have_valid_value_to(:birth_year)
end

it "should not be valid with a wrong birth time" do
subject.birth_time = "333:20"
subject.should be_invalid
subject.should_not have_valid_value_to(:birth_time)
end

it "should not be valid with a wrong body fat" do
subject.body_fat = 333.00
subject.should be_invalid
subject.should_not have_valid_value_to(:body_fat)
end
end

Expand All @@ -109,13 +95,13 @@
it "should be valid with an correct custom value" do
subject.stub!(:custom_mask => "99999-999")
subject.custom = '32632-567'
subject.should be_valid
subject.should have_valid_value_to(:custom)
end

it "should not be valid with an empty custom value" do
subject.stub!(:custom_mask => "9.9.9")
subject.custom = ''
subject.should_not be_valid
subject.should_not have_valid_value_to(:custom)
end
end

Expand All @@ -124,78 +110,78 @@
it "should be valid with an correct value" do
subject.stub!(:proc_mask => "999.9.99")
subject.identification = '326.3.67'
subject.should be_valid
subject.should have_valid_value_to(:identification)
end

it "should not be valid with an empty value" do
subject.stub!(:prock_mask => "9.9.9")
subject.identification = ''
subject.should_not be_valid
subject.should_not have_valid_value_to(:identification)
end
end

# validates :phone, :mask => "99999-999", :allow_blank => true
context "mask validation to phone with explicit ':allow_blank => true'" do
it "should not be valid with an nil phone" do
subject.phone = nil
subject.should be_valid
subject.should have_valid_value_to(:phone)
end

it "should be valid with an empty phone" do
subject.phone = ''
subject.should be_valid
subject.should have_valid_value_to(:phone)
end
end

# validates :acronym, :mask => "***", :allow_nil => true
context "mask validation to acronym with explicit ':allow_nil => true'" do
it "should be valid with an nil acronym" do
subject.acronym = nil
subject.should be_valid
subject.should have_valid_value_to(:acronym)
end

it "should be valid with an empty acronym" do
subject.acronym = ''
subject.should be_invalid
subject.should_not have_valid_value_to(:acronym)
end
end

# validates :fax, :mask => "(99) 9999-9999", :allow_nil => false
context "mask validation to fax with explicit ':allow_nil => false'" do
it "should not be valid with an nil fax" do
subject.fax = nil
subject.should be_invalid
subject.should_not have_valid_value_to(:fax)
end

it "should be valid with an empty fax" do
subject.fax = ''
subject.should be_invalid
subject.should_not have_valid_value_to(:fax)
end
end

# validates :zip_code, :mask => "99999-999", :allow_blank => false
context "mask validation to zip_code with explicit ':allow_blank => false'" do
it "should not be valid with an nil zip_code" do
subject.zip_code = nil
subject.should be_invalid
subject.should_not have_valid_value_to(:zip_code)
end

it "should be valid with an empty zip_code" do
subject.zip_code = ''
subject.should be_invalid
subject.should_not have_valid_value_to(:zip_code)
end
end

# validates :birth_date, :mask => '99/99/9999'
context "mask validation to birth_date with implicit ':allow_blank => false'" do
it "should not be valid with an nil birth_date" do
subject.birth_date = nil
subject.should be_invalid
subject.should_not have_valid_value_to(:birth_date)
end

it "should be valid with an empty birth_date" do
subject.birth_date = ''
subject.should be_invalid
subject.should_not have_valid_value_to(:birth_date)
end
end
end
16 changes: 16 additions & 0 deletions spec/spec_helper.rb
Expand Up @@ -5,3 +5,19 @@

require "support/db/schema"
require "support/models/person"

RSpec::Matchers.define :have_valid_value_to do |attribute|
match do |actual|
actual.valid?

!actual.errors[attribute].include?("is invalid")
end

failure_message_for_should do |actual|
"expected that the #{attribute} attribute would be valid"
end

failure_message_for_should_not do |actual|
"expected that the #{attribute} attribute would be invalid"
end
end

0 comments on commit cfc2698

Please sign in to comment.