开发者

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 s开发者_如何学Goure what actually gets sent to the database What actually appears before save and in what format. String? Integer?

Thanks in advance for responses.


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