RSpec: stubbing Kernel::sleep?

Viewed 17810

Is there a way to stub Kernel.sleep in an rspec scenario?

8 Answers

If you're using Mocha, then something like this will work:

def setup
  Kernel.stubs(:sleep)
end

def test_my_sleepy_method
  my_object.take_cat_nap!
  Kernel.assert_received(:sleep).with(1800) #should take a half-hour paower-nap
end

Or if you're using rr:

def setup
  stub(Kernel).sleep
end

def test_my_sleepy_method
  my_object.take_cat_nap!
  assert_received(Kernel) { |k| k.sleep(1800) }
end

You probably shouldn't be testing more complex threading issues with unit tests. On integration tests, however, use the real Kernel.sleep, which will help you ferret out complex threading issues.

In pure rspec:

before do
  Kernel.stub!(:sleep)
end

it "should sleep" do
  Kernel.should_receive(:sleep).with(100)
  Object.method_to_test #We need to call our method to see that it is called
end

Here's the newer way of stubbing Rspec with Kernal::Sleep.

This is basically an update to the following answer: Tony Pitluga's answer to the same question

class Foo
  def self.some_method
    sleep 5
  end
end

it "should call sleep" do
  allow_any_instance_of(Foo).to receive(:sleep)
  expect(Foo).to receive(:sleep).with(5)
  Foo.some_method
end

For rspec version 1, the stubbing syntax has changed. This works:

allow_any_instance_of(Kernel).to receive(:sleep).and_return("")

When I use it.

Related