我正在尝试在Phoenix中运行以下测试:
别名BarronWatchCompany.MWS别名BarronWatchCompany.Accounts
@valid_attrs %{amazon_order_id: "some amazon_order_id", buyer_email: "some buyer_email", buyer_name: "some buyer_name", earliest_delivery_date: "some earliest_delivery_date", last_update_date: "some last_update_date", latest_delivery_date: "some latest_delivery_date", order_status: "some order_status", order_total: "some order_total", purchase_date: "some purchase_date"}
@user_attrs %{encrypted_password: "some encrypted_password", username: "some username", mws_token: "some mwstoken"}
test "list_orders/0 returns all orders" do
user = Accounts.create_user(@user_attrs)
order = MWS.create_order(@valid_attrs, user)
assert MWS.list_orders() == []
end
但是,我收到此错误:
* test orders list_orders/0 returns all orders (369.7ms)
1) test orders list_orders/0 returns all orders (BarronWatchCompany.MWSTest)
test/barron_watch_company/mws/mws_test.exs:65
** (FunctionClauseError) no function clause matching in BarronWatchCompany.MWS.create_order/2
The following arguments were given to BarronWatchCompany.MWS.create_order/2:
# 1
%{amazon_order_id: "some amazon_order_id", buyer_email: "some buyer_email", buyer_name: "some buyer_name", earliest_delivery_date: "some earliest_delivery_date", last_update_date: "some last_update_date", latest_delivery_date: "some latest_delivery_date", order_status: "some order_status", order_total: "some order_total", purchase_date: "some purchase_date"}
# 2
{:ok, %BarronWatchCompany.Accounts.User{__meta__: #Ecto.Schema.Metadata<:loaded, "users">, encrypted_password: "$2b$12$rJCbdwGS7uIAcHuMrnk/K.jij1bUfWNrx65sTtO4aqXYAMyGVLmIG", id: 44, inserted_at: ~N[2019-10-14 18:20:58], mws_token: "some mwstoken", orders: #Ecto.Association.NotLoaded<association :orders is not loaded>, updated_at: ~N[2019-10-14 18:20:58], username: "some username"}}
Attempted function clauses (showing 1 out of 1):
def create_order(attrs, %BarronWatchCompany.Accounts.User{} = user)
code: order = MWS.create_order(@valid_attrs, user)
stacktrace:
(barron_watch_company) lib/barron_watch_company/mws/mws.ex:54: BarronWatchCompany.MWS.create_order/2
test/barron_watch_company/mws/mws_test.exs:67: (test)
在此处定义create_user / 1和create_order的位置:
# BarronWatchCompany.MWS
def create_order(attrs \\ %{}, %User{} = user) do
%Order{}
|> Order.changeset(attrs)
|> Ecto.Changeset.put_change(:user_id, user.id)
|> Repo.insert()
end
# BarronWatchCompany.Accounts
def create_user(attrs \\ %{}) do
%User{}
|> User.changeset(attrs)
|> Repo.insert()
end
知道为什么create_order/2
不匹配吗?
您的错误是告诉您正在传递给create_order/2
函数的第二个参数是{:ok, %User{...}}
。您的功能只是期望顺序,而不是元组。如果您将用户提取出来并正确传递,则您的呼叫应该可以进行。