Explicit vs DRY tests

Hmm, this is pretty subjective, but I’d personally go with a variant on Version 2 that swaps Enum.each for a for comprehension. I find it just reads better since it’s so close to spoken language i.e. “for each of these numbers, assert this condition”

  test "1, 2, 4, 7, 8 returns number as a string" do
    for n <- [1, 2, 4, 7, 8], do: assert fizz_buzz(n) == "#{n}"
  end

Since it’s generally good practice to avoid “magic strings” – or in this case numbers – when writing tests, we could generate the multiples. It’s probably a bit much to test something like FizzBuzz, but since it’s an exercise… might as well!

  test "multiple of fifteen aka three and five returns FizzBuzz" do
    for n <- 1..10, do: assert fizz_buzz(15 * n) == "FizzBuzz"
  end

Or maybe even throw in an Enum.random instead.

  test "multiple of fifteen aka three and five returns FizzBuzz" do
    assert fizz_buzz(15 * Enum.random(1..100)) == "FizzBuzz"
  end
1 Like