6
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Time.currentとDBに入れた値との比較テストを書くときに微妙に差が出てつらい問題

Last updated at Posted at 2015-09-17
Timecop.freeze do
  hoge.activate!

  assert_equal Time.current, hogs.activated_at
end
No visible difference in the ActiveSupport::TimeWithZone#inspect output.

こんなのが出る。

調べてみると、

Time.current.to_f # => 1442454360.028926
hoge.activated_at # => 1442454360.0

microsecの差分のせいなので、そこを無視するようにTime.freezeする。

Timecop.freeze(Time.current.change(usec: 0))
Timecop.freeze(Time.current.change(usec: 0)) do
  hoge.activate!

  assert_equal Time.current, hogs.activated_at
end
6
5
1

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
6
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?