以卓越的方式测试条件验证

时间:2010-08-25 15:37:03

标签: ruby-on-rails

我有一个带有此验证的rails模型:

  validates_numericality_of(:insert_oz,
                            :allow_blank=>true,
                            :greater_than=>0,
                            :if=>(proc do |note|
                                    !note.insert_pages.nil? &&
                                       note.insert_pages > 0
                                  end))

使用非凡的,如何测试使用:if选项的验证?

如果我做了明显的事情,并将验证添加到我的规范中,将“validates”更改为“should_validate”:

  should_validate_numericality_of(:insert_oz,
                                  :allow_blank=>true,
                                  :greater_than=>0,
                                  :if=>(proc do |note|
                                          !note.insert_pages.nil? &&
                                            note.insert_pages > 0
                                        end))

然后规范因此消息失败:

'Processingnote should ensure numericality of insert oz is greater than 0 and allowing blank values' FAILED
Expected Processingnote to be invalid when insert oz is less than 1

1 个答案:

答案 0 :(得分:0)

尝试并捕捉陈述?