|
| 1 | +require 'spec_helper' |
| 2 | + |
| 3 | +describe Grape::Exceptions::Base do |
| 4 | + describe '#compose_message' do |
| 5 | + subject { described_class.new.send(:compose_message, key, attributes) } |
| 6 | + |
| 7 | + let(:key) { :invalid_formatter } |
| 8 | + let(:attributes) { { klass: String, to_format: 'xml' } } |
| 9 | + |
| 10 | + after do |
| 11 | + I18n.available_locales = %i[en] |
| 12 | + I18n.default_locale = :en |
| 13 | + end |
| 14 | + |
| 15 | + context 'when I18n enforces available locales' do |
| 16 | + before { I18n.enforce_available_locales = true } |
| 17 | + |
| 18 | + context 'when the fallback locale is available' do |
| 19 | + before do |
| 20 | + I18n.available_locales = %i[de en] |
| 21 | + I18n.default_locale = :de |
| 22 | + end |
| 23 | + |
| 24 | + it 'returns the translated message' do |
| 25 | + expect(subject).to eq('cannot convert String to xml') |
| 26 | + end |
| 27 | + end |
| 28 | + |
| 29 | + context 'when the fallback locale is not available' do |
| 30 | + before do |
| 31 | + I18n.available_locales = %i[de jp] |
| 32 | + I18n.default_locale = :de |
| 33 | + end |
| 34 | + |
| 35 | + it 'returns the translation string' do |
| 36 | + expect(subject).to eq("grape.errors.messages.#{key}") |
| 37 | + end |
| 38 | + end |
| 39 | + end |
| 40 | + |
| 41 | + context 'when I18n does not enforce available locales' do |
| 42 | + before { I18n.enforce_available_locales = false } |
| 43 | + |
| 44 | + context 'when the fallback locale is available' do |
| 45 | + before { I18n.available_locales = %i[de en] } |
| 46 | + |
| 47 | + it 'returns the translated message' do |
| 48 | + expect(subject).to eq('cannot convert String to xml') |
| 49 | + end |
| 50 | + end |
| 51 | + |
| 52 | + context 'when the fallback locale is not available' do |
| 53 | + before { I18n.available_locales = %i[de jp] } |
| 54 | + |
| 55 | + it 'returns the translated message' do |
| 56 | + expect(subject).to eq('cannot convert String to xml') |
| 57 | + end |
| 58 | + end |
| 59 | + end |
| 60 | + end |
| 61 | +end |
0 commit comments