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?