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:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With