创建!没有在rails3中设置datetime属性

时间:2011-03-21 20:34:31

标签: ruby-on-rails-3 rspec

我的一个模型中有一个数据时间属性taken_at,它将记录测试的时间。我正在尝试使用以下行验证此字段是否始终存在于我的模型中:

validates :taken_at, :presence => true

在我的rspec测试中,我正在通过以下测试验证此规则:

before (:each) do
  @user = Factory(:user)
  @attr = { :taken_at => DateTime.now }
end

it "should create a new profile given valid attributes" do
  # set taken_at inside of a block because the validations fail otherwise.    
  @user.hartman_value_profiles.create! do |hvp|
    hvp.taken_at = DateTime.now
  end
end

如您所见,我在块中设置taken_at而不是将其作为参数传递。这是有效的,但我不明白为什么以下测试会失败:

it "should create a new profile given valid attributes" do    
  @user.hartman_value_profiles.create!(@attr)
end

有没有人对这里发生的事情有任何见解?任何有关文档的提示,技巧或指示都将不胜感激。

1 个答案:

答案 0 :(得分:1)

您的第二个示例使用质量分配,而第一个示例使用直接分配。您是否通过任何更改禁止批量分配给taken_at?