我有我的Waiver
模型的age
方法,看起來像:比較失敗
def age(date = nil)
if date.nil?
date = Date.today
end
age = 0
unless date_of_birth.nil?
age = date.year - date_of_birth.year
age -= 1 if date < date_of_birth + age.years #for days before birthday
end
return age
end
然後我有一個規範,看起來像:
it "calculates the proper age" do
waiver = FactoryGirl.create(:waiver, date_of_birth: 12.years.ago)
waiver.age.should == 12
end
當我運行此規格我得到comparison of Date with ActiveSupport::TimeWithZone failed
。我究竟做錯了什麼?
Failures:
1) Waiver calculates the proper age
Failure/Error: waiver.age.should == 12
ArgumentError:
comparison of Date with ActiveSupport::TimeWithZone failed
# ./app/models/waiver.rb:132:in `<'
# ./app/models/waiver.rb:132:in `age'
# ./spec/models/waiver_spec.rb:23:in `block (2 levels) in <top (required)>'
我不知道爲什麼會失敗,但如果你設置DATE_OF_BIRTH到'12.years.ago它工作正常。 to_date'而不是'12.years.ago'。 – oldergod