模型中有以下内容:
validates_uniqueness_of :title,
if: proc { |item| item.item_type == 'tag' },
case_sensitive: false,
message: I18n.t('errors.key', value: "%{value}")
并尝试使用以下内容在 rspec 中进行验证:
expect { xxx }.to raise_error(
ActiveRecord::RecordInvalid,
I18n.t('errors.key', value: '...passing the title...')
)
我坚持以下非常接近的观点:
ActiveRecord::RecordInvalid with "translated error",
got #<ActiveRecord::RecordInvalid: Validation failed: translated error>
期望等待一个不带引号的值,而发送的值带有引号;它失败了
我在默认设置下遇到了完全相同的错误:
activerecord:
errors:
models:
item:
attributes:
title:
taken: 'translated error'
相同的测试通过以下内容:
expect { xxx }.to raise_error
expect { xxx }.to raise_error( ActiveRecord::RecordInvalid )
使用默认设置也是如此:
如果对此有任何帮助,谢谢
ActiveRecord 添加前缀Validation failed:
到错误消息。在你的测试中试试这个:
expect { xxx }.to raise_error(
ActiveRecord::RecordInvalid,
"Validation failed: " + I18n.t('errors.key', value: '...passing the title...')
)
您可以通过在语言文件中设置以下键来更改此默认值:
en:
activerecord:
errors:
messages:
record_invalid: "Validation failed: %{errors}"
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)