I’m using Phoenix (1.2.1) and having trouble in my test environment. The problem is that the data being inserted (updated, deleted, etc) across tests are being persisted in the test database making my testing non-repeatable.
First Run:
root@d7594ec7baf0:/mnt/sidecar_api# mix test
................................
Finished in 2.2 seconds
32 tests, 0 failures
Randomized with seed 180820
Second Run:
root@d7594ec7baf0:/mnt/sidecar_api# mix test
...........................
1) test user signup with POST /v1/users with valid attributes (SidecarApi.V1.UserControllerTest)
test/controllers/v1/user_controller_test.exs:15
Assertion with == failed
code: response == expected
lhs: %{"jsonapi" => %{"version" => "1.0"}, "data" => %{"attributes" => %{"first-name" => "Juan", "last-name" => "Dela Cruz", "primary-email" => "jdelacruz@example.com", "primary-mobile-number" => "639123456789", "username" => "jdelacruz"}, "type" => "user", "id" => "2"}}
rhs: %{"jsonapi" => %{"version" => "1.0"}, "data" => %{"attributes" => %{"first-name" => "Juan", "last-name" => "Dela Cruz", "primary-email" => "jdelacruz@example.com", "primary-mobile-number" => "639123456789", "username" => "jdelacruz"}, "type" => "user", "id" => "1"}}
stacktrace:
test/controllers/v1/user_controller_test.exs:36: (test)
....
Finished in 2.3 seconds
32 tests, 1 failure
Randomized with seed 556822
The only relevant info in these instances is the object ID. I’m checking if the inserted record has the ID 1 which is my assumption on the state of the DB before the execution of all my tests.
Here’s my test.exs
:
use Mix.Config
# We don't run a server during test. If one is required,
# you can enable the server option below.
config :sidecar_api, SidecarApi.Endpoint,
http: [port: 4001],
server: false
# Print only warnings and errors during test
config :logger, level: :warn
# Configure your database
config :sidecar_api, SidecarApi.Repo,
adapter: Ecto.Adapters.Postgres,
username: "sidecar",
password: "sidecardb",
database: "sidecar_test",
hostname: "sidecar-db-main",
pool: Ecto.Adapters.SQL.Sandbox
and test_helper.exs
ExUnit.start
Ecto.Adapters.SQL.Sandbox.mode(SidecarApi.Repo, :manual)
Note that after each tests, checking the content of postgres from psql
itself is reflecting empty databases. Both for development and testing environments. Right now, I’m confused where to look at.
sidecar_dev=# select * from users;
id | username | password | first_name | last_name | inserted_at | updated_at
----+----------+----------+------------+-----------+-------------+------------
(0 rows)
sidecar_test=# select * from users;
id | username | password | first_name | last_name | inserted_at | updated_at
----+----------+----------+------------+-----------+-------------+------------
(0 rows)
Thanks in advance!