julngomz

julngomz

Programming Phoenix LiveView B9: Testing Fails with the provided testing code. (pages 295 - 296)

Hello,

In chapter 10, Testing Your Live Views, the provided code is confusing. (Or I misread this passage)

Example Code

On page 295, when testing ratings by age group, the following code snippet is provided:

test "ratings are filtered by age group",  ...<map>.. do
  ...
  socket =
    update_socket(socket, :age_group_filter, "18 and under")
    |> SurveyResultsLive.assign_age_group_filter()

    assert socket.assigns.age_group_filter == "18 and under"
  ...
end

defp update_socket(socket, key, value) do
  %{socket | assigns: Map.merge(socket.assigns, Map.new([{key, value}]))}
end

Problem

So essentially what this piece of code was suppose to do is update the :age_group_filter to 18 and under, but what the the code provided actually does, is it updates the socket to 18 and under but then it gets piped back through SurveyResultsLive.assign_age_group_filter() reverting it back to all.

The following is the code to update the :age_group_filter in the SurveyResultsLive.assign_age_group_filter() live component function.

def assign_age_group_filter(socket) do
  assign(socket, :age_group_filter, "all")
end

def assign_age_group_filter(socket, age_group_filter) do
  assign(socket, :age_group_filter, age_group_filter)
end

Since we are not passing a second parameter, the first implementation is matched. Assigning all to :age_group_filter once again. Resulting in a failed assertion:

assert socket.assigns.age_group_filter == "18 and under"

Snippet Rewrite

So essentially the testing code should be as the following:

  ...
  socket = SurveyResultsLive.assign_age_group_filter(socket, "18 and under")
  assert socket.assigns.age_group_filter == "18 and under"
  ...

the update_socket helper function is essentially unnecessary and confusing, shouldn’t we leave the socket updates on to implementer not the tester?

Further Changes

If we follow the previous code, then on the next page (page 296), the resulting testing code should be:

 socket
      |> SurveyResultsLive.assign_gender_filter()
      |> SurveyResultsLive.assign_age_group_filter()
      |> assert_keys(:age_group_filter, "all")
      |> SurveyResultsLive.assign_age_group_filter("18 and under")
      |> assert_keys(:age_group_filter, "18 and under")
      |> SurveyResultsLive.assign_age_group_filter()
      |> SurveyResultsLive.assign_products_with_average_ratings()
      |> assert_keys(:products_with_average_ratings, [{"Test Game", 2.0}])

This works as expected and looks much nicer without the update_socket, but this also fails the last assertion test since we are creating two user ratings, the resulting average will change. The simple way to fix this is to change it to from 2.0 to 2.5.

Or you can add a helper function such as the following:

  defp calculate_average_rating(user_ratings) do
    stars = for {_user, stars} <- user_ratings, do: stars
    stars_count = length(stars)
    stars_sum = Enum.sum(stars)
    stars_sum / stars_count
  end
  
  ...using within test...

  user_ratings = [{user, 2}, {user2, 3}]
  avg_rating = calculate_average_rating(user_ratings)

  for {user, stars} <- user_ratings do
    create_rating(stars, user, product)
  end

  ...assertion test...

  |> assert_keys(:products_with_average_ratings, [{"Test Game", avg_rating}])

where user_ratings is a list of user rating tuples [{user, 2}, {user2, 3}] and can be for looped to create multiple ratings

Popular Prag Prog topics Top

Razor54672
The answer to 3rd Problem of Chapter 5 (Making Choices) of “Practical Programming, Third Edition” seems incorrect in the given answer ke...
New
jamis
The following is cross-posted from the original Ray Tracer Challenge forum, from a post by garfieldnate. I’m cross-posting it so that the...
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
rmurray10127
Title: Intuitive Python: docker run… denied error (page 2) Attempted to run the docker command in both CLI and Powershell PS C:\Users\r...
New
jeremyhuiskamp
Title: Web Development with Clojure, Third Edition, vB17.0 (p9) The create table guestbook syntax suggested doesn’t seem to be accepted ...
New
Chrichton
Dear Sophie. I tried to do the “Authorization” exercise and have two questions: When trying to plug in an email-service, I found the ...
New
brian-m-ops
#book-python-testing-with-pytest-second-edition Hi. Thanks for writing the book. I am just learning so this might just of been an issue ...
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
dtonhofer
@parrt In the context of Chapter 4.3, the grammar Java.g4, meant to parse Java 6 compilation units, no longer passes ANTLR (currently 4....
New
gorkaio
root_layout: {PentoWeb.LayoutView, :root}, This results in the following following error: no “root” html template defined for PentoWeb...
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
dasdom
No chair. I have a standing desk. This post was split into a dedicated thread from our thread about chairs :slight_smile:
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
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
Exadra37
I am a Linux user since 2012, more or less, and I always use Ubuntu on my computers, and my last 2 laptops have been used Thinkpads, wher...
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
PragmaticBookshelf
“A Mystical Experience” Hero’s Journey with Paolo Perrotta @nusco Ever wonder how authoring books compares to writing articles?...
New
New
PragmaticBookshelf
Author Spotlight: Peter Ullrich @PJUllrich Data is at the core of every business, but it is useless if nobody can access and analyze ...
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

Latest in PragProg

View all threads ❯