fearless_fool
fearless_fool

Reputation: 35219

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:

Upvotes: 8

Views: 13930

Answers (1)

Jörg W Mittag
Jörg W Mittag

Reputation: 369518

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.

Upvotes: 9

Related Questions