I'm trying to run the following test in Phoenix:
alias BarronWatchCompany.MWS alias 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
But, I'm getting this error:
* 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)
Where create_user/1 and create_order are defined here:
# 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
Any idea why create_order/2
doesn't match?
Your error is telling you that the second argument that is being passed to your create_order/2
function is {:ok, %User{...}}
. Your function is just expecting the user, not a tuple. If you extract your user out and pass it in properly, your call should work.