mercredi 4 novembre 2015

Ruby testing results of a loop

Quick testing question i have a FizzBuzz implementation,

def fizzbuzz(n)
  (1..n).each do |num|
    string = ''
    string << 'Fizz' if num % 3 == 0
    string << 'Buzz' if num % 5 == 0
    string << 'Boom' if num % 7 == 0
    string = num if string.empty?
    puts string
  end
end

I would like to write a test for it:

it 'should return the word Fizz for a number divisible by 3' do 
  expect(Thing.fizzbuzz(3)).to eq ("Fizz")
end

And i got

 Failure/Error: expect(@thing.fizzbuzz(3)).to eq ("Fizz")

   expected: "Fizz"
        got: 1..3

Now i know the actual result is more like

1
2 
'fizz'

But it got me thinking how i would write this so it would be easy to test? I could shovel each instance into an array and then test specific locations, but is there a better way?

Aucun commentaire:

Enregistrer un commentaire