如何在rspec中添加日期时间属性?

时间:2014-04-05 23:35:01

标签: ruby-on-rails ruby rspec

我有一个测试套件,由于未知的属性错误而继续失败。未知属性是datetime。下面是我的架构文件:

create_table "posts", force: true do |t|
  t.string   "title",             null: false
  t.text     "body",              null: false
  t.datetime "date_created",      null: false
  t.integer  "author_id",         null: false
  t.datetime "created_at"
  t.datetime "updated_at"
  t.integer  "interest_group_id", null: false
  t.integer  "comments_count"
end

以下是我的rspec文件的摘录:

let(:post){Post.new(body: 'hello', title: 'Hello, 
by the Beatles', author: author, interest_group: interest_group, 
date_created: ?????)}

我的问题很简单,我在date_created字段中放置了什么?它寻找的是一个特定于日期时间的值,但它看起来像什么?我试过把'2012-3-13'作为一个例子,但由于值是一个字符串而失败了?有什么想法吗?

1 个答案:

答案 0 :(得分:3)

这个怎么样?

let(:post){Post.new(body: 'hello', title: 'Hello, 
by the Beatles', author: author, interest_group: interest_group, 
date_created: DateTime.parse('2012-3-13'))}