![](/img/trans.png)
[英]Rspec test with HTTP post request passes locally but fails on CI server
[英]RSpec Time comparision fails locally, passes CircleCI and Heroku
我的 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
config.active_record.default_timezone = :utc
config.time_zone = "UTC"
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
很可能您在時間計算中遇到了錯誤,可能是與時區相關的錯誤。
當測試中的時間比較失敗時,在本地機器上啟動 pry 或 byebug 並找出哪個時間是正確的。 然后找出為什么其他時間是錯誤的並修復它。
建議您始終將所有內容存儲為 UTC。 記錄時間戳、付款時間戳、審計日期等等。 使用 UTC 值進行所有日期和時間計算以及間隔。 這樣,即使您的用戶位於不同的時區,或者他們在早上 01:59:59 查看他們的購物車時,您也永遠不會在弄清楚發生了什么以及發生的順序方面遇到問題。在秋天關閉夏令時。 如果您需要向用戶顯示日期和時間,這是一個演示問題,您始終可以將 UTC 值轉換為其本地時區值以進行顯示。
這是一個條紋問題。 他們直到發票創建后 1 小時才完成發票,因此我需要添加一個小時。 @subscription.current_period_end+1.hour
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.