2017-07-16 107 views
1

有模型如下:比较rspec的定制的ActiveRecord :: RecordInvalid错误的消息

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> 

期望等待一个没有报价的值,而值se NT有引用;它没有对

我得到默认的设置非常相同的错误:

activerecord: 
    errors: 
     models: 
     item: 
      attributes: 
      title: 
       taken: 'translated error' 

同样的测试通过用如下:

expect { xxx }.to raise_error

expect { xxx }.to raise_error(ActiveRecord::RecordInvalid)

也是如此使用默认设置:

个感谢,如果对一个

回答

1

ActiveRecord的任何帮助添加前缀Validation failed:的错误消息。在您的测试试试这个:

en: 
    activerecord: 
    errors: 
     messages: 
     record_invalid: "Validation failed: %{errors}" 

expect { xxx }.to raise_error(
    ActiveRecord::RecordInvalid, 
    "Validation failed: " + I18n.t('errors.key', value: '...passing the title...') 
) 

你可以在你的语言文件中设置下面的键更改此默认