sf8193
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?
Trending in Questions
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
Hello,
I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind.
However, when I launch mix phx.server, I get an error...
New
I really like the adapter patterns that ecto, nebulex, waffle, etc. use and would love find something similar for a key management servic...
New
I’m working on a small exercise involving update_in/3, and I came up with this solution:
data = %{
name: "Periodic Table",
category:...
New
Hello folks!
So at work, we are seeing some situations where we have to define some “fixed” strings that are used across the codebase in...
New
I’ve got trouble wrapping my head around the order in which functions are called in this snippet (from Phoenix’s authentication):
toke...
New
Is there any way to avoid the Hologram compiler running when using iex? It seems like the front-end code could potentially be disregarded...
New
Other Trending Topics
Edit: 2026 May 15 - This post is archived.
Mob is alive!!
Main docs: mob v0.7.11 — Documentation
A bit of explanation for the slightly c...
New
Hobbes is a low-level distributed database for the Elixir programming language.
Hobbes provides a simple, safe, and scalable storage lay...
New
A little off-topic, but I feel like people here have a good head on their shoulders.
I used to be quite good at making software. Was luc...
New
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #ai
- #podcasts-by-brainlid
- #ecto-query
- #elixirconf-us
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #elixirconf-eu
- #api
- #forms
- #security
- #metaprogramming











Showing Posts 1 to 4- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
benwilson512
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
updatecontroller function, this is exactly what you tell it to do:When you hit the
updatefunction from your browser, it gets the 302 status code and immediately redirects to theshowpage, 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.sf8193
@benwilson512 The test case is (trying to) test the error block of the code. That’s why it’s looking for a 200 status (i.e. the render call when it hits the error case).
Having said that, it is an issue with the test case because this works in the actual application. I’m just having trouble figuring out why the
putcall doesn’t change my route struct to be label:nilbenwilson512
What are your actual params?
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