我应该延长或包括ActiveModel:Validations:Callbacks:ClassMethods或ActiveModel:Validations:Callbacks?
相关/固定: Ruby on Rails:表单对象验证不起作用
我有以下验证..
validates :age, numericality: { greater_than_or_equal_to: 0,
only_integer: true,
:allow_blank => true
}
Run Code Online (Sandbox Code Playgroud)
不是必须的,如果输入需要是数字。我注意到如果有人输入单词而不是数字,则提交并通过验证后字段值会更改为 0。我希望它是空白的或输入的值。
仍然没有解决方案,但这里有更多信息。
rspec测试 it "returns error when age is not a number" do
params[:age] = "string"
profile = Registration::Profile.new(user, params)
expect(profile.valid?).to eql false
expect(profile.errors[:age]).to include("is not a number")
end
Run Code Online (Sandbox Code Playgroud)
Rspec 测试失败:
Registration::Profile Validations when not a number returns error when age is not a number
Failure/Error: expect(profile.errors[:age]).to include("is not a number")
expected [] to include "is not a number"
Run Code Online (Sandbox Code Playgroud)
2.6.5 :011 …Run Code Online (Sandbox Code Playgroud)