From 7fadc2c164103d96cfc811949ed8214252bfc4fd Mon Sep 17 00:00:00 2001 From: Adam Meehan Date: Wed, 21 May 2008 10:02:13 +1000 Subject: [PATCH] tightened up specs for attribute methods --- lib/validates_timeliness/validations.rb | 2 +- spec/attribute_methods_spec.rb | 19 +++++++++++-------- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/lib/validates_timeliness/validations.rb b/lib/validates_timeliness/validations.rb index fd05da0..039c452 100644 --- a/lib/validates_timeliness/validations.rb +++ b/lib/validates_timeliness/validations.rb @@ -30,7 +30,7 @@ module ValidatesTimeliness # checks if date is valid which enforces number of days in a month unlike Time Date.new(*time_array[0..2]) - # checks if time is valid and return object + # checks if time part is valid and returns object Time.mktime(*time_array) rescue nil diff --git a/spec/attribute_methods_spec.rb b/spec/attribute_methods_spec.rb index de3692f..7d90b4b 100644 --- a/spec/attribute_methods_spec.rb +++ b/spec/attribute_methods_spec.rb @@ -8,30 +8,33 @@ describe ValidatesTimeliness::AttributeMethods do end it "should return string value for attribute_before_type_cast when written as string" do - @person.birth_date_and_time = "1980-12-25 01:02:03" - @person.birth_date_and_time_before_type_cast.should == "1980-12-25 01:02:03" + time_string = "2000-06-01 01:02:03" + @person.birth_date_and_time = time_string + @person.birth_date_and_time_before_type_cast.should == time_string end it "should return Time object for attribute_before_type_cast when written as Time" do - @person.birth_date_and_time = Time.mktime(1980, 12, 25, 1, 2, 3) + @person.birth_date_and_time = Time.mktime(2000, 06, 01, 1, 2, 3) @person.birth_date_and_time_before_type_cast.should be_kind_of(Time) end it "should return Time object using attribute read method when written with string" do - @person.birth_date_and_time = "1980-12-25 01:02:03" + @person.birth_date_and_time = "2000-06-01 01:02:03" @person.birth_date_and_time.should be_kind_of(Time) end unless Rails::VERSION::STRING <= '2.0.2' it "should return stored time string as Time with correct timezone" do - Time.zone = TimeZone['Sydney'] # no I'm not from Sydney but there is no Melbourne timezone! - @person.birth_date_and_time = "1980-12-25 01:02:03" - @person.birth_date_and_time.zone == Time.zone + Time.zone = TimeZone['Melbourne'] + time_string = "2000-06-01 01:02:03" + @person.birth_date_and_time = time_string + @person.birth_date_and_time.utc_offset.should == 10.hours + @person.birth_date_and_time.strftime('%Y-%m-%d %H:%M:%S').should == time_string end end it "should return nil when time is invalid" do - @person.birth_date_and_time = "1980-02-30 01:02:03" + @person.birth_date_and_time = "2000-02-30 01:02:03" @person.birth_date_and_time.should be_nil end