比较rspec自定义ActiveRecord :: RecordInvalid错误消息 [英] Comparing rspec custom ActiveRecord::RecordInvalid errors messages
问题描述
模型中具有以下内容:
validates_uniqueness_of :title,
if: proc { |item| item.item_type == 'tag' },
case_sensitive: false,
message: I18n.t('errors.key', value: "%{value}")
并尝试在rspec中使用以下命令进行验证:
and trying to validate in rspec with the following :
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>
期望值等待一个没有报价的值,而发送的值带有报价;
The expectation awaits a value without quote, while the value sent has quote; it fails on that
我在默认设置下遇到了同样的错误:
I get the very same error with the default setup :
activerecord:
errors:
models:
item:
attributes:
title:
taken: 'translated error'
同一测试通过以下测试:
The same test passes with the followings :
期望{xxx}。以提高错误
期望{xxx}。以提高错误(ActiveRecord :: RecordInvalid)
使用默认设置也是如此:
So does using the default setup :
感谢任何帮助
推荐答案
ActiveRecord将前缀 Validation failed:
添加到错误信息。相反,请在测试中尝试以下操作:
ActiveRecord adds the prefix Validation failed:
to the error message. Try this in your test instead:
expect { xxx }.to raise_error(
ActiveRecord::RecordInvalid,
"Validation failed: " + I18n.t('errors.key', value: '...passing the title...')
)
您可以通过在语言文件中设置以下键来更改此默认值:
You can change this default by setting the following key in your language file:
en:
activerecord:
errors:
messages:
record_invalid: "Validation failed: %{errors}"
这篇关于比较rspec自定义ActiveRecord :: RecordInvalid错误消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!