mercredi 6 mai 2015

date based condition check in ruby

I want to check a category based on the age of a member. My test are looking like this:

it "should return 'CATEGORY B' if member turn 15 on the 01-11-2015" do
  Timecop.freeze(Date.parse("01-11-2015"))
  @member.date_of_birth = "2000-11-01"
  expect(@member.category).to eq('CATEGORY B')
end

it "should return 'CATEGORY C' if member turn 15 on the 31-10-2015" do
  Timecop.freeze(Date.parse("01-11-2015"))
  @member.date_of_birth = "2000-10-31"
  expect(@member.category).to eq('CATEGORY C')
end

and my method in the model is looking like this:

def category
  local_age = age
  next_november = "01-11-#{Date.today.year}"
  last_day_in_oktober = "31-10-#{Date.today.year}"
  if local_age < 7 then
    'CATEGORY A'
  elsif local_age >= 7 && local_age < 15
    'CATEGORY B'
  elsif local_age > 15 && local_age < 40
    'CATEGORY C'
  elsif local_age >= 40
    'CATEGORY D'
  end
end

How can I implement the and freeze the time in the model in order pass the tests? Any idea

Aucun commentaire:

Enregistrer un commentaire