1
雖然當我將%{attribute}
傳遞給它時,測試失敗,但我有一些自定義錯誤消息的翻譯文件設置,用於某些ActiveRecord驗證。我如何獲得rspec測試來使用翻譯?如何使用i18m將驗證消息中的屬性插入到rspec中
en.locale
en:
activerecord:
errors:
models:
account:
attributes:
name:
invalid: "%{attribute} must only include numbers and letters"
RSpec的測試
it "is invalid with special characters" do
account = FactoryGirl.build(:account, name: "test_account_*name")
account.valid?
expect(account.errors[:name]).to include(I18n.t('activerecord.errors.models.account.attributes.name.invalid'))
end
結果
20) Account is invalid with special characters
Failure/Error: expect(account.errors[:name]).to include(I18n.t('activerecord.errors.models.account.attributes.name.invalid'))
expected ["Name must only include numbers and letters"] to include "%{attribute} must only include numbers and letters"
# ./spec/models/account_spec.rb:29:in `block (2 levels) in <top (required)>'