A few issues on code to test average/1
:
-
add_element/2
inEnum.reduce/3
keeps adding1
(one) to the list, but numbers ininput_list
aren’t just ones. -
average/1
inassert
should be givenmeasurements
, notinput_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