【问题标题】:RSpec Time comparision fails locally, passes CircleCI and HerokuRSpec 时间比较在本地失败,通过 CircleCI 和 Heroku
【发布时间】: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


【解决方案1】:

您很可能在时间计算中存在错误,可能是与时区相关的错误。

当时间比较在测试中失败时,在您的本地计算机上启动 pry 或 byebug 并确定哪个时间是正确的。然后找出其他时间错误的原因并修复它。

【讨论】:

    【解决方案2】:

    建议您始终将所有内容存储为 UTC。记录时间戳、支付时间戳、审计日期等等。使用 UTC 值进行所有日期和时间计算以及时间间隔。这样,即使您的用户处于不同的时区,或者他们在您早上 01:59:59 查看购物车在秋天关闭夏令时。如果您需要向用户显示日期和时间,这是一个演示问题,您可以随时将 UTC 值转换为其本地时区值以进行显示。

    【讨论】:

      【解决方案3】:

      这是一个条纹问题。他们直到发票创建后 1 小时才最终确定发票,因此我需要增加一个小时。 @subscription.current_period_end+1.hour

      【讨论】:

        猜你喜欢
        • 2018-12-18
        • 1970-01-01
        • 1970-01-01
        • 2017-07-22
        • 1970-01-01
        • 2011-10-19
        • 2022-01-20
        • 2012-10-11
        • 2015-11-16
        相关资源
        最近更新 更多