Stub sleep with RSpec

与世无争的帅哥 提交于 2019-12-12 03:18:37

问题


I have the following code in my app:

def timeout_tasks
  10.times do
    # Work...
    sleep 2
  end
end

I would like to test this method, but need to stub sleep to I don't have to wait that much for the test to finish.

How can I do this? I am aware of the allow(Object).to receive(:sleep) but I'm not sure what the Object is. Or is there any other better approach?


回答1:


You should stub sleep on the object it is called on

Eg.

class SleepTest
  def sleep_method
    sleep 2
  end
end

And in your test

sleep_test = SleepTest.new
allow(sleep_test).to receive(:sleep)



回答2:


I looks like the sleep method is defined in Kernel so that would be your Object.



来源:https://stackoverflow.com/questions/32399175/stub-sleep-with-rspec

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!