Josiah Josiah - 2 months ago 11
Ruby Question

Rails Rspec - model attribute undefined method `>' for nil:NilClass

I keep getting the error undefined method `>' for nil:NilClass when I run my rspec on the Billing model. Below is my code for the model

class Billing < ActiveRecord::Base
validate :start_date, :end_date, presence: true
validate :is_valid_date?

def is_valid_date?
errors.add(:start_date, 'must be in the past') if start_date > Time.current.to_date
errors.add(:end_date, 'must be in the past') if end_date > Time.current.to_date
errors.add(:end_date, 'must be more than or equal to start date') if start_date > end_date
end
end


And this is my code for the rspec

require 'rails_helper'
RSpec.describe FamilyBilling, type: :model do
it { should validate_presence_of(:start_date) }
it { should validate_presence_of(:end_date) }
it { should validate_presence_of(:amount) }
it { should validate_presence_of(:hours) }
it { should validate_presence_of(:rate) }
it { should validate_presence_of(:processing_fee) }
it { should_not validate_presence_of(:tip) }
end


I get this error when I run rspec

Failed examples:
rspec ./spec/models/billing_spec.rb:8 # Billing should require start_date to be set
rspec ./spec/models/billing_spec.rb:9 # Billing should require end_date to be set
rspec ./spec/models/billing_spec.rb:10 # Billing should require amount to be set
rspec ./spec/models/billing_spec.rb:11 # Billing should require hours to be set
rspec ./spec/models/billing_spec.rb:12 # Billing should require rate to be set
rspec ./spec/models/billing_spec.rb:13 # Billing should require processing_fee to be set
rspec ./spec/models/billing_spec.rb:14 # Billing should not require tip to be set


and all of them show this error

Failure/Error: errors.add(:start_date, 'must be in the past') if start_date > Time.current.to_date

NoMethodError:
undefined method `>' for nil:NilClass


What did I do wrong?

Answer

Your custom validator expects that both start_date and end_date must exist. If they don't exist that error is thrown - for example here: start_date > Time.current.to_date.

Therefore you should validate their presence explicitly and check that they are present in your custom validator:

class Billing < ActiveRecord::Base
  validates :start_date, :end_date, presence: true
  validate :dates_valid?

  def dates_valid?
    errors.add(:start_date, 'must be in the past') if start_date && start_date > Time.current.to_date
    errors.add(:end_date, 'must be in the past') if end_date && end_date > Time.current.to_date
    errors.add(:end_date, 'must be more than or equal to start date') if start_date && end_date && start_date > end_date
  end
end