Module: Translator::Assertions

Included in:
Test::Unit::TestCase
Defined in:
lib/translator.rb

Overview

Additions to TestUnit to make testing i18n easier

Instance Method Summary collapse

Instance Method Details

#assert_translated(msg = nil, &block) ⇒ Object

Assert that within the block there are no missing translation keys. This can be used in a more tailored way that the global strict_mode

Example:

assert_translated do
  str = "Test will fail for #{I18n.t('a_missing_key')}"
end


217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
# File 'lib/translator.rb', line 217

def assert_translated(msg = nil, &block)
  
  # Enable strict mode to force raising of MissingTranslationData
  Translator.strict_mode(true)
  
  msg ||= "Expected no missing translation keys"
  
  begin
    yield
    # Credtit for running the assertion
    assert(true, msg)
  rescue I18n::MissingTranslationData => e
    # Fail!
    assert_block(build_message(msg, "Exception raised:\n?", e)) {false}
  ensure
    # uninstall strict exception handler
    Translator.strict_mode(false)
  end
    
end