1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Remove use of mocha from Active Model

This commit is contained in:
Roque Pinel 2015-05-28 18:33:21 -05:00
parent 2f9d88954c
commit 5a6ae7f753
4 changed files with 117 additions and 59 deletions

View file

@ -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,62 +307,76 @@ class ErrorsTest < ActiveModel::TestCase
test "add_on_empty generates message" do
person = Person.new
person.errors.expects(:generate_message).with(:name, :empty, {})
@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, {})
@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' })
@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, {})
@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, {})
@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, {})
@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' })
@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
test "details returns added error detail" do
person = Person.new

View file

@ -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 = '')

View file

@ -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,9 +32,11 @@ 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")
@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
I18n.backend.store_translations('en', errors: { format: "Field %{attribute} %{message}" })
@ -60,40 +64,48 @@ 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'))
@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
# validates_acceptance_of w/ mocha
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)
@mock_generator.expect(:call, nil, [:title, :accepted, generate_message_options])
@person.errors.stub(:generate_message, @mock_generator) do
@person.valid?
end
end
end
# validates_presence_of w/ mocha
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)
@mock_generator.expect(:call, nil, [:title, :blank, generate_message_options])
@person.errors.stub(:generate_message, @mock_generator) do
@person.valid?
end
end
end
# validates_length_of :within too short w/ mocha
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))
@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
# validates_length_of :within too long w/ mocha
@ -101,20 +113,24 @@ 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))
@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
# validates_length_of :is w/ mocha
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))
@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
# validates_format_of w/ mocha
@ -122,10 +138,12 @@ 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'))
@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
# validates_inclusion_of w/ mocha
@ -133,10 +151,12 @@ 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'))
@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
# validates_inclusion_of using :within w/ mocha
@ -144,10 +164,12 @@ 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'))
@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
# validates_exclusion_of w/ mocha
@ -155,10 +177,12 @@ 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'))
@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
# validates_exclusion_of using :within w/ mocha
@ -166,10 +190,12 @@ 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'))
@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
# validates_numericality_of without :only_integer w/ mocha
@ -177,10 +203,12 @@ 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'))
@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
# validates_numericality_of with :only_integer w/ mocha
@ -188,10 +216,12 @@ 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'))
@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
# validates_numericality_of :odd w/ mocha
@ -199,10 +229,12 @@ 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))
@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
# validates_numericality_of :less_than w/ mocha
@ -210,10 +242,12 @@ 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))
@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
# To make things DRY this macro is defined to define 3 tests for every validation case.

View file

@ -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