Module: AnnoTranslate::Assertions

Included in:
Test::Unit::TestCase
Defined in:
lib/annotranslate.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


245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
# File 'lib/annotranslate.rb', line 245

def assert_translated(msg = nil, &block)

  # Enable strict mode to force raising of MissingTranslationData
  AnnoTranslate.strict_mode(true)

  msg ||= "Expected no missing translation keys"

  begin
    yield
    # Credtit for running the assertion
    assert(true, msg)
  rescue I18n::MissingTranslationData => e
    # Fail!
    error = build_message(msg, "Exception raised:\n?", e)
    AnnoTranslate.log.error
    assert_block(error) {false}
  ensure
    # uninstall strict exception handler
    AnnoTranslate.strict_mode(false)
  end

end