symbolboxer

symbolboxer

From Ruby to Elixir: Broken test for MessageController (182)

Hi! I believe I’ve found an erratum.

tl;dr: MessageController/create does not add changeset validation errors to the flash, which breaks one of its tests.

On page 182 we write this test:

test "invalid params is rejected", %{conn: conn} do
  conn = post(conn, ~p"/messages/new", %{}) assert html_response(conn, 200) =~
             Plug.HTML.html_escape("can't be blank")
end

Which resulted in this error when I ran it:

1) test POST /messages/new invalid params is rejected (PhoneAppWeb.MessageControllerTest)
     test/phone_app_web/controllers/message_controller_test.exs:34
     Assertion with =~ failed
     code:  assert html_response(conn, 200) =~ Plug.HTML.html_escape("can't be blank")
     left:  "<!DOCTYPE html>\n<html lang=\"en\" class=\"[scrollbar-gutter:stable]\">\n  <head>
     [rest of page truncated]
     right: "can&#39;t be blank"

After some valuable Elixir debugging practice, I believe I have the cause. The test in this case is looking for a “can’t be blank” error somewhere on the page. In MessageController/create, there is code that takes errors and puts them into the flash on line 48. However, that code is never reached because changeset validation happens on line 43 and the error shoots us down to line 55. That error handler doesn’t do anything specifically with the error. It passes the changeset to the view to render, but I don’t see where in the view it would display the error. Thus, the validation error doesn’t appear on the rendered view, and the test can’t find it.

Thanks, and just let me know if I’ve gotten something wrong.

Marked As Solved

sb8244

sb8244

Author of From Ruby to Elixir and Real-Time Phoenix

Thanks for sharing that. I took a look at CoreComponents.input and found the definition was slightly varied. I changed this line:

    # used to be:
    # errors =  if Phoenix.Component.used_input?(field), do: field.errors, else: []
    errors = field.errors

and the test passes. This was a change in the latest implementation of CoreComponents, hence the difference.

Now, to get this test passing without modifying the above line, I had to tweak the test to pass in blank inputs:

    test "invalid params is rejected", %{conn: conn} do
      params = %{message: %{to: "", body: ""}}
      conn = post(conn, ~p"/messages/new", params)

      assert html_response(conn, 200) =~
               Plug.HTML.html_escape("can't be blank")
    end

We can look through the implementation of used_input? to see why this works. By passing in the data, the form.params field contains the input field, so it counts as “used”

Popular Prag Prog topics Top

jimschubert
In Chapter 3, the source for index introduces Config on page 31, followed by more code including tests; Config isn’t introduced until pag...
New
johnp
Running the examples in chapter 5 c under pytest 5.4.1 causes an AttributeError: ‘module’ object has no attribute ‘config’. In particula...
New
mikecargal
Title: Hands-on Rust: question about get_component (page 295) (feel free to respond. “You dug you’re own hole… good luck”) I have somet...
New
joepstender
The generated iex result below should list products instead of product for the metadata. (page 67) iex&gt; product = %Product{} %Pento....
New
alanq
This isn’t directly about the book contents so maybe not the right forum…but in some of the code apps (e.g. turbo/06) it sends a TURBO_ST...
New
curtosis
Running mix deps.get in the sensor_hub directory fails with the following error: ** (Mix) No SSH public keys found in ~/.ssh. An ssh aut...
New
akraut
The markup used to display the uploaded image results in a Phoenix.LiveView.HTMLTokenizer.ParseError error. lib/pento_web/live/product_l...
New
jwandekoken
Book: Programming Phoenix LiveView, page 142 (157/378), file lib/pento_web/live/product_live/form_component.ex, in the function below: d...
New
tkhobbes
After some hassle, I was able to finally run bin/setup, now I have started the rails server but I get this error message right when I vis...
New
SlowburnAZ
Getting an error when installing the dependencies at the start of this chapter: could not compile dependency :exla, "mix compile" failed...
New

Other popular topics Top

AstonJ
I have seen the keycaps I want - they are due for a group-buy this week but won’t be delivered until October next year!!! :rofl: The Ser...
New
Exadra37
I am asking for any distro that only has the bare-bones to be able to get a shell in the server and then just install the packages as we ...
New
AstonJ
In case anyone else is wondering why Ruby 3 doesn’t show when you do asdf list-all ruby :man_facepalming: do this first: asdf plugin-upd...
New
Exadra37
Oh just spent so much time on this to discover now that RancherOS is in end of life but Rancher is refusing to mark the Github repo as su...
New
PragmaticBookshelf
“A Mystical Experience” Hero’s Journey with Paolo Perrotta @nusco Ever wonder how authoring books compares to writing articles?...
New
wmnnd
Here’s the story how one of the world’s first production deployments of LiveView came to be - and how trying to improve it almost caused ...
New
husaindevelop
Inside our android webview app, we are trying to paste the copied content from another app eg (notes) using navigator.clipboard.readtext ...
New
PragmaticBookshelf
Author Spotlight: Karl Stolley @karlstolley Logic! Rhetoric! Prag! Wow, what a combination. In this spotlight, we sit down with Karl ...
New
PragmaticBookshelf
Author Spotlight: Bruce Tate @redrapids Programming languages always emerge out of need, and if that’s not always true, they’re defin...
New
New

Latest in PragProg

View all threads ❯