rails,mock before_filter

时间:2012-03-16 11:26:46

标签: ruby-on-rails ruby-on-rails-3 stub before-filter

我有很多模型应该在创建后的几天内“完成”。 我做了一个before_filter,找到了应该完成的所有模型并完成它们。 问题是我无法弄清楚如何编写相同的规范。 模型验证如下:(伪验证):

validates :till, date: { later_than: 2.days.from_now.to_date, earlier_than: 8.days.from_now.to_date }

before_filter看起来像这样:

@models = Model.where(['till <= ? AND finished = ?', Time.now.to_date, false])
   @models.each do |model|
     model.update_attributes(finished: true)
   end
end

因此,为了让我的before_filter找到测试模型,我可以通过传递validate: false来保存假日期(即5.days.ago)

但是在过滤之前,update_attributes调用将返回false,因为模型的日期错误。

请问您如何让before_filter在单个测试用例的上下文中跳过验证?

1 个答案:

答案 0 :(得分:1)

您应该在验证中添加:on => :create,以便在update_attributes中调用before_filter不会触发它。

相关问题