diff --git a/test/cases/backend/active_record_test.rb b/test/cases/backend/active_record_test.rb index 9a64387d..6244b8ca 100644 --- a/test/cases/backend/active_record_test.rb +++ b/test/cases/backend/active_record_test.rb @@ -40,9 +40,11 @@ def test_store_translations_does_not_allow_ambigous_keys_2 assert_equal 'foo', I18n.t(:foo) end - def test_missing_translations_table_does_not_cause_available_locales_to_error - I18n::Backend::ActiveRecord::Translation.expects(:available_locales).raises(::ActiveRecord::StatementInvalid) - assert_equal [], I18n.backend.available_locales + with_mocha do + def test_missing_translations_table_does_not_cause_available_locales_to_error + I18n::Backend::ActiveRecord::Translation.expects(:available_locales).raises(::ActiveRecord::StatementInvalid) + assert_equal [], I18n.backend.available_locales + end end def test_expand_keys diff --git a/test/cases/backend/cache_test.rb b/test/cases/backend/cache_test.rb index 65f22cdd..7387a9da 100644 --- a/test/cases/backend/cache_test.rb +++ b/test/cases/backend/cache_test.rb @@ -29,22 +29,24 @@ def test_uses_cache assert I18n.cache_store.is_a?(ActiveSupport::Cache::MemoryStore) end - define_method "test translate hits the backend and caches the response" do - I18n.backend.expects(:lookup).returns('Foo') - assert_equal 'Foo', I18n.t(:foo) + with_mocha do + define_method "test translate hits the backend and caches the response" do + I18n.backend.expects(:lookup).returns('Foo') + assert_equal 'Foo', I18n.t(:foo) - I18n.backend.expects(:lookup).never - assert_equal 'Foo', I18n.t(:foo) + I18n.backend.expects(:lookup).never + assert_equal 'Foo', I18n.t(:foo) - I18n.backend.expects(:lookup).returns('Bar') - assert_equal 'Bar', I18n.t(:bar) - end + I18n.backend.expects(:lookup).returns('Bar') + assert_equal 'Bar', I18n.t(:bar) + end - define_method "test still raises MissingTranslationData but also caches it" do - I18n.backend.expects(:lookup).returns(nil) - assert_raises(I18n::MissingTranslationData) { I18n.t(:missing, :raise => true) } - I18n.backend.expects(:lookup).never - assert_raises(I18n::MissingTranslationData) { I18n.t(:missing, :raise => true) } + define_method "test still raises MissingTranslationData but also caches it" do + I18n.backend.expects(:lookup).returns(nil) + assert_raises(I18n::MissingTranslationData) { I18n.t(:missing, :raise => true) } + I18n.backend.expects(:lookup).never + assert_raises(I18n::MissingTranslationData) { I18n.t(:missing, :raise => true) } + end end define_method "test uses 'i18n' as a cache key namespace by default" do diff --git a/test/cases/i18n_test.rb b/test/cases/i18n_test.rb index 01d08a80..4e7d56e9 100644 --- a/test/cases/i18n_test.rb +++ b/test/cases/i18n_test.rb @@ -71,26 +71,28 @@ def test_can_set_exception_handler I18n.exception_handler = :default_exception_handler # revert it end - def test_uses_custom_exception_handler - I18n.exception_handler = :custom_exception_handler - I18n.expects(:custom_exception_handler) - I18n.translate :bogus - I18n.exception_handler = :default_exception_handler # revert it - end + with_mocha do + def test_uses_custom_exception_handler + I18n.exception_handler = :custom_exception_handler + I18n.expects(:custom_exception_handler) + I18n.translate :bogus + I18n.exception_handler = :default_exception_handler # revert it + end - def test_delegates_translate_to_backend - I18n.backend.expects(:translate).with 'de', :foo, {} - I18n.translate :foo, :locale => 'de' - end + def test_delegates_translate_to_backend + I18n.backend.expects(:translate).with 'de', :foo, {} + I18n.translate :foo, :locale => 'de' + end - def test_delegates_localize_to_backend - I18n.backend.expects(:localize).with 'de', :whatever, :default - I18n.localize :whatever, :locale => 'de' - end + def test_delegates_localize_to_backend + I18n.backend.expects(:localize).with 'de', :whatever, :default + I18n.localize :whatever, :locale => 'de' + end - def test_translate_given_no_locale_uses_i18n_locale - I18n.backend.expects(:translate).with :en, :foo, {} - I18n.translate :foo + def test_translate_given_no_locale_uses_i18n_locale + I18n.backend.expects(:translate).with :en, :foo, {} + I18n.translate :foo + end end def test_translate_on_nested_symbol_keys_works @@ -117,10 +119,12 @@ def test_translate_with_dot_separated_key_array_and_scope_works assert_equal [".", ","], I18n.t(%w(format.separator format.delimiter), :scope => 'currency') end - def test_translate_with_options_using_scope_works - I18n.backend.expects(:translate).with('de', :precision, :scope => :"currency.format") - I18n.with_options :locale => 'de', :scope => :'currency.format' do |locale| - locale.t :precision + with_mocha do + def test_translate_with_options_using_scope_works + I18n.backend.expects(:translate).with('de', :precision, :scope => :"currency.format") + I18n.with_options :locale => 'de', :scope => :'currency.format' do |locale| + locale.t :precision + end end end diff --git a/test/test_helper.rb b/test/test_helper.rb index 67a00cc1..a5475da9 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -5,12 +5,18 @@ require 'rubygems' require 'test/unit' -require 'mocha' require 'i18n' require 'i18n/core_ext/object/meta_class' require 'time' require 'yaml' +begin + require 'mocha' +rescue LoadError + puts "skipping tests using mocha as mocha can't be found" +end + + Dir[File.dirname(__FILE__) + '/api/**/*.rb'].each do |filename| require filename end @@ -24,11 +30,15 @@ # end # end -class Test::Unit::TestCase +class Test::Unit::TestCase def self.test(name, &block) define_method("test: " + name, &block) end + def self.with_mocha + yield if Object.respond_to?(:expects) + end + def teardown I18n.locale = nil I18n.default_locale = :en @@ -40,13 +50,13 @@ def teardown def translations I18n.backend.instance_variable_get(:@translations) end - + def store_translations(*args) data = args.pop locale = args.pop || :en I18n.backend.store_translations(locale, data) end - + def locales_dir File.dirname(__FILE__) + '/fixtures/locales' end