wolfiton

wolfiton

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.

Showing Posts 1 to 5

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”).

wolfiton

wolfiton OP

Thanks @al2o3cr for the reply. even though it is hard to understand what i wrote, the author uses the following test pattern.

defmodule Mango.SalesTest do
  use Mango.DataCase

  alias Mango.Sales
  alias Mango.Sales.Order
  alias Mango.Catalog.Product

  test "create_cart" do
    assert %Order{status: "In Cart"} = Sales.create_cart
  end

  test "get_cart/1" do
    cart1 = Sales.create_cart
    cart2 = Sales.get_cart(cart1.id)
    assert cart1.id == cart2.id
  end

  test "add_to_cart/2" do
    product = %Product{
      name: "Tomato",
      pack_size: "1 kg",
      price: 55,
      sku: "A123",
      is_seasonal: false, category: "vegetables" } |> Repo.insert!
    cart = Sales.create_cart
    {:ok, cart} = Sales.add_to_cart(cart, %{"product_id" => product.id, "quantity" => "2"})

    assert [line_item] = cart.line_items
    assert line_item.product_id == product.id
    assert line_item.product_name == "Tomato"
    assert line_item.pack_size == "1 kg"
    assert line_item.quantity == 2
    assert line_item.unit_price == Decimal.new(product.price)
    assert line_item.total == Decimal.mult(Decimal.new(product.price), Decimal.new(2))
  end
end

Also my intent wasn’t to provide just pieces of code. I just found the authors github with the full code and trying to grasp how this test satisfies his requirements for the exercise in the book:

.Exercise requirements:

My Orders
User Story #12

As a customer, I want to see my order history.

As explained earlier, this user story is an exercise for you to workout by yourself by applying the concepts learned so far. Specifications for this user story are given below:

Specifications

There should be a link "My Orders" on the nav bar under the user menu.

Clicking on this "My Orders" link should take users to the /orders page which 
lists all orders for the  currently logged in customer in an HTML table.

The table should list the following details of each order:

    Order ID

    Status

    Products Ordered

    Total

Each order should have a "View" link that takes users to /orders/:id 
to view the complete details of the order.

If customers try to view an order that doesn’t belong to them, then a 
404 page should be returned.
wolfiton

wolfiton OP

I have access form a plug to the current_customer

This exercise is created using TDD so I was following the book’s way of building this. First write the test then the acceptance test and then write the functionality to make it pass.

Full github code here GitHub - shankardevy/mango at chapter8 · GitHub

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.

wolfiton

wolfiton OP

Thanks i will read programming phoenix 1.4 hopefully, after that i can come back and complete the rest of the book and add tests as they should be.

— All posts loaded —

Where Next? Top

Trending in Chat/Questions Top

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews