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

Use private method call assertions in Active Model tests.

Also fix Minitest constant reference.
This commit is contained in:
Kasper Timm Hansen 2015-07-10 23:42:18 +02:00
parent 46b92c4d12
commit 8a389ffc10
4 changed files with 46 additions and 59 deletions

View file

@ -27,14 +27,6 @@ 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'
@ -307,8 +299,7 @@ class ErrorsTest < ActiveModel::TestCase
test "add_on_empty generates message" do
person = Person.new
@mock_generator.expect(:call, nil, [:name, :empty, {}])
person.errors.stub(:generate_message, @mock_generator) do
assert_called_with(person.errors, :generate_message, [:name, :empty, {}]) do
assert_deprecated do
person.errors.add_on_empty :name
end
@ -317,9 +308,8 @@ class ErrorsTest < ActiveModel::TestCase
test "add_on_empty generates message for multiple attributes" do
person = Person.new
@mock_generator.expect(:call, nil, [:name, :empty, {}])
@mock_generator.expect(:call, nil, [:age, :empty, {}])
person.errors.stub(:generate_message, @mock_generator) do
expected_calls = [ [:name, :empty, {}], [:age, :empty, {}] ]
assert_called_with(person.errors, :generate_message, expected_calls) do
assert_deprecated do
person.errors.add_on_empty [:name, :age]
end
@ -328,8 +318,7 @@ class ErrorsTest < ActiveModel::TestCase
test "add_on_empty generates message with custom default message" do
person = Person.new
@mock_generator.expect(:call, nil, [:name, :empty, { message: 'custom' }])
person.errors.stub(:generate_message, @mock_generator) do
assert_called_with(person.errors, :generate_message, [:name, :empty, { message: 'custom' }]) do
assert_deprecated do
person.errors.add_on_empty :name, message: 'custom'
end
@ -339,8 +328,7 @@ class ErrorsTest < ActiveModel::TestCase
test "add_on_empty generates message with empty string value" do
person = Person.new
person.name = ''
@mock_generator.expect(:call, nil, [:name, :empty, {}])
person.errors.stub(:generate_message, @mock_generator) do
assert_called_with(person.errors, :generate_message, [:name, :empty, {}]) do
assert_deprecated do
person.errors.add_on_empty :name
end
@ -349,8 +337,7 @@ class ErrorsTest < ActiveModel::TestCase
test "add_on_blank generates message" do
person = Person.new
@mock_generator.expect(:call, nil, [:name, :blank, {}])
person.errors.stub(:generate_message, @mock_generator) do
assert_called_with(person.errors, :generate_message, [:name, :blank, {}]) do
assert_deprecated do
person.errors.add_on_blank :name
end
@ -359,9 +346,8 @@ class ErrorsTest < ActiveModel::TestCase
test "add_on_blank generates message for multiple attributes" do
person = Person.new
@mock_generator.expect(:call, nil, [:name, :blank, {}])
@mock_generator.expect(:call, nil, [:age, :blank, {}])
person.errors.stub(:generate_message, @mock_generator) do
expected_calls = [ [:name, :blank, {}], [:age, :blank, {}] ]
assert_called_with(person.errors, :generate_message, expected_calls) do
assert_deprecated do
person.errors.add_on_blank [:name, :age]
end
@ -370,8 +356,7 @@ class ErrorsTest < ActiveModel::TestCase
test "add_on_blank generates message with custom default message" do
person = Person.new
@mock_generator.expect(:call, nil, [:name, :blank, { message: 'custom' }])
person.errors.stub(:generate_message, @mock_generator) do
assert_called_with(person.errors, :generate_message, [:name, :blank, { message: 'custom' }]) do
assert_deprecated do
person.errors.add_on_blank :name, message: 'custom'
end

View file

@ -11,6 +11,7 @@ ActiveSupport::Deprecation.debug = true
I18n.enforce_available_locales = false
require 'active_support/testing/autorun'
require 'active_support/testing/method_call_assertions'
require 'minitest/mock'
@ -22,3 +23,7 @@ end
def jruby_skip(message = '')
skip message if defined?(JRUBY_VERSION)
end
class ActiveModel::TestCase
include ActiveSupport::Testing::MethodCallAssertions
end

View file

