Image of Lucian Ghinda writing for notes.ghinda.com
September 4th, 2024

What tests would you write for these two simple Ruby objects

Imagine you wrote this simple PORO in your Ruby on Rails app:

class CreditCard::AgeValidator
  def initialize(age)
    @age = age
  end
  
  def valid?
    return true if @age >= 18 
    
    false
  end
end

What tests would you write for it? 

What cases would you cover with your tests? How many tests would you write?

What about this update_required? method. 

What cases would you cover when testing Account#update_required? 

What cases would you cover with your tests? How many tests would you write?

class Account < ApplicationRecord
  \# t.date "expires_at"
  \# t.string "website"
  
  def update_required?
    website.blank? || expired?
  end
  
  def eternal? = expires_at.nil?
  
  def expired?
    return false if eternal?

    expires_at < Date.current
  end
end