使用 validates_presence_of 和 validates_numericality_of 时验证失败
我需要验证模型中的数据,然后使用 rspec 测试该模型:
这是模型中的验证:
validates_presence_of :sales_price
validates_presence_of :retail_price
validates_numericality_of :sales_price, :greater_than => 0
validates_numericality_of :retail_price,:greater_than => 0
validates_numericality_of :sales_price, :less_than => :retail_price,
:message => "must be less than retail price."
当我尝试像这样测试模型时,
it { should validate_presence_of :sales_price }
it { should validate_presence_of :retail_price }
it { should validate_numericality_of :sales_price }
it { should validate_numericality_of :retail_price }
我收到此错误
Failure/Error: it { should validate_presence_of :retail_price }
ArgumentError:
comparison of Float with nil failed
# ./spec/models/offer_option_spec.rb:19:in `block (2 levels) in <top (required)>'
我该如何解决这个问题?
I need to validate data in my model and then test this model using rspec:
Here is the validation in model:
validates_presence_of :sales_price
validates_presence_of :retail_price
validates_numericality_of :sales_price, :greater_than => 0
validates_numericality_of :retail_price,:greater_than => 0
validates_numericality_of :sales_price, :less_than => :retail_price,
:message => "must be less than retail price."
when i'm trying to test tho model like this
it { should validate_presence_of :sales_price }
it { should validate_presence_of :retail_price }
it { should validate_numericality_of :sales_price }
it { should validate_numericality_of :retail_price }
I get this error
Failure/Error: it { should validate_presence_of :retail_price }
ArgumentError:
comparison of Float with nil failed
# ./spec/models/offer_option_spec.rb:19:in `block (2 levels) in <top (required)>'
How can i solve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
谢谢您的回答。
我终于明白了。
现在我遇到了另一个问题。我正在使用rails.validation.js,它可以帮助我执行客户端验证。如果您在数值验证器中使用类似的内容,一切都可以:
validation.js 创建一个函数来检查字段中的值是否大于 0。该函数的创建方式如下:
element 是我的输入字段,
CHECKS 包含不同的哈希值:less_than : <、greater_than : > 等
options[check] 包含一些传递到 validator 的值(:greater_than => 0,在本例中为零,options[check] is less_than : '0')。但是,当我使用其他内容而不是值时,我收到错误:
我尝试了类似的操作
,但执行验证时该对象不存在,因此 self 只是指向该类,而 Retail_price 不存在。
你会建议什么来解决这个问题?
Thank you for your answer.
I finally got this.
Now i got another problem. I'm using rails.validation.js which helps me to perform client side validations. Everything is ok if you use something like this in your numerical validator:
validation.js creates a function that checks if the value in the field is greater than 0. The function is created like that:
element is my input field,
CHECKS contains different hash: less_than : <, greater_than : >, etc
options[check] contains some value that is passed into validator(:greater_than => 0, zero in this case, options[check] is less_than : '0'). But when i use something else instead of a value i get an error:
I tried something like this
but the object doesn't exist when the validation is performed, so self just points to the class and retail_price doesn't exist.
what would you recommend to solve the problem?
@sled 是正确的,nil 检查内置于 validates_numericality_of 中并默认启用,因此没有必要同时满足这两个要求。
另一件需要注意的事情是,销售价格实际上验证了两次数值,这可能会导致问题。我会把它改为
@sled is correct that the nil check is built into validates_numericality_of and is enabled by default, so it's unnecessary to have both requirements.
One other thing to note is that sales price really is validating numericality twice which might be causing a problem. I would instead change it to