简体   繁体   English

RSpec 时间比较在本地失败,通过 CircleCI 和 Heroku

[英]RSpec Time comparision fails locally, passes CircleCI and Heroku

My rspec test fails on local machine between 6:00pm and 6:59pm, but passes at before 5:59pm and after 7:00pm on the local machine,我的 rspec 测试在下午 6:00 到下午 6:59 之间在本地机器上失败,但在下午 5:59 之前和晚上 7:00 之后在本地机器上通过,

It seems to pass on CircleCI and Heroku, but I do not know if CircleCI or Heroku fail at specific times.它似乎传递了 CircleCI 和 Heroku,但我不知道 CircleCI 或 Heroku 是否在特定时间失败。

I set my local computer time to UTC, and it seems @subscription.current_period_end is incorrect by 1 hour.我将本地计算机时间设置为 UTC, @subscription.current_period_end似乎错误了 1 小时。

I suspect it has something to do with how my local machine handles the time vs CircleCi and Heroku.我怀疑这与我的本地机器如何处理 CircleCi 和 Heroku 的时间有关。 Any ideas how to address this issue?任何想法如何解决这个问题?

Code:代码:

def get_proration_date
  Time.zone.now.to_i
end

Application.rb应用程序.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

Error错误

Failure/Error: expect(Time.zone.at(invoice.next_payment_attempt)).to eql(@subscription.current_period_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

Most likely you have a bug in time calculations, potentially a time zone-related one.很可能您在时间计算中遇到了错误,可能是与时区相关的错误。

Fire up pry or byebug on your local machine when the time comparison fails in the test and figure out which time is the correct one.当测试中的时间比较失败时,在本地机器上启动 pry 或 byebug 并找出哪个时间是正确的。 Then figure out why the other time is wrong and fix that.然后找出为什么其他时间是错误的并修复它。

Suggest that you always store everything as UTC.建议您始终将所有内容存储为 UTC。 Log timestamps, payment timestamps, audit dates, whatever.记录时间戳、付款时间戳、审计日期等等。 Do all your date and time calculations and intervals using the UTC values.使用 UTC 值进行所有日期和时间计算以及间隔。 That way you never ever have an issue with figuring out what happened, and the order in which it happened, even when your users are in different time zones, or they check out their shopping cart at 01:59:59 on the morning when you switch out of daylight saving time in the autumn.这样,即使您的用户位于不同的时区,或者他们在早上 01:59:59 查看他们的购物车时,您也永远不会在弄清楚发生了什么以及发生的顺序方面遇到问题。在秋天关闭夏令时。 If you have a requirement to display dates and times to your users, this is a presentation issue and you can always convert the UTC values to their local timezone values for display purposes.如果您需要向用户显示日期和时间,这是一个演示问题,您始终可以将 UTC 值转换为其本地时区值以进行显示。

It was a stripe issue.这是一个条纹问题。 They do not finalize an invoice until 1 hour after invoice is created, hence I needed to add an hour.他们直到发票创建后 1 小时才完成发票,因此我需要添加一个小时。 @subscription.current_period_end+1.hour @subscription.current_period_end+1.hour

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 具有HTTP发布请求的Rspec测试在本地通过,但在CI服务器上失败 - Rspec test with HTTP post request passes locally but fails on CI server RSpec失败,然后通过,然后失败 - RSpec fails then passes then fails Feature Spec在RSpec中每次都通过,在Jenkins中失败 - Feature Spec passes every time in RSpec, Fails in Jenkins Rspec通过类方法但未通过实例方法 - Rspec passes class method but fails instance method Rspec传递单个规范文件,但在整个目录上失败 - Rspec passes on individual spec files but fails on whole directory Rails会话第一次失败并通过重新加载 - Rails sessions fails first time and passes on reload 延迟的作业在heroku上失败,但在本地运行 - Delayed Job fails on heroku but runs locally 预编译资产在Heroku部署上失败,但可在本地工作 - Precompiling assets fails on Heroku deploy but works locally 通过“ rake spec”调用时,Rspec水豚规格失败,但通过“ rspec spec”调用时,Rspec水豚规格失败 - Rspec capybara spec fails when invoked via 'rake spec' but passes when invoked via 'rspec spec' 无法在Heroku上创建表-迁移在本地有效,但在Heroku上失败 - Failure to Create Table on Heroku — Migration works locally but fails on Heroku
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM