簡體   English   中英

在測試環境中創建的新用戶的 ID 使用 Wallaby/Phoenix 不斷增加

[英]New users being created in test environment have their IDs continually incrementing using Wallaby/Phoenix

我是 Elixir/Phoenix 的新手,所以我不確定這是否按預期工作或我應該解決的問題。

我有一個 Phoenix 應用程序,我剛剛開始向其中添加集成測試。 經過幾天的設置和測試用戶注冊功能,我現在開始測試登錄。

我正在使用小袋鼠:

測試.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 = "test-user@example.com"
    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: "test-user@example.com",
   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 db 確實是空的。 數據在別處嗎?

看看鳳凰測試文檔 happy_app_test db 為空的原因:

默認的測試幫助文件 test/test_helper.exs 為我們創建和遷移我們的測試數據庫。 它還為每個要運行的測試啟動一個事務。這將通過在每個測試完成時回滾事務來“清理”數據庫。

以及為什么 id 不從 1 開始是數據庫保留 AUTO_INCREMENT id 除非您重置數據庫或指定 postgres database

更新:查看 mix.exs 文件

  defp aliases do
    [
      "ecto.setup": ["ecto.create", "ecto.migrate", "run priv/repo/seeds.exs"],
      "ecto.reset": ["ecto.drop", "ecto.setup"],
      test: ["ecto.create --quiet", "ecto.migrate", "test"]
    ]
  end

當您運行mix test 時,它實際上會調用 3 個命令: "ecto.create --quiet", "ecto.migrate", "test" 這就是為什么每次測試后數據庫都是干凈的。

這個東西跟數據庫引擎有關。 我找不到可以肯定的信息,但是我懷疑回滾正在刪除以前插入的行。

刪除行時,id 計數器不會重置,它會繼續遞增。 基本上你需要刪除/更新表來重置計數器,或者如果你真的需要,你可以手動重置它。

如果您重新啟動測試,您會注意到插入記錄的第一個測試將從 1 開始,其他測試將繼續計數器,因為在測試之前您運行遷移並創建一個帶有新串行計數器的新表。

如果您有興趣了解有關 id 自動遞增的更多信息,您可以在這里閱讀。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM