DEV Community

loading...

Which test will fall if we delete this piece of code?

vasily profile image Vasily Polovnyov ・1 min read

Look, there is an alarm like this:

class Alarm
  def to_human
    at.strftime("%k:%M").strip
  end
end
Enter fullscreen mode Exit fullscreen mode

And this test:

it "returns alarm in human-readable 24-hour format" do
  time = Time.local(2019, 7, 7, 10, 35)
  alarm = described_class.new(at: time)

  expect(alarm.to_human).to eq "10:35"
end
Enter fullscreen mode Exit fullscreen mode

To see if these tests are enough, I look at the module being tested and ask myself: what would happen, if I remove this piece of code, which expectation will fail? If none, then I'm 100% missing checks or expectations.

In the alarm clock example, the .strip is in question. If you delete it, the test stays green. So the expectation is missing:

it "strips any leading spaces" do
  time = Time.local(2019, 7, 7, 7, 15)
  alarm = described_class.new(at: time)

  expect(alarm.to_human).to eq "7:15"
end
Enter fullscreen mode Exit fullscreen mode

Discussion (0)

pic
Editor guide