sf8193

sf8193

Unit testing phoenix with put does not change map

Hello!

I’m trying to do a unit test with ex_machina like so

    test "renders errors when data is invalid", %{conn: conn,  group: group, shipment: shipment, route: route} do
      conn = put conn, group_shipment_route_path(conn, :update, group, shipment, route), route: params_for(:invalid_route)
      assert html_response(conn, 200) =~ "Edit Route"
    end
  end

where the value I want to place in looks like this

  def invalid_route_factory do
    %Route{
        label: nil
    }

  end

my changeset and schema looking like this


  schema "routes" do
    field :label, :string, null: false
    field :address, :string
    field :date, :string
    field :groups, :string
    field :checklist, {:array, :string}
    belongs_to :shipment, Shipment # on_delete set in database via migration

    timestamps()
  end

  @doc false
  def changeset(route, attrs) do
    route
    |> cast(attrs, [:address, :date, :groups, :label, :shipment_id, :checklist])
    |> validate_required([:label, :shipment_id])
  end
end

and the function I am trying to test looking like this

  def update(conn, %{"route" => route_params} = params) do
    { group_id, shipment_id, route_id } = get_ids(params)
    route = Shipments.get_route!(route_id)
    case Shipments.update_route(route, route_params) do
      {:ok, route} ->
        conn
        |> put_flash(:info, "Route updated successfully.")
        |> redirect(to: group_shipment_route_path(conn, :show, group_id, shipment_id, route))
      {:error, %Ecto.Changeset{} = changeset} ->
        render(conn, "edit.html", group: group_id, shipment: shipment_id, route: route, changeset: changeset)
    end
  end

the error I get indicates that the case returns {:ok, route} rather than :error, which should not be the case since :label should be validated against null as you can see in my schema.

This is the error I get that indicates I get {:ok, route}, and in my conn struct I see the route wasn’t created from my ex_machina factory.

  1) test update route renders errors when data is invalid (FerryWeb.RouteControllerTest)
     test/ferry_web/controllers/route_controller_test.exs:69
     ** (RuntimeError) expected response with status 200, got: 302, with body:
     <html><body>You are being <a href="/groups/74/shipments/183/routes/120">redirected</a>.</body></html>
     code: assert html_response(conn, 200) =~ "Edit Route"
     stacktrace:
       (phoenix) lib/phoenix/test/conn_test.ex:362: Phoenix.ConnTest.response/2
       (phoenix) lib/phoenix/test/conn_test.ex:376: Phoenix.ConnTest.html_response/2
       test/ferry_web/controllers/route_controller_test.exs:75: (test)

Is there anyway I can debug why this is happening?

Marked As Solved

sf8193

sf8193

I figured it out, the issue was with the ex_machina library. If you put something as “nil”, it will not include it in the struct, which is why I couldn’t get my test to go down the “error” branch of my code

Also Liked

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

This is just an issue with your test case actually.

What this is saying is that the HTTP PUT request you made in your test case got a 302 response. The 302 response is the redirection response code. If you look at your update controller function, this is exactly what you tell it to do:

When you hit the update function from your browser, it gets the 302 status code and immediately redirects to the show page, which can make it less obvious that two independent HTTP requests happen. However in your test case there isn’t an automatic redirect follow that happens.

Where Next?

Popular in Questions Top

chokchit
** (DBConnection.ConnectionError) connection not available and request was dropped from queue after 2733ms. You can configure how long re...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
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
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
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

Other popular topics Top

aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
Nvim
Anybody knows a comprehensive comparison of Django and Phoenix, thanks for the help. Where are they similar? Where do they differ the m...
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 30877 112
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 53690 245
New
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New

We're in Beta

About us Mission Statement