Testing Elixir: test code typo (page 72, beta 4)

A few issues on code to test average/1:

  1. add_element/2 in Enum.reduce/3 keeps adding 1 (one) to the list, but numbers in input_list aren’t just ones.
  2. average/1 in assert should be given measurements, not input_list.

The corrected code should then be as follows:

describe "average/1" do
  test "it returns the average of the list" do
    max_measurements = Enum.random(1..100)
    measurements = RollingAverageMeasurements.new(max_measurements)
   
    input_list = Enum.take_random(1..1_000, max_measurements)
    measurements = Enum.reduce(input_list, measurements, fn i, acc ->
      RollingAverageMeasurements.add_element(acc, i)
    end)
   
    expected_average = Enum.sum(input_list) / length(input_list)
    assert RollingAverageMeasurements.average(measurements) == expected_average
  end
end

Good catch, fixed! Thank you :slight_smile: