class Post < ActiveRecord::Base
validates_presence_of :value
...
end
one:
row: 2
col: 3
name: 'Test'
require 'test_helper'
class SimulationsModelTest < ActiveSupport::TestCase
def setup
@post = Post.new
end
test 'post must have name' do
@post.name = ' '
assert @post.valid?
end
端
为什么以上测试用例失败?但是,以下测试用例正在通过
test 'post must have name' do
@post.name = ' '
assert_not @post.valid?
端
在我的模型中,我没有设置presence => true
名称,那么为什么第一个测试用例失败?
如果我有两个validates_presence of : value , :name
怎么办?我的测试用例是:
test 'post must have name' do
@post.name = 'some'
@post.value = 'test'
assert @post.valid?
end
它必须通过测试用例,但它失败了。我在models
答案 0 :(得分:1)
因为@post
仅在value
有效时才有效,并且在您的测试中,您只需为其分配name
。
在你的模特中:
validates_presence_of :value
您不能指定value
。你不能在任何地方做@post.value = 'some_value'
。
编辑:
之前的:
表示其代表属性名称的符号。您可以通过以下方式为多个字段定义验证:
validates_presence_of :value, :name