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

Where Next?

Popular Pragmatic Bookshelf topics Top

abtin
page 20: … protoc command… I had to additionally run the following go get commands in order to be able to compile protobuf code using go...
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
jeffmcompsci
Title: Design and Build Great Web APIs - typo “https://company-atk.herokuapp.com/2258ie4t68jv” (page 19, third bullet in URL list) Typo:...
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
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
AndyDavis3416
@noelrappin Running the webpack dev server, I receive the following warning: ERROR in tsconfig.json TS18003: No inputs were found in c...
New
digitalbias
Title: Build a Weather Station with Elixir and Nerves: Problem connecting to Postgres with Grafana on (page 64) If you follow the defau...
New
New
kolossal
Hi, I need some help, I’m new to rust and was learning through your book. but I got stuck at the last stage of distribution. Whenever I t...
New
bjnord
Hello @herbert ! Trying to get the very first “Hello, Bracket Terminal!" example to run (p. 53). I develop on an Amazon EC2 instance runn...
New

Other popular topics Top

axelson
I’ve been really enjoying obsidian.md: It is very snappy (even though it is based on Electron). I love that it is all local by defaul...
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
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
Thanks to @foxtrottwist’s and @Tomas’s posts in this thread: Poll: Which code editor do you use? I bought Onivim! :nerd_face: https://on...
New
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
AstonJ
This looks like a stunning keycap set :orange_heart: A LEGENDARY KEYBOARD LIVES ON When you bought an Apple Macintosh computer in the e...
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
AstonJ
Continuing the discussion from Thinking about learning Crystal, let’s discuss - I was wondering which languages don’t GC - maybe we can c...
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

Sub Categories: