Testing with Minitest

If all that seems a little abstract, let’s look at an example of how you use the minitest library to write automated testing. We’ll start with a Roman numeral class. Our first pass at the code is pretty simple; it lets us create an object representing a certain number and display that object in Roman numerals:

 # This code has bugs
 class​ Roman
  MAX_ROMAN = 4999
 
 def​ ​initialize​(value)
 if​ value <= 0 || value > MAX_ROMAN
  fail ​"Roman values must be > 0 and <= ​​#{​MAX_ROMAN​}​​"
 end
  @value = value
 end
 
  FACTORS = [
  [​"m"​, 1000], [​"cm"​, 900], [​"d"​, 500], [​"cd"​, 400],
  [​"c"​, 100], [​"xc"​, 90], [​"l"​, 50], [​"xl"​, 40],
  [​"x"​, 10], [​"ix"​, 9], [​"v"​, 5], ...

Get Programming Ruby 3.3 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.