diff --git a/activemodel/test/cases/errors_test.rb b/activemodel/test/cases/errors_test.rb index d10c20caad..3fa63917d0 100644 --- a/activemodel/test/cases/errors_test.rb +++ b/activemodel/test/cases/errors_test.rb @@ -27,6 +27,14 @@ class ErrorsTest < ActiveModel::TestCase end end + def setup + @mock_generator = MiniTest::Mock.new + end + + def teardown + @mock_generator.verify + end + def test_delete errors = ActiveModel::Errors.new(self) errors[:foo] << 'omg' @@ -299,60 +307,74 @@ class ErrorsTest < ActiveModel::TestCase test "add_on_empty generates message" do person = Person.new - person.errors.expects(:generate_message).with(:name, :empty, {}) - assert_deprecated do - person.errors.add_on_empty :name + @mock_generator.expect(:call, nil, [:name, :empty, {}]) + person.errors.stub(:generate_message, @mock_generator) do + assert_deprecated do + person.errors.add_on_empty :name + end end end test "add_on_empty generates message for multiple attributes" do person = Person.new - person.errors.expects(:generate_message).with(:name, :empty, {}) - person.errors.expects(:generate_message).with(:age, :empty, {}) - assert_deprecated do - person.errors.add_on_empty [:name, :age] + @mock_generator.expect(:call, nil, [:name, :empty, {}]) + @mock_generator.expect(:call, nil, [:age, :empty, {}]) + person.errors.stub(:generate_message, @mock_generator) do + assert_deprecated do + person.errors.add_on_empty [:name, :age] + end end end test "add_on_empty generates message with custom default message" do person = Person.new - person.errors.expects(:generate_message).with(:name, :empty, { message: 'custom' }) - assert_deprecated do - person.errors.add_on_empty :name, message: 'custom' + @mock_generator.expect(:call, nil, [:name, :empty, { message: 'custom' }]) + person.errors.stub(:generate_message, @mock_generator) do + assert_deprecated do + person.errors.add_on_empty :name, message: 'custom' + end end end test "add_on_empty generates message with empty string value" do person = Person.new person.name = '' - person.errors.expects(:generate_message).with(:name, :empty, {}) - assert_deprecated do - person.errors.add_on_empty :name + @mock_generator.expect(:call, nil, [:name, :empty, {}]) + person.errors.stub(:generate_message, @mock_generator) do + assert_deprecated do + person.errors.add_on_empty :name + end end end test "add_on_blank generates message" do person = Person.new - person.errors.expects(:generate_message).with(:name, :blank, {}) - assert_deprecated do - person.errors.add_on_blank :name + @mock_generator.expect(:call, nil, [:name, :blank, {}]) + person.errors.stub(:generate_message, @mock_generator) do + assert_deprecated do + person.errors.add_on_blank :name + end end end test "add_on_blank generates message for multiple attributes" do person = Person.new - person.errors.expects(:generate_message).with(:name, :blank, {}) - person.errors.expects(:generate_message).with(:age, :blank, {}) - assert_deprecated do - person.errors.add_on_blank [:name, :age] + @mock_generator.expect(:call, nil, [:name, :blank, {}]) + @mock_generator.expect(:call, nil, [:age, :blank, {}]) + person.errors.stub(:generate_message, @mock_generator) do + assert_deprecated do + person.errors.add_on_blank [:name, :age] + end end end test "add_on_blank generates message with custom default message" do person = Person.new - person.errors.expects(:generate_message).with(:name, :blank, { message: 'custom' }) - assert_deprecated do - person.errors.add_on_blank :name, message: 'custom' + @mock_generator.expect(:call, nil, [:name, :blank, { message: 'custom' }]) + person.errors.stub(:generate_message, @mock_generator) do + assert_deprecated do + person.errors.add_on_blank :name, message: 'custom' + end end end diff --git a/activemodel/test/cases/helper.rb b/activemodel/test/cases/helper.rb index 2b9de5e5d2..0d179ea9ad 100644 --- a/activemodel/test/cases/helper.rb +++ b/activemodel/test/cases/helper.rb @@ -12,7 +12,7 @@ I18n.enforce_available_locales = false require 'active_support/testing/autorun' -require 'mocha/setup' # FIXME: stop using mocha +require 'minitest/mock' # Skips the current run on Rubinius using Minitest::Assertions#skip def rubinius_skip(message = '') diff --git a/activemodel/test/cases/validations/i18n_validation_test.rb b/activemodel/test/cases/validations/i18n_validation_test.rb index 70ee7afecc..be17575402 100644 --- a/activemodel/test/cases/validations/i18n_validation_test.rb +++ b/activemodel/test/cases/validations/i18n_validation_test.rb @@ -11,6 +11,7 @@ class I18nValidationTest < ActiveModel::TestCase I18n.load_path.clear I18n.backend = I18n::Backend::Simple.new I18n.backend.store_translations('en', errors: { messages: { custom: nil } }) + @mock_generator = MiniTest::Mock.new end def teardown @@ -18,6 +19,7 @@ class I18nValidationTest < ActiveModel::TestCase I18n.load_path.replace @old_load_path I18n.backend = @old_backend I18n.backend.reload! + @mock_generator.verify end def test_full_message_encoding @@ -30,8 +32,10 @@ class I18nValidationTest < ActiveModel::TestCase def test_errors_full_messages_translates_human_attribute_name_for_model_attributes @person.errors.add(:name, 'not found') - Person.expects(:human_attribute_name).with(:name, default: 'Name').returns("Person's name") - assert_equal ["Person's name not found"], @person.errors.full_messages + @mock_generator.expect(:call, "Person's name", [:name, default: 'Name']) + Person.stub(:human_attribute_name, @mock_generator) do + assert_equal ["Person's name not found"], @person.errors.full_messages + end end def test_errors_full_messages_uses_format @@ -60,8 +64,10 @@ class I18nValidationTest < ActiveModel::TestCase test "validates_confirmation_of on generated message #{name}" do Person.validates_confirmation_of :title, validation_options @person.title_confirmation = 'foo' - @person.errors.expects(:generate_message).with(:title_confirmation, :confirmation, generate_message_options.merge(attribute: 'Title')) - @person.valid? + @mock_generator.expect(:call, nil, [:title_confirmation, :confirmation, generate_message_options.merge(attribute: 'Title')]) + @person.errors.stub(:generate_message, @mock_generator) do + @person.valid? + end end end @@ -70,8 +76,10 @@ class I18nValidationTest < ActiveModel::TestCase COMMON_CASES.each do |name, validation_options, generate_message_options| test "validates_acceptance_of on generated message #{name}" do Person.validates_acceptance_of :title, validation_options.merge(allow_nil: false) - @person.errors.expects(:generate_message).with(:title, :accepted, generate_message_options) - @person.valid? + @mock_generator.expect(:call, nil, [:title, :accepted, generate_message_options]) + @person.errors.stub(:generate_message, @mock_generator) do + @person.valid? + end end end @@ -80,8 +88,10 @@ class I18nValidationTest < ActiveModel::TestCase COMMON_CASES.each do |name, validation_options, generate_message_options| test "validates_presence_of on generated message #{name}" do Person.validates_presence_of :title, validation_options - @person.errors.expects(:generate_message).with(:title, :blank, generate_message_options) - @person.valid? + @mock_generator.expect(:call, nil, [:title, :blank, generate_message_options]) + @person.errors.stub(:generate_message, @mock_generator) do + @person.valid? + end end end @@ -90,8 +100,10 @@ class I18nValidationTest < ActiveModel::TestCase COMMON_CASES.each do |name, validation_options, generate_message_options| test "validates_length_of for :withing on generated message when too short #{name}" do Person.validates_length_of :title, validation_options.merge(within: 3..5) - @person.errors.expects(:generate_message).with(:title, :too_short, generate_message_options.merge(count: 3)) - @person.valid? + @mock_generator.expect(:call, nil, [:title, :too_short, generate_message_options.merge(count: 3)]) + @person.errors.stub(:generate_message, @mock_generator) do + @person.valid? + end end end @@ -101,8 +113,10 @@ class I18nValidationTest < ActiveModel::TestCase test "validates_length_of for :too_long generated message #{name}" do Person.validates_length_of :title, validation_options.merge(within: 3..5) @person.title = 'this title is too long' - @person.errors.expects(:generate_message).with(:title, :too_long, generate_message_options.merge(count: 5)) - @person.valid? + @mock_generator.expect(:call, nil, [:title, :too_long, generate_message_options.merge(count: 5)]) + @person.errors.stub(:generate_message, @mock_generator) do + @person.valid? + end end end @@ -111,8 +125,10 @@ class I18nValidationTest < ActiveModel::TestCase COMMON_CASES.each do |name, validation_options, generate_message_options| test "validates_length_of for :is on generated message #{name}" do Person.validates_length_of :title, validation_options.merge(is: 5) - @person.errors.expects(:generate_message).with(:title, :wrong_length, generate_message_options.merge(count: 5)) - @person.valid? + @mock_generator.expect(:call, nil, [:title, :wrong_length, generate_message_options.merge(count: 5)]) + @person.errors.stub(:generate_message, @mock_generator) do + @person.valid? + end end end @@ -122,8 +138,10 @@ class I18nValidationTest < ActiveModel::TestCase test "validates_format_of on generated message #{name}" do Person.validates_format_of :title, validation_options.merge(with: /\A[1-9][0-9]*\z/) @person.title = '72x' - @person.errors.expects(:generate_message).with(:title, :invalid, generate_message_options.merge(value: '72x')) - @person.valid? + @mock_generator.expect(:call, nil, [:title, :invalid, generate_message_options.merge(value: '72x')]) + @person.errors.stub(:generate_message, @mock_generator) do + @person.valid? + end end end @@ -133,8 +151,10 @@ class I18nValidationTest < ActiveModel::TestCase test "validates_inclusion_of on generated message #{name}" do Person.validates_inclusion_of :title, validation_options.merge(in: %w(a b c)) @person.title = 'z' - @person.errors.expects(:generate_message).with(:title, :inclusion, generate_message_options.merge(value: 'z')) - @person.valid? + @mock_generator.expect(:call, nil, [:title, :inclusion, generate_message_options.merge(value: 'z')]) + @person.errors.stub(:generate_message, @mock_generator) do + @person.valid? + end end end @@ -144,8 +164,10 @@ class I18nValidationTest < ActiveModel::TestCase test "validates_inclusion_of using :within on generated message #{name}" do Person.validates_inclusion_of :title, validation_options.merge(within: %w(a b c)) @person.title = 'z' - @person.errors.expects(:generate_message).with(:title, :inclusion, generate_message_options.merge(value: 'z')) - @person.valid? + @mock_generator.expect(:call, nil, [:title, :inclusion, generate_message_options.merge(value: 'z')]) + @person.errors.stub(:generate_message, @mock_generator) do + @person.valid? + end end end @@ -155,8 +177,10 @@ class I18nValidationTest < ActiveModel::TestCase test "validates_exclusion_of generated message #{name}" do Person.validates_exclusion_of :title, validation_options.merge(in: %w(a b c)) @person.title = 'a' - @person.errors.expects(:generate_message).with(:title, :exclusion, generate_message_options.merge(value: 'a')) - @person.valid? + @mock_generator.expect(:call, nil, [:title, :exclusion, generate_message_options.merge(value: 'a')]) + @person.errors.stub(:generate_message, @mock_generator) do + @person.valid? + end end end @@ -166,8 +190,10 @@ class I18nValidationTest < ActiveModel::TestCase test "validates_exclusion_of using :within generated message #{name}" do Person.validates_exclusion_of :title, validation_options.merge(within: %w(a b c)) @person.title = 'a' - @person.errors.expects(:generate_message).with(:title, :exclusion, generate_message_options.merge(value: 'a')) - @person.valid? + @mock_generator.expect(:call, nil, [:title, :exclusion, generate_message_options.merge(value: 'a')]) + @person.errors.stub(:generate_message, @mock_generator) do + @person.valid? + end end end @@ -177,8 +203,10 @@ class I18nValidationTest < ActiveModel::TestCase test "validates_numericality_of generated message #{name}" do Person.validates_numericality_of :title, validation_options @person.title = 'a' - @person.errors.expects(:generate_message).with(:title, :not_a_number, generate_message_options.merge(value: 'a')) - @person.valid? + @mock_generator.expect(:call, nil, [:title, :not_a_number, generate_message_options.merge(value: 'a')]) + @person.errors.stub(:generate_message, @mock_generator) do + @person.valid? + end end end @@ -188,8 +216,10 @@ class I18nValidationTest < ActiveModel::TestCase test "validates_numericality_of for :only_integer on generated message #{name}" do Person.validates_numericality_of :title, validation_options.merge(only_integer: true) @person.title = '0.0' - @person.errors.expects(:generate_message).with(:title, :not_an_integer, generate_message_options.merge(value: '0.0')) - @person.valid? + @mock_generator.expect(:call, nil, [:title, :not_an_integer, generate_message_options.merge(value: '0.0')]) + @person.errors.stub(:generate_message, @mock_generator) do + @person.valid? + end end end @@ -199,8 +229,10 @@ class I18nValidationTest < ActiveModel::TestCase test "validates_numericality_of for :odd on generated message #{name}" do Person.validates_numericality_of :title, validation_options.merge(only_integer: true, odd: true) @person.title = 0 - @person.errors.expects(:generate_message).with(:title, :odd, generate_message_options.merge(value: 0)) - @person.valid? + @mock_generator.expect(:call, nil, [:title, :odd, generate_message_options.merge(value: 0)]) + @person.errors.stub(:generate_message, @mock_generator) do + @person.valid? + end end end @@ -210,8 +242,10 @@ class I18nValidationTest < ActiveModel::TestCase test "validates_numericality_of for :less_than on generated message #{name}" do Person.validates_numericality_of :title, validation_options.merge(only_integer: true, less_than: 0) @person.title = 1 - @person.errors.expects(:generate_message).with(:title, :less_than, generate_message_options.merge(value: 1, count: 0)) - @person.valid? + @mock_generator.expect(:call, nil, [:title, :less_than, generate_message_options.merge(value: 1, count: 0)]) + @person.errors.stub(:generate_message, @mock_generator) do + @person.valid? + end end end diff --git a/activemodel/test/cases/validations/with_validation_test.rb b/activemodel/test/cases/validations/with_validation_test.rb index 01804032f0..9ee8b79da9 100644 --- a/activemodel/test/cases/validations/with_validation_test.rb +++ b/activemodel/test/cases/validations/with_validation_test.rb @@ -97,12 +97,14 @@ class ValidatesWithTest < ActiveModel::TestCase test "passes all configuration options to the validator class" do topic = Topic.new - validator = mock() - validator.expects(:new).with(foo: :bar, if: "1 == 1", class: Topic).returns(validator) - validator.expects(:validate).with(topic) + validator = MiniTest::Mock.new + validator.expect(:new, validator, [{foo: :bar, if: "1 == 1", class: Topic}]) + validator.expect(:validate, nil, [topic]) + validator.expect(:is_a?, false, [Symbol]) Topic.validates_with(validator, if: "1 == 1", foo: :bar) assert topic.valid? + validator.verify end test "validates_with with options" do