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

Travis
Dockerfile: FROM ruby:2.6 RUN apt-get update -yqq RUN apt-get install -yqq --no-install-recommends nodejs COPY . /usr/src/app/ WORKDI...
New
telemachus
Python Testing With Pytest - Chapter 2, warnings for “unregistered custom marks” While running the smoke tests in Chapter 2, I get these...
New
sdmoralesma
Title: Web Development with Clojure, Third Edition - migrations/create not working: p159 When I execute the command: user=&gt; (create-...
New
edruder
I thought that there might be interest in using the book with Rails 6.1 and Ruby 2.7.2. I’ll note what I needed to do differently here. ...
New
raul
Hi Travis! Thank you for the cool book! :slight_smile: I made a list of issues and thought I could post them chapter by chapter. I’m rev...
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
creminology
Skimming ahead, much of the following is explained in Chapter 3, but new readers (like me!) will hit a roadblock in Chapter 2 with their ...
New
Henrai
Hi, I’m working on the Chapter 8 of the book. After I add add the point_offset, I’m still able to see acne: In the image above, I re...
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
New

Other popular topics Top

AstonJ
If it’s a mechanical keyboard, which switches do you have? Would you recommend it? Why? What will your next keyboard be? Pics always w...
New
Exadra37
Please tell us what is your preferred monitor setup for programming(not gaming) and why you have chosen it. Does your monitor have eye p...
New
brentjanderson
Bought the Moonlander mechanical keyboard. Cherry Brown MX switches. Arms and wrists have been hurting enough that it’s time I did someth...
New
AstonJ
I’ve been hearing quite a lot of comments relating to the sound of a keyboard, with one of the most desirable of these called ‘thock’, he...
New
foxtrottwist
Here’s our thread for the Keyboardio Atreus. It is a mechanical keyboard based on and a slight update of the original Atreus (Keyboardio ...
New
Rainer
Not sure if following fits exactly this thread, or if we should have a hobby thread… For many years I’m designing and building model air...
New
dimitarvp
Small essay with thoughts on macOS vs. Linux: I know @Exadra37 is just waiting around the corner to scream at me “I TOLD YOU SO!!!” but I...
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
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
OvermindDL1
Woooooooo! This is such a huge release for it, and 2 years incoming! In short, the library is now using an updated hyper backend (not j...
New

Latest in PragProg

View all threads ❯