LondonGuy
LondonGuy

Reputation: 11098

How do you test dates in ruby on rails 3 with rspec?

Say for instance I have a datefield or 3 select fields for day month and yea..

How does the final selection of date get sent to the database? or in what format doesn't it get sent?

20110803 ?

2011-08-03 ?

What I want to do is:

  1. Test for a valid date (a date that has been selected that exists)
  2. Test for an invalid date (invalid would be a date that doesn't exist and also be when nothing has been selected)

I know an idea of how to write these tests but not sure what actually gets sent to the database What actually appears before save and in what format. String? Integer?

Thanks in advance for responses.

Upvotes: 2

Views: 3470

Answers (1)

Maurício Linhares
Maurício Linhares

Reputation: 40333

The gets sent to the database as the database expects it. Each database and configuration will yield a different format for a date but this will not make any difference to you as the field is configured to be Date and it will also be a Date on Rails, so you should not care about the format.

While testing, there are many ways you can validate, but you will not be using neither integer or string, you're using Date objects. Here's an example of how you could write the first one:

it 'should find a model today' do 
  @date = Date.today
  @model = Model.create!(:date => @date)
  Model.first( :conditions => {:date => @date}).should == @model 
end

Upvotes: 4

Related Questions