Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what sleep() does Rails use?

I am testing a piece of Rails code that reads:

sleep(10.0)

In my RSpec tests, calling:

Kernel.should_receive(:sleep).exactly(1).time

failed and the test slept for ten seconds. This led me to conclude that sleep() in a Rails program isn't calling Kernel.sleep(). I verified this by changing my Rails code to:

Kernel.sleep(10.0)

... after which my RSpec tests passed (and the test didn't sleep).

This leads to a specific and a general question:

  • What implementation of sleep() does Rails use (I'm running Ruby 1.9.3 / Rails 3.2.1)?
  • From the interpreter, what's the easiest way to find the source code for any function?
like image 293
fearless_fool Avatar asked Jul 03 '12 11:07

fearless_fool


1 Answers

The implicit receiver, when you don't specify an explicit one, is self, not Kernel. (Why would you think that?)

So,

sleep(10.0)

is roughly the same as

self.sleep(10.0)

and not at all the same as

Kernel.sleep(10.0)

So, it is calling Kernel#sleep on self and not on Kernel. Which means you need to set an expectation on whatever object self is in that particular method.

like image 129
Jörg W Mittag Avatar answered Sep 20 '22 05:09

Jörg W Mittag