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

jimmykiang
This test is broken right out of the box… — FAIL: TestAgent (7.82s) agent_test.go:77: Error Trace: agent_test.go:77 agent_test.go:...
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
jeffmcompsci
Title: Design and Build Great Web APIs - typo “https://company-atk.herokuapp.com/2258ie4t68jv” (page 19, third bullet in URL list) Typo:...
New
jesse050717
Title: Web Development with Clojure, Third Edition, pg 116 Hi - I just started chapter 5 and I am stuck on page 116 while trying to star...
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
brunogirin
When I run the coverage example to report on missing lines, I get: pytest --cov=cards --report=term-missing ch7 ERROR: usage: pytest [op...
New
Charles
In general, the book isn’t yet updated for Phoenix version 1.6. On page 18 of the book, the authors indicate that an auto generated of ro...
New
adamwoolhether
Is there any place where we can discuss the solutions to some of the exercises? I can figure most of them out, but am having trouble with...
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
Keton
When running the program in chapter 8, “Implementing Combat”, the printout Health before attack was never printed so I assumed something ...
New

Other popular topics Top

dasdom
No chair. I have a standing desk. This post was split into a dedicated thread from our thread about chairs :slight_smile:
New
AstonJ
Curious to know which languages and frameworks you’re all thinking about learning next :upside_down_face: Perhaps if there’s enough peop...
New
New
Rainer
My first contact with Erlang was about 2 years ago when I used RabbitMQ, which is written in Erlang, for my job. This made me curious and...
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
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
PragmaticBookshelf
A Hero’s Journey with Chris Pine @chrispine Chris Pine, author of Learn to Program, Third Edition, discusses his journey to beco...
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
New
First poster: bot
Large Language Models like ChatGPT say The Darnedest Things. The Errors They MakeWhy We Need to Document Them, and What We Have Decided ...
New

Latest in PragProg

View all threads ❯