What tests would you write for these two simple Ruby objects

lucianghinda

Lucian Ghinda

Posted on September 4, 2024

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?
💖 💪 🙅 🚩
lucianghinda
Lucian Ghinda

Posted on September 4, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related