如何在我的规范中使用 Timecop 正确冻结时间?

发布于 2024-11-26 12:40:15 字数 926 浏览 1 评论 0原文

我正在尝试结合使用 Timecop 和查询 arel where_sql 数据,但我似乎无法让 Timecop 真正冻结时间。我尝试过 Timecop.freeze 和 Timecop.freeze(Time.now),在我的规范中使用 Time.now 时,这两个方法都略有偏差。

我缺少什么? Ruby 1.9.2、Rails 3.1.0.rc5

--

错误

Failure/Error: Game.unreleased.arel.where_sql.should eq("WHERE (release_date > '#{Time.now}')")


     expected "WHERE (release_date > '0000-01-01 00:00:00 -0500')"
     got "WHERE (release_date > '0000-01-01 05:00:00.000000')"

模型

scope :unreleased, lambda { |limit = 4| where('release_date > ?', Time.now).
                                        order('release_date asc').
                                        limit(limit) }

规范

it "should retrieve games with a release date later than today" do
  Timecop.freeze
  Game.unreleased.arel.where_sql.should eq("WHERE (release_date > '#{Time.now}')")
end

I am trying to use a combination of Timecop and querying the arel where_sql to data, but I can't seem to get Timecop to actually freeze the time. I've tried Timecop.freeze and Timecop.freeze(Time.now), both of which are slightly off when using Time.now in my spec.

What am I missing? Ruby 1.9.2, Rails 3.1.0.rc5

--

error

Failure/Error: Game.unreleased.arel.where_sql.should eq("WHERE (release_date > '#{Time.now}')")


     expected "WHERE (release_date > '0000-01-01 00:00:00 -0500')"
     got "WHERE (release_date > '0000-01-01 05:00:00.000000')"

model

scope :unreleased, lambda { |limit = 4| where('release_date > ?', Time.now).
                                        order('release_date asc').
                                        limit(limit) }

spec

it "should retrieve games with a release date later than today" do
  Timecop.freeze
  Game.unreleased.arel.where_sql.should eq("WHERE (release_date > '#{Time.now}')")
end

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

忆梦 2024-12-03 12:40:15

我在规范中对 timecop 的使用总是如下所示:

Timecop.travel(Time.zone.local(2010, 6, 1, 13, 0, 0)) do
  .. time sensitive spec here ..
end

在处理时间时,使用 Time.zone 代理(Time.zone.now、Time.zone.utc、Time.zone.local 等)通常也是一种很好的做法。导轨应用程序。

My usage of timecop in specs always looks like this:

Timecop.travel(Time.zone.local(2010, 6, 1, 13, 0, 0)) do
  .. time sensitive spec here ..
end

It's also generally good practice to use the Time.zone proxy (Time.zone.now, Time.zone.utc, Time.zone.local, etc) when dealing with time in a rails app.

思念满溢 2024-12-03 12:40:15

我刚刚在运行时使用 RSpec 的 expect 语法运行 Timecop 时遇到了问题:

it "updates :completed_at" do
  Timecop.freeze
  expect(@task.completed_at).to eq(Time.zone.now)
end

时间不匹配。为了解决这个问题,我将 Timecop.freeze 放在 before 子句中。

(我意识到这个问题比较旧,并且 RSpec 的 expect 语法不存在,但我认为将 Timecop.freeze 添加到 before 块或子句可能会帮助那些在原始问题中提到相同问题的人。当然,提出一个新问题并回答它似乎并不值得,因为我的问题与上面的问题非常相似。)

I just had a problem running Timecop with RSpec's expect syntax when I was running:

it "updates :completed_at" do
  Timecop.freeze
  expect(@task.completed_at).to eq(Time.zone.now)
end

Where the times weren't matching. To solve, I put Timecop.freeze in a before clause.

(I realize this question is older and RSpec's expect syntax wasn't around, but I think adding Timecop.freeze to a before block or clause may help people who have the same problem mentioned in the original question. Certainly, it doesn't seem like asking a new question and answering it is worthwhile since my question would be very very similar to the one above.)

若能看破又如何 2024-12-03 12:40:15

前往日期并包括 TimeHelpers,例如:

include ActiveSupport::Testing::TimeHelpers

let!(:archived_date) { Time.zone.now }

travel_to(archived_date) do
  expect(OrderService.getOrder(some_old_order).archived_at).to eq Time.zone.now
end

Travel to the date and include TimeHelpers, example:

include ActiveSupport::Testing::TimeHelpers

let!(:archived_date) { Time.zone.now }

travel_to(archived_date) do
  expect(OrderService.getOrder(some_old_order).archived_at).to eq Time.zone.now
end
始终不够 2024-12-03 12:40:15
require 'timecop'

# freeze time to August 1st 2022 in this block
Timecop.freeze(2022, 8, 1) do
  result = something_that_uses_time()
end
require 'timecop'

# freeze time to August 1st 2022 in this block
Timecop.freeze(2022, 8, 1) do
  result = something_that_uses_time()
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文