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 Pragmatic Bookshelf topics Top

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
HarryDeveloper
Hi @venkats, It has been mentioned in the description of ‘Supervisory Job’ title that 2 things as mentioned below result in the same eff...
New
AleksandrKudashkin
On the page xv there is an instruction to run bin/setup from the main folder. I downloaded the source code today (12/03/21) and can’t see...
New
adamwoolhether
When trying to generate the protobuf .go file, I receive this error: Unknown flag: --go_opt libprotoc 3.12.3 MacOS 11.3.1 Googling ...
New
leonW
I ran this command after installing the sample application: $ cards add do something --owner Brian And got a file not found error: Fil...
New
hazardco
On page 78 the following code appears: &lt;%= link_to ‘Destroy’, product, class: ‘hover:underline’, method: :delete, data: { confirm...
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
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
EdBorn
Title: Agile Web Development with Rails 7: (page 70) I am running windows 11 pro with rails 7.0.3 and ruby 3.1.2p20 (2022-04-12 revision...
New
New

Other popular topics Top

wolf4earth
@AstonJ prompted me to open this topic after I mentioned in the lockdown thread how I started to do a lot more for my fitness. https://f...
New
New
AstonJ
You might be thinking we should just ask who’s not using VSCode :joy: however there are some new additions in the space that might give V...
New
AstonJ
We have a thread about the keyboards we have, but what about nice keyboards we come across that we want? If you have seen any that look n...
New
AstonJ
Biggest jackpot ever apparently! :upside_down_face: I don’t (usually) gamble/play the lottery, but working on a program to predict the...
New
Maartz
Hi folks, I don’t know if I saw this here but, here’s a new programming language, called Roc Reminds me a bit of Elm and thus Haskell. ...
New
PragmaticBookshelf
Author Spotlight Jamis Buck @jamis This month, we have the pleasure of spotlighting author Jamis Buck, who has written Mazes for Prog...
New
PragmaticBookshelf
Author Spotlight: Sophie DeBenedetto @SophieDeBenedetto The days of the traditional request-response web application are long gone, b...
New
CommunityNews
A Brief Review of the Minisforum V3 AMD Tablet. Update: I have created an awesome-minisforum-v3 GitHub repository to list information fo...
New
AstonJ
This is cool! DEEPSEEK-V3 ON M4 MAC: BLAZING FAST INFERENCE ON APPLE SILICON We just witnessed something incredible: the largest open-s...
New