imalik8088
imalik8088

Reputation: 1641

write rspec test based on age or date

I have a method that should return a category of member based on the age/date_of_birth. I want do write some test for that method because it a important method.

And my question is how can I test the model in order not failing the test in future because the date continuously changing.

I try to provide a pseudo test:

IF THE member IS > 18 => OUTPUT: "Adult"  
IF THE member IS < 15 => OUTPUT: "XYZ"

thanks

Upvotes: 0

Views: 429

Answers (2)

Mohammad AbuShady
Mohammad AbuShady

Reputation: 42869

Do you even need to manipulate time? you can simply set the member's birthdate to the past

# build member
let(:adult_member) { Member.new(date_of_birth: 20.years.ago) }
let(:young_member) { Member.new(date_of_birth: 13.years.ago) }

# spec
expect(adult_member.whatever).to eq('Adult')
expect(young_member.whatever).to eq('Young')

Upvotes: 1

willis
willis

Reputation: 189

Use timecop gem to freeze time in spec or TimeHelpers from Rails 4.2 for the same things (see travel method).

Upvotes: 4

Related Questions