Ryan Tan

Ryan Tan

© 2019

Unit Testing Pitfalls

Simple function to get the nth colour of the rainbow.

def colour_at(index)
  colours = ['red', 'orange', 'yellow', 'green', 'blue', 'indigo', 'violet']

  colours[index]
end

A unit test for this can look like this

describe '#colour_at' do
  it 'should return third colour of rainbow' do
    expect(colour_at(3)).to eq '' # hmm, what value ah?
  end
end

When writing the expected result, you might assume code is right/lazy to count rainbow/not know the colours of the rainbow…(don’t judge)
so you go ahead run the test and u get..

Failure/Error: expect(colour_at(3)).to eq ''
    expected ''
    got 'green'

#copypasta

describe '#colour_at' do
  it 'should return third colour of rainbow' do
    expect(colour_at(3)).to eq 'green'
  end
end
1 example(s), 0 failures.

think about it. if not guilty, move on!