I'm currently using rspec in a ruby on rails project, to test a method in a model.
In my case, I have a #set_season method which returns the season based on the current_date. So I expect this method to never return nil.
My test is passing just fine, but this seems a very ugly way to do it. I just can't see any other solution for now. But I'm trying to improve my testing skills AND the performance of the codebase that I'm testing. So any help of you girls&guys is welcome 🙏
describe '#set_season' do
it 'is never nil' do
(1..12).each do |month|
(1..31).each do |day|
promotion.update(start_date: Time.new(2021, month, day))
promotion.set_season
expect(promotion.season).not_to be_nil
end
end
end
end
Thanks !
Just to close this subject, I decided to keep testing my method for each day of the year, and improved my loop with a (Date.today..Date.today + 1.year) as suggested.
describe '#set_season' do
it 'is never nil' do
(Date.today..Date.today + 1.year).each do |date|
promotion.update(start_date: date)
promotion.set_season
expect(promotion.season).not_to be_nil
end
end
end