# RSPEC and Custom Validation date fields

**URL:** <https://www.ruby-forum.com/t/rspec-and-custom-validation-date-fields/150664>\
**Category:** Rails\
**Created:** [October 11, 2008, 1:30am UTC](https://www.ruby-forum.com/t/rspec-and-custom-validation-date-fields/150664 "2008-10-11T01:30:06Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![leogodin217](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/67e7ee/32.png) [@leogodin217](https://www.ruby-forum.com/u/leogodin217)\
**Post date:** [October 11, 2008, 1:30am UTC](https://www.ruby-forum.com/t/rspec-and-custom-validation-date-fields/150664/1 "2008-10-11T01:30:06Z")

</div>

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
