LoginSignup
13
14

More than 5 years have passed since last update.

テストにsleep入れるな問題の解決策

Last updated at Posted at 2012-12-14

テストの中でレコードの作成時間をちょっとずらしたい時などに,sleepを入れるという最悪な解決方法はやめてtravisjeffery/timecopを使うとよい.
実行後に時間を置きたいメソッドをFoo#barとすると以下のように書く.

some_spec.rb
class Foo
  def bar_with_timeshift
    val = bar_without_timeshift
    Timecop.travel(Time.now + 10.seconds)
    val
  end
  alias_method_chain :bar, :timeshift
end

timeshiftというかっこいい名前はmotemenさんのgist:325058から.

13
14
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
13
14