diff --git a/spec/mask_validator_spec.rb b/spec/mask_validator_spec.rb index 32cbe3d..0cd6789 100644 --- a/spec/mask_validator_spec.rb +++ b/spec/mask_validator_spec.rb @@ -2,42 +2,28 @@ 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 @@ -45,18 +31,18 @@ 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 @@ -64,43 +50,43 @@ 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 @@ -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 @@ -124,13 +110,13 @@ 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 @@ -138,12 +124,12 @@ 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 @@ -151,12 +137,12 @@ 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 @@ -164,12 +150,12 @@ 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 @@ -177,12 +163,12 @@ 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 @@ -190,12 +176,12 @@ 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 diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 4b298f1..05ed387 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -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