[go: up one dir, main page]

Skip to content

Follow up: Empty VCR cassette failure in spec/services/reconciliations/order_api_spec.rb solution to avoid mocking

Problem

Original issue: https://gitlab.com/gitlab-org/customers-gitlab-com/-/issues/10737+.

The specs in spec/services/reconciliations/order_api_spec.rb are currently failing in the empty_vcr_services job. Job link.

1st Try error in ./spec/services/reconciliations/order_api_spec.rb:129:
Reconciliation could not be updated. Error details: {:message=>"quoted_invoice_amount does not match latest preview amount", :id=>14, :quoted_invoice_amount=>0.234257e4, :quoted_invoice_at=>Tue, 03 Sep 2024 00:00:00.000000000 UTC +00:00, :latest_preview_amount=>2351.14}

The initial solution added a mock for the preview request to avoid hitting a failure due to this direct comparison. However, this avoids running actual logic, which is problematic.

Proposal

This is not the first time that I'm seeing this amount being updated in this spec file. A while ago, I tried to mitigate the need to update by adding some "flexibility" to an assertion:

# spec/services/reconciliations/order_api_spec.rb:72

      # The amount_without_tax returned from Zuora can vary a bit based on number of days remaining in subscription (which can differ slightly based on whether remaining
      # months have 30 or 31 days).
      expect(invoice.amount_without_tax).to be_within(amount_without_tax * 0.01).of(amount_without_tax)

This time around, how about then adding some related logic in the ReconciliationService? Something like:

# app/services/reconciliation_service.rb

def raise_error(details)
  return if Rails.env.test? && diff_within_tolerance_for?(details)

  ...
end

def diff_within_tolerance_for?(details)
  (details[:quoted_invoice_amount] / details[:latest_preview_amount]).between?(0.99, 1.01)
end

With something like this, we won't have to update this amount in the spec file periodically.

Source: Make ReconciliationService logic more flexible ... (#8595 - closed).

Edited by Michael Lunøe