Ecto Sandbox Async Tests: Need for External HTTP Call Isolation in Elixir 1.16
0 reputation · 22 Dec 2023, 09:04 UTC
Goal
Run integration tests that touch the database and external APIs without using production credentials, relying on the Ecto sandbox for DB isolation.
Constraints and Uncertainty
The Ecto sandbox (introduced in Ecto 3.10) rolls back database transactions after each test, even in async: true mode. However, the sandbox does not address state changes made by external HTTP calls. When tests perform real HTTP requests, those calls may modify external services or consume rate limits, and there is no built‑in mechanism to roll them back.
Typical workarounds involve stubbing the HTTP client with Mox or a test adapter, but it is unclear whether the sandbox guarantees isolation when the stub is used concurrently in async tests.
Specific Questions
- Does the Ecto sandbox automatically roll back external HTTP calls in
async: truetests? - What configuration or pattern can enforce isolation of external HTTP calls while still using the sandbox for DB isolation?
- Is there a recommended combination of
--no-start, sandbox options, and HTTP stubbing that ensures full test isolation in Elixir 1.16?