wolfiton
Phoenix inside out exercise stuck on test
Hi everyone,
I have a problem of understanding how this test should like on chapter 8 My order exercise here https://shankardevy.com/phoenix-inside-out-mpf/#chapter-8
Question
I created my test like so
test "get_order/1" do
assert %Order{status: "Confirmed"} = Sales.get_order()
end
But I don’t understand how to get the order for the current user without using Repo or conn here?
Expected result:
To get the order history of the current user in this test.
Thanks in advance for any hints or directions.
Update I started to write something like this
test "create_order" do
@valid_attrs %{
"order_id" => 1
"status" => "Confirmed"
}
end
Didn’t work because I don’t have a create order func in my schema can someone show me what i am doing wrong?
Thanks, because i really want to understand how to build this test correctly.
Most Liked
al2o3cr
It’s hard to know for sure without more code, but I’ll take some guesses.
Sales.get_order is a context function - it likely uses your Repo under the hood.
That function can’t take zero args - which order is it getting? For what user? You’ll likely want to pass in the user.
The setup functions in your test file should be setting up things like the current user and orders then passing them into the tests like:
test "get_order/1", %{current_user: current_user} do
assert %Order{status: "Confirmed"} = Sales.get_order(current_user)
end
I’m also not sure if get_order is the right name; chapter 8 talks about viewing the user’s order history, so the logical thing to return is a list of orders - something like Sales.get_orders(user)
I don’t understand what the intent of this code is, or how exactly it would fail. You’ll get more effective help if you show complete code examples (instead of fragments) and specific error messages (instead of “didn’t work”).
al2o3cr
Looking at the commit history for that repo, I don’t see any code for this particular exercise - the “My Orders” link doesn’t appear, for instance. There also aren’t any tests for controller actions that need an authenticated user, or for context functions that expect a customer…
My reading of the style suggested in that tutorial suggests you’ll want to add a bunch of things:
-
code in the
CRMcontext to find all orders given a customer, and a particular order given an ID and a customer -
tests for the new
CRMfunctions. If there were tests forCRM.get_customer_ticket!, I’d recommend that you follow those - but the generated tests were deleted without replacement. -
a controller that handles showing an index of the user’s orders and individual ones, based on
conn.assigns.current_customer. It will look a lot likeTicketController. -
an acceptance test that interacts with those controller actions. Again, there aren’t any examples in that repo of this kind of test. You’ll need to dig into the Hound docs to figure out how to set up the session correctly with
customer_id, or simulate going through the login flow by hand.
Popular in Chat/Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance








