Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rspec unit testing a method which has a infinite loop

I have a method like this

class SomeClass
  def self.test_method
    while(true)
      make_heavy_lifting
      sleep(60*60)
    end
  end
end

How do i write test for this method.

one solution i thought of is stubbing sleep method and it worked but i am not able to stub the while keyword. please suggest best practices for unit testing a method which has infinite loop.

like image 985
Sharath B. Patel Avatar asked Jan 15 '23 01:01

Sharath B. Patel


1 Answers

Maybe you can move everything inside the while except the sleep to another method, then you write specs only for that method, I don't think you really need to test that your method has a while loop and a sleep (you don't need to test EVERY line of your code :P) but if you want to do that check this question, it has some things you can try What is the best practice when it comes to testing "infinite loops"?

like image 96
arieljuod Avatar answered Jan 22 '23 06:01

arieljuod