rspec-time_stop (0.1.0): Stop current time in example. https://t.co/7RArS5QO0o
— RubyGems (@rubygems) 2018年8月25日
モチベーション
前職の spec/support/
によくあったテスト系の便利ヘルパを現職でも使いたくなったので記憶からサルベージしてgem化しました
使い方
it
や describe
に :time_stop
をつけるだけで現在時刻を止めた状態でテストを実行します
実装はたったこれだけです
https://github.com/sue445/rspec-time_stop/blob/v0.1.0/lib/rspec/time_stop.rb
例
READMEからコピペ
RSpec.describe Rspec::TimeStop do context "with :time_stop", :time_stop do it "current time is freezed" do before_time = Time.now sleep 1 after_time = Time.now expect(after_time).to eq before_time end end context "without :time_stop" do it "current time is not freezed" do before_time = Time.now sleep 1 after_time = Time.now expect(after_time).to be > before_time end it "current time is freezed when :time_stop is granted", :time_stop do before_time = Time.now sleep 1 after_time = Time.now expect(after_time).to eq before_time end end end
備考
原状対応してるのは ActiveSupport::Testing::TimeHelpers
だけです。
(timecop対応も一応考えたんだけどCI込みだと両方対応するのがちょい面倒だった)