Ho un metodo di age
sul mio modello Waiver
che assomiglia a:confronto di Data con ActiveSupport :: TimeWithZone fallito
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
Ho poi una specifica che assomiglia:
it "calculates the proper age" do
waiver = FactoryGirl.create(:waiver, date_of_birth: 12.years.ago)
waiver.age.should == 12
end
Quando ho eseguire questa specifica ottengo comparison of Date with ActiveSupport::TimeWithZone failed
. Che cosa sto facendo di sbagliato?
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)>'
Non so il motivo per cui sta venendo a mancare, ma si sta lavorando bene se si imposta date_of_birth a '12.years.ago. to_date' e non '12.years.ago'. – oldergod