Forum: Ruby on Rails RSPEC and Custom Validation date fields

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
8e0b66cad5effc404754052de3318ed2?d=identicon&s=25 Leo Godin (leogodin217)
on 2008-10-11 01:30
(Received via mailing list)
HI All,
I have a model that uses custom validation.  The validation works from
the browser but RSPEC gives it an error.  It looks to me like RSPEC
isn't seeing the start_time and end_time fields in my model.  If I
change line 9 in my validation to something like "unless title ==
title" I don't have any problem.  The issues seems to be with date
fields.  I suspect this is a bug but since I'm new to RSPEC I'd like
advice before reporting it.

Here is the error from rspec:


    You have a nil object when you didn't expect it!
    The error occurred while evaluating nil.<

    /home/leo/railsprojects/trunk/memorymap/app/models/event.rb:9:in
`valid_dates?'
    /home/leo/railsprojects/trunk/memorymap/spec/models/event_spec.rb:
24:

    7
    8  def valid_dates?
    9    unless start_time < end_time
    10      errors.add("End Time must be later than Start Time")
    11    end

Here is my model:

1 class Event < ActiveRecord::Base
  2
validates_presence_of :title, :description, :start_time, :end_time
  3   validates_uniqueness_of :title
  4   validate_on_create :valid_dates?
  5
  6   private
  7
  8   def valid_dates?
  9     unless start_time < end_time
 10       errors.add("End Time must be later than Start Time")
 11     end
 12   end
 13 end
This topic is locked and can not be replied to.