DEV Community

Cover image for What tests would you write for these two simple Ruby objects
Lucian Ghinda
Lucian Ghinda

Posted on • Originally published at notes.ghinda.com

What tests would you write for these two simple Ruby objects

A simple PORO

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
Enter fullscreen mode Exit fullscreen mode

Questions:

  1. What tests would you write for it?
  2. What cases would you cover with your tests? How many tests would you write?

What about this update_required? method

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
Enter fullscreen mode Exit fullscreen mode

Questions:

  1. What cases would you cover when testing Account#update_required?
  2. What cases would you cover with your tests? How many tests would you write?

Top comments (0)