我是Elixir / Phoenix的新手,所以我不确定这是否按预期工作或应该解决的问题。
我有一个Phoenix应用程序,刚刚开始向其中添加集成测试。完成了几天的设置和测试用户注册功能后,现在开始测试登录。
我正在使用Wallaby:
test.exs:
config :happy_app, :sql_sandbox, true
config :wallaby,
driver: Wallaby.Experimental.Chrome,
chrome: [headless: true],
screenshot_on_failure: true
test_helper.exs:
ExUnit.start()
Ecto.Adapters.SQL.Sandbox.mode(HappyApp.Repo, :manual)
{:ok, _} = Application.ensure_all_started(:wallaby)
Application.put_env(:wallaby, :base_url, HappyAppWeb.Endpoint.url())
我的测试看起来像这样:
defmodule HappyAppWeb.UserLoginTest do
use HappyAppWeb.IntegrationCase, async: true
alias HappyApp.Accounts
import HappyAppWeb.IntegrationSteps
@tag integration: true
test "user can login", %{session: session} do
email = "[email protected]"
password = "12345678"
# seed user into db
user = Accounts.create_user(%{email: email, password: password})
IO.inspect user
# logging in with the user's email and password
# asserting the view is correct
end
end
[当我检查用户时:IO.inspect user
我得到:
IO.inspect user
=>
{:ok,
%HappyApp.Accounts.User{
__meta__: #Ecto.Schema.Metadata<:loaded, "users">,
email: "[email protected]",
id: 449,
inserted_at: ~N[2019-11-24 13:20:33],
password: nil,
password_hash: "shhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh",
updated_at: ~N[2019-11-24 13:20:33]
}}
注意id: 449,
。那是对的吗?测试之间不应该将其重置为1吗?
手动查看postgres,我发现happy_app_test数据库确实为空。数据在其他地方吗?
看Phoenix Testing Document。 happy_app_test数据库为空的原因:
默认的测试帮助文件test / test_helper.exs为我们创建并迁移了我们的测试数据库。它还会为要运行的每个测试启动一个事务。这将通过在每个测试完成时回滚该事务来“清理”数据库。
以及为什么ID不是从1开始
随机化
以随机顺序运行测试是确保我们的测试真正隔离的好方法。如果我们发现给定测试会偶尔出现故障,则可能是因为先前的测试以以后未清除的方式更改了系统状态,从而影响了后续的测试。仅当测试以特定顺序运行时,这些失败才可能出现。