errors_spec.rb 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. # Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe ActiveModel::Error, :aggregate_failures do
  4. context 'with a default rails error available from locale.yml (including interpolation)' do
  5. let(:error) { User.first.errors.add(:firstname, :equal_to, count: '25') }
  6. context 'when using the standard error format and the default locale' do
  7. it 'produces a standard Rails error including the field name' do
  8. expect(error.message).to eq('must be equal to 25')
  9. expect(error.full_message).to eq('Firstname must be equal to 25')
  10. end
  11. end
  12. context 'when using a custom error format and a custom locale' do
  13. let(:custom_translations) { { 'must be equal to %{count}' => 'muss den Wert %{count} haben', 'This field %s' => 'Dieses Feld %<message>s' } } # rubocop:disable Style/FormatStringToken
  14. it 'produces a custom error NOT including the field name' do
  15. allow(Translation).to receive(:translate) { |_locale, string| custom_translations[string] || string }
  16. expect(error.message).to eq('must be equal to 25')
  17. expect(error.localized_full_message(no_field_name: true, locale: 'de-de')).to eq('Dieses Feld muss den Wert 25 haben')
  18. end
  19. end
  20. end
  21. context 'with a custom error not available from locale.yml (does not support interpolation)' do
  22. let(:error) { User.first.errors.add(:firstname, 'is unknown') }
  23. context 'when using the standard error format and the default locale' do
  24. it 'produces a standard Rails error including the field name' do
  25. expect(error.message).to eq('is unknown')
  26. expect(error.full_message).to eq('Firstname is unknown')
  27. end
  28. end
  29. context 'when using a custom error format and a custom locale' do
  30. let(:custom_translations) { { 'is unknown' => 'ist unbekannt', 'This field %s' => 'Dieses Feld %{message}' } }
  31. it 'produces a custom error NOT including the field name' do
  32. allow(Translation).to receive(:translate) { |_locale, string| custom_translations[string] || string }
  33. expect(error.message).to eq('is unknown')
  34. expect(error.localized_full_message(no_field_name: true, locale: 'de-de')).to eq('Dieses Feld ist unbekannt')
  35. end
  36. end
  37. end
  38. end