【发布时间】:2020-12-15 19:44:14
【问题描述】:
我的 rspec 测试在下午 6:00 到 6:59 之间在本地计算机上失败,但在本地计算机上在下午 5:59 之前和晚上 7:00 之后通过,
好像是通过了CircleCI和Heroku,但是不知道CircleCI或者Heroku是否在特定时间失败。
我将本地计算机时间设置为 UTC,但 @subscription.current_period_end 似乎有 1 小时的错误。
我怀疑这与我的本地机器如何处理与 CircleCi 和 Heroku 相比的时间有关。任何想法如何解决这个问题?
代码:
def get_proration_date
Time.zone.now.to_i
end
应用程序.rb
config.active_record.default_timezone = :utc
config.time_zone = "UTC"
Rspec
it "should create upcoming_invoice with existing plan for the next_month if there are no changes to subscription(Mock Version)", live: false do
create_stripe_elements
stripe_gold_plan
@subscription = payment_gateway.create_subscription(plan: silver_plan, user: user,
coupon_id: @coupon.stripe_id, source_id_or_token: default_card_token)
invoice = payment_gateway.upcoming_invoice_for_update_plan(subscription: @subscription, to_plan: silver_plan,
coupon_id: "", proration_date: get_proration_date)
expect(invoice.lines.total_count).to eql(1)
expect(invoice.amount_due).to eql(silver_plan.amount)
expect(invoice.total).to eql(silver_plan.amount)
expect(Time.zone.at(invoice.next_payment_attempt)).to eql(@subscription.current_period_end)
delete_stripe_elements
end
错误
失败/错误:expect(Time.zone.at(invoice.next_payment_attempt)).to eql(@subscription.current_period_end)
expected: 2020-09-27 00:20:20.000000000 +0000
got: 2020-09-27 01:20:20.000000000 +0000
(compared using eql?)
Diff:
@@ -1,2 +1,2 @@
-Sun, 27 Sep 2020 00:20:20 UTC +00:00
+Sun, 27 Sep 2020 01:20:20 UTC +00:00
【问题讨论】:
-
你是偶然在美国/加拿大山区吗?我的猜测是您的机器设置为山地时间,目前是 UTC - 6。所以,在下午 6:00 之后。与
Time.zone.new.to_date相比,您的当地时间current_period_end.to_date(可能以UTC 存储)是“明天”。晚上 7 点后本地,你又在同一天。 CircleCI 和 Heroku 默认只是 UTC,所以不要显示问题。要么将两者都转换为本地时区,要么将两者都作为 UTC 进行比较。 -
你可以使用timecop来冻结你的测试时间,这样可以保证一致性
-
@rmlockerd,如何设置 UTC?我以为我是UTC时间
标签: ruby-on-rails ruby datetime timezone