0
請注意,這是my forum post的副本。鳳凰城測試數據持續存在,使測試不可重複
我使用Phoenix(1.2.1)並在我的測試環境中遇到了問題。問題是跨越測試插入(更新,刪除等)的數據正在測試數據庫中持久存在,這使得我的測試不可重複。
首次運行:
[email protected]:/mnt/sidecar_api# mix test
................................
Finished in 2.2 seconds
32 tests, 0 failures
Randomized with seed 180820
第二輪:
[email protected]:/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" => "[email protected]", "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" => "[email protected]", "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
唯一的相關信息在這些情況下是對象ID。我正在檢查插入的記錄是否具有ID 1,這是我在執行所有測試之前對數據庫狀態的假設。
這裏是我的test.exs
:
use Mix.Config
config :sidecar_api, SidecarApi.Endpoint,
http: [port: 4001],
server: false
config :logger, level: :warn
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
和test_helper.exs
ExUnit.start
Ecto.Adapters.SQL.Sandbox.mode(SidecarApi.Repo, :manual)
注意,每個測試後,從psql
檢查的Postgres的內容本身反映了空數據庫。用於開發和測試環境。現在,我很困惑在哪裏看。
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)
在此先感謝!
你確定這是不是因爲你的測試正在以不同的順序每次運行? –
你可以附加更重要的片段 - 你的測試等。如果你運行測試的副作用功能,你記得不要異步使用它們嗎? – PatNowak