wolfiton

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

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

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 CRM context to find all orders given a customer, and a particular order given an ID and a customer

  • tests for the new CRM functions. If there were tests for CRM.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 like TicketController.

  • 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.

Where Next?

Popular in Chat/Questions Top

lgmfred
Hello, I want to get started with elixir today. Having learnt the basics of Erlang (my first ever language) and seen what it’s can do, I’...
New
Fl4m3Ph03n1x
Background I have been reading quite a lot about design and architecture in Elixir and overall FP. My latest incursions took to me the On...
New
shansiddiqui94
Hello, I have an interview coming up and I seem to have forgotten important concepts of Elixir. So I was wondering if you guys know of a...
New
maz
I’m getting this error: ## ** (Ecto.Query.CompileError) Tuples can only be used in comparisons with literal tuples of the same s...
New
AstonJ
It’s been a while since we asked this - I’m sure others (especially newcomers to the language) will be interested to hear how you’ve all ...
New
meraj_enigma
Hey, What’s a good resource to learn Microservices in Elixir/Phoenix? Is there any book/docs/Github repos that I can refer to? Thanks, -M
New
Fl4m3Ph03n1x
Background After following the communitiy suggestion, I bought the Elixir in Action 2nd Edition book and I am about to finish it now. I ...
New
Chawki
Hi,i’m new to elixir. i’m searching elixir small programs to try it out my self,Is any good resources out there? Thank you.
New
pdgonzalez872
Hi! In my quest of becoming the best Elixir dev I can be, I saw one aspect in my career that I’d like to improve upon. This is language...
New
Twfo326
As a novice dev I’m trying to keep the curriculum as lean as possible. My requirements are modest: build simple CRUD apps with Phoenix...
New

Other popular topics Top

senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
marick
I had some trouble figuring out how to make many-to-many associations work. Once I got it working, I wrote a blog post. Because I’m a nov...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

We're in Beta

About us Mission Statement