Skip to content

Commit 99fd474

Browse files
committed
updated
1 parent 1f91a51 commit 99fd474

1 file changed

Lines changed: 19 additions & 27 deletions

File tree

test/support/conn_case.ex

Lines changed: 19 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,28 @@
11
defmodule GoCardlessClient.TestCase do
22
@moduledoc """
3-
Base test case for GoCardlessClient integration tests using Bypass.
3+
Base test case for GoCardlessClient resource tests using Bypass.
44
5-
Sets up a local HTTP server (Bypass) that intercepts all GoCardlessClient API calls,
6-
allowing tests to run without real network requests.
5+
Sets up a local HTTP server (Bypass) and a Client that points at it,
6+
so all tests run without real network requests.
77
88
## Usage
99
1010
defmodule MyTest do
1111
use GoCardlessClient.TestCase
12+
alias Plug.Conn
1213
1314
test "creates a customer", %{client: client, bypass: bypass} do
14-
customer = GoCardlessClient.Factory.build(:customer)
15+
customer = build(:customer)
1516
1617
Bypass.expect_once(bypass, "POST", "/customers", fn conn ->
17-
conn
18-
|> Plug.Conn.put_resp_content_type("application/json")
19-
|> Plug.Conn.send_resp(200, Jason.encode!(%{"customers" => customer}))
18+
Conn.put_resp_content_type(conn, "application/json")
19+
|> Conn.send_resp(200, Jason.encode!(%{"customers" => customer}))
2020
end)
2121
22-
assert {:ok, ^customer} = GoCardlessClient.Resources.Customers.create(client, %{
23-
email: customer["email"]
24-
})
22+
assert {:ok, ^customer} = GoCardlessClient.Resources.Customers.create(
23+
client,
24+
%{email: customer["email"]}
25+
)
2526
end
2627
end
2728
"""
@@ -32,35 +33,26 @@ defmodule GoCardlessClient.TestCase do
3233
quote do
3334
import GoCardlessClient.Factory
3435
alias GoCardlessClient.{Client, Resources}
35-
36-
# Assert that {:ok, result} matches expected
37-
defmacro assert_ok({:ok, value}, do: block) do
38-
quote do
39-
var!(value) = unquote(value)
40-
unquote(block)
41-
end
42-
end
4336
end
4437
end
4538

4639
setup do
4740
bypass = Bypass.open()
41+
base_url = "http://localhost:#{bypass.port}"
4842

49-
client =
50-
GoCardlessClient.Client.new!(
51-
access_token: "test_token",
43+
# Build a client that points at the Bypass server, not the real GoCardless API.
44+
# The _base_url_override key in config is read by GoCardlessClient.HTTP.Client.
45+
config =
46+
GoCardlessClient.Config.new!(
47+
access_token: "test_token_sandbox",
5248
environment: :sandbox,
5349
max_retries: 0,
5450
finch_name: GoCardlessClient.Finch
5551
)
5652

57-
# Override the base URL to point at Bypass
58-
config = %{client.config | access_token: "test_token"}
59-
base_url = "http://localhost:#{bypass.port}"
6053
config = Map.put(config, :_base_url_override, base_url)
54+
client = %GoCardlessClient.Client{config: config}
6155

62-
patched_client = %{client | config: config}
63-
64-
{:ok, bypass: bypass, client: patched_client, base_bypass_url: base_url}
56+
{:ok, bypass: bypass, client: client, base_bypass_url: base_url}
6557
end
6658
end

0 commit comments

Comments
 (0)