Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

370
Views
Rspec: How to test efficiently a method for each day of the year?

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 !

over 4 years ago ยท Santiago Trujillo
1 answers
Answer question

0

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
over 4 years ago ยท Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
ยฉ 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!