2017-07-16 108 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...') 
) 

你可以在你的語言文件中設置下面的鍵更改此默認