Skip to content

Commit

Permalink
chore: fix flaky tests
Browse files Browse the repository at this point in the history
Some tests are failing because query params are in wrong order.

My fix is to ignore order and just checking all were set with correct
value.
  • Loading branch information
robuye committed Mar 18, 2024
1 parent fe30022 commit b86ff54
Showing 1 changed file with 15 additions and 10 deletions.
25 changes: 15 additions & 10 deletions test/support/stripe_case.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,27 @@ defmodule Stripe.StripeCase do
use ExUnit.CaseTemplate

def assert_stripe_requested(expected_method, path, extra \\ []) do
expected_url = build_url(path, Keyword.get(extra, :query))
expected_params = Keyword.get(extra, :query, %{})
expected_path = URI.parse(path).path
expected_body = Keyword.get(extra, :body)
expected_headers = Keyword.get(extra, :headers)

assert_received({method, url, headers, body, _})

actual_uri = URI.parse(url)

actual_query_params =
to_string(actual_uri.query)
|> URI.query_decoder()
|> Enum.into(%{})

Enum.each(expected_params, fn {key, value} ->
actual_val = Map.get(actual_query_params, to_string(key))
assert actual_val == to_string(value)
end)

assert expected_method == method
assert expected_url == url
assert expected_path == actual_uri.path

assert_stripe_request_body(expected_body, body)
assert_stripe_request_headers(expected_headers, headers)
Expand Down Expand Up @@ -51,14 +64,6 @@ defmodule Stripe.StripeCase do
assert body == Stripe.URI.encode_query(expected_body)
end

defp build_url(path, nil) do
stripe_base_url() <> path
end

defp build_url(path, query_params) do
stripe_base_url() <> path <> "?" <> URI.encode_query(query_params)
end

defmodule HackneyMock do
@doc """
Send message to the owning process for each request so we can assert that
Expand Down

0 comments on commit b86ff54

Please sign in to comment.