我有一个用于Happymeal
的模块,该模块包含:food
(字符串)和:toy
(以:id
作为主键的简单数据库对象)
构造函数如下:
defmodule Myapp.Happymeal do
alias Myapp.{Repo, Toy}
defstruct [
:toy,
:food
]
@type t :: %__MODULE__{
toy: Toy,
food: String
}
def new(%{toy_id: toy_id, food: food}) do
toy = Repo.get(Toy, toy_id)
{:ok,
%__MODULE__{
toy: toy,
food: food
}}
end
end
在Myapp.Happymeal.new(%{toy_id: 1, food: "burger"})
中运行iex
时,我按预期返回了一个Happymeal
。
但是,当我通过测试运行它时
defmodule MyappTest do
use ExUnit.Case
describe "Happymeal tests" do
test "Happymeal can be created" do
{:ok, happymeal} = Myapp.Happymeal.new(%{toy_id: 1, food: "burger"})
IO.inspect(happymeal)
end
end
end
...我收到一个错误:
** (DBConnection.OwnershipError) cannot find ownership process for #PID<0.341.0>.
When using ownership, you must manage connections in one
of the four ways:
* By explicitly checking out a connection
* By explicitly allowing a spawned process
* By running the pool in shared mode
* By using :caller option with allowed process
The first two options require every new process to explicitly
check a connection out or be allowed by calling checkout or
allow respectively.
The third option requires a {:shared, pid} mode to be set.
If using shared mode in tests, make sure your tests are not
async.
The fourth option requires [caller: pid] to be used when
checking out a connection from the pool. The caller process
should already be allowed on a connection.
If you are reading this error, it means you have not done one
of the steps above or that the owner process has crashed.
See Ecto.Adapters.SQL.Sandbox docs for more information.
code: {:ok, happymeal} = Myapp.Happymeal.new(%{toy_id: 1, food: "burger"})
如何在phoenix测试中使这种构造函数起作用?
编辑:我的test_helper.exs
如下:
ExUnit.start()
Ecto.Adapters.SQL.Sandbox.mode(Myapp.Repo, :manual)