@ -11,7 +11,6 @@ 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
@ -19,7 +18,6 @@ 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
@ -32,8 +30,7 @@ class I18nValidationTest < ActiveModel::TestCase
def test_errors_full_messages_translates_human_attribute_name_for_model_attributes
@person.errors.add(:name, 'not found')
@mock_generator.expect(:call, "Person's name", [:name, default: 'Name'])
Person.stub(:human_attribute_name, @mock_generator) do
assert_called_with(Person, :human_attribute_name, [:name, default: 'Name'], returns: "Person's name") do
assert_equal ["Person's name not found"], @person.errors.full_messages
end
end
@ -64,8 +61,8 @@ class I18nValidationTest < ActiveModel::TestCase
test "validates_confirmation_of on generated message #{name}" do
Person.validates_confirmation_of :title, validation_options
@person.title_confirmation = 'foo'
@mock_generator.expect(:call, nil, [:title_confirmation, :confirmation, generate_message_options.merge(attribute: 'Title')])
@person.errors.stub(:generate_message, @mock_generator) do
call = [:title_confirmation, :confirmation, generate_message_options.merge(attribute: 'Title')]
assert_called_with(@person.errors, :generate_message, call) do
@person.valid?
end
end
@ -76,8 +73,8 @@ 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)
@mock_generator.expect(:call, nil, [:title, :accepted, generate_message_options])
@person.errors.stub(:generate_message, @mock_generator) do
call = [:title, :accepted, generate_message_options]
assert_called_with(@person.errors, :generate_message, call) do
@person.valid?
end
end
@ -88,8 +85,8 @@ 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
@mock_generator.expect(:call, nil, [:title, :blank, generate_message_options])
@person.errors.stub(:generate_message, @mock_generator) do
call = [:title, :blank, generate_message_options]
assert_called_with(@person.errors, :generate_message, call) do
@person.valid?
end
end
@ -100,8 +97,8 @@ class I18nValidationTest < ActiveModel::TestCase
COMMON_CASES.each do |name, validation_options, generate_message_options|
test "validates_length_of for :within on generated message when too short #{name}" do
Person.validates_length_of :title, validation_options.merge(within: 3..5)
@mock_generator.expect(:call, nil, [:title, :too_short, generate_message_options.merge(count: 3)])
@person.errors.stub(:generate_message, @mock_generator) do
call = [:title, :too_short, generate_message_options.merge(count: 3)]
assert_called_with(@person.errors, :generate_message, call) do
@person.valid?
end
end
@ -113,8 +110,8 @@ 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'
@mock_generator.expect(:call, nil, [:title, :too_long, generate_message_options.merge(count: 5)])
@person.errors.stub(:generate_message, @mock_generator) do
call = [:title, :too_long, generate_message_options.merge(count: 5)]
assert_called_with(@person.errors, :generate_message, ) do
@person.valid?
end
end
@ -125,8 +122,8 @@ 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)
@mock_generator.expect(:call, nil, [:title, :wrong_length, generate_message_options.merge(count: 5)])
@person.errors.stub(:generate_message, @mock_generator) do
call = [:title, :wrong_length, generate_message_options.merge(count: 5)]
assert_called_with(@person.errors, :generate_message, call) do
@person.valid?
end
end
@ -138,8 +135,8 @@ 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'
@mock_generator.expect(:call, nil, [:title, :invalid, generate_message_options.merge(value: '72x')])
@person.errors.stub(:generate_message, @mock_generator) do
call = [:title, :invalid, generate_message_options.merge(value: '72x')]
assert_called_with(@person.errors, :generate_message, call) do
@person.valid?
end
end
@ -151,8 +148,8 @@ 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'
@mock_generator.expect(:call, nil, [:title, :inclusion, generate_message_options.merge(value: 'z')])
@person.errors.stub(:generate_message, @mock_generator) do
call = [:title, :inclusion, generate_message_options.merge(value: 'z')]
assert_called_with(@person.errors, :generate_message, call) do
@person.valid?
end
end
@ -164,8 +161,8 @@ 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'
@mock_generator.expect(:call, nil, [:title, :inclusion, generate_message_options.merge(value: 'z')])
@person.errors.stub(:generate_message, @mock_generator) do
call = [:title, :inclusion, generate_message_options.merge(value: 'z')]
assert_called_with(@person.errors, :generate_message, call) do
@person.valid?
end
end
@ -177,8 +174,8 @@ 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'
@mock_generator.expect(:call, nil, [:title, :exclusion, generate_message_options.merge(value: 'a')])
@person.errors.stub(:generate_message, @mock_generator) do
call = [:title, :exclusion, generate_message_options.merge(value: 'a')]
assert_called_with(@person.errors, :generate_message, call) do
@person.valid?
end
end
@ -190,8 +187,8 @@ 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'
@mock_generator.expect(:call, nil, [:title, :exclusion, generate_message_options.merge(value: 'a')])
@person.errors.stub(:generate_message, @mock_generator) do
call = [:title, :exclusion, generate_message_options.merge(value: 'a')]
assert_called_with(@person.errors, :generate_message, call) do
@person.valid?
end
end
@ -203,8 +200,8 @@ class I18nValidationTest < ActiveModel::TestCase
test "validates_numericality_of generated message #{name}" do
Person.validates_numericality_of :title, validation_options
@person.title = 'a'
@mock_generator.expect(:call, nil, [:title, :not_a_number, generate_message_options.merge(value: 'a')])
@person.errors.stub(:generate_message, @mock_generator) do
call = [:title, :not_a_number, generate_message_options.merge(value: 'a')]
assert_called_with(@person.errors, :generate_message, call) do
@person.valid?
end
end
@ -216,8 +213,8 @@ 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'
@mock_generator.expect(:call, nil, [:title, :not_an_integer, generate_message_options.merge(value: '0.0')])
@person.errors.stub(:generate_message, @mock_generator) do
call = [:title, :not_an_integer, generate_message_options.merge(value: '0.0')]
assert_called_with(@person.errors, :generate_message, call) do
@person.valid?
end
end
@ -229,8 +226,8 @@ 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
@mock_generator.expect(:call, nil, [:title, :odd, generate_message_options.merge(value: 0)])
@person.errors.stub(:generate_message, @mock_generator) do
call = [:title, :odd, generate_message_options.merge(value: 0)]
assert_called_with(@person.errors, :generate_message, call) do
@person.valid?
end
end
@ -242,8 +239,8 @@ 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
@mock_generator.expect(:call, nil, [:title, :less_than, generate_message_options.merge(value: 1, count: 0)])
@person.errors.stub(:generate_message, @mock_generator) do
call = [:title, :less_than, generate_message_options.merge(value: 1, count: 0)]
assert_called_with(@person.errors, :generate_message, call) do
@person.valid?
end
end

View file

@ -97,7 +97,7 @@ class ValidatesWithTest < ActiveModel::TestCase
test "passes all configuration options to the validator class" do
topic = Topic.new
validator = MiniTest::Mock.new
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])