errors.added? returns 与 :exclusion 一起使用时为 false
errors.added? returns false when used with :exclusion
在递归 rails 模型中,我验证了模型无法引用自身:
validates :parent_entity, exclusion: { in: ->(entity) { [entity] } }
这是成功的,并且设置了带有正确消息的排除错误。我可以通过 rails 控制台批准。
在Rspec测试中我想检查是否添加了适当的排除错误:
it 'parent_entity cannot be same entity as child_entity' do
@child_entity1.parent_entity = @child_entity1
@child_entity1.valid?
expect(@child_entity1.errors.added?(:parent_entity, :exclusion)).to be_truthy
end
测试在预期中返回错误值失败。
前面的方法对 e 非常有效。 G。空白错误但不排除。如果我在测试中用已解决的错误消息交换':exclusion' 'is reserved',我可以让它工作,但这不是我想要和应该做的。
取自rails docs为补充?方法:
If the error message requires options, then it returns true with the
correct options, or false with incorrect or missing options.
因此选项必须完全匹配。 :exclusion
的必要选项是 value
并作为最后一个参数传递:
@child_entity1.errors.added?(:parent_entity, :exclusion, value: @child_entity1)
在递归 rails 模型中,我验证了模型无法引用自身:
validates :parent_entity, exclusion: { in: ->(entity) { [entity] } }
这是成功的,并且设置了带有正确消息的排除错误。我可以通过 rails 控制台批准。
在Rspec测试中我想检查是否添加了适当的排除错误:
it 'parent_entity cannot be same entity as child_entity' do
@child_entity1.parent_entity = @child_entity1
@child_entity1.valid?
expect(@child_entity1.errors.added?(:parent_entity, :exclusion)).to be_truthy
end
测试在预期中返回错误值失败。
前面的方法对 e 非常有效。 G。空白错误但不排除。如果我在测试中用已解决的错误消息交换':exclusion' 'is reserved',我可以让它工作,但这不是我想要和应该做的。
取自rails docs为补充?方法:
If the error message requires options, then it returns true with the correct options, or false with incorrect or missing options.
因此选项必须完全匹配。 :exclusion
的必要选项是 value
并作为最后一个参数传递:
@child_entity1.errors.added?(:parent_entity, :exclusion, value: @child_entity1)