diff options
Diffstat (limited to 'activemodel/test/cases')
-rw-r--r-- | activemodel/test/cases/attribute_methods_test.rb | 2 | ||||
-rw-r--r-- | activemodel/test/cases/callbacks_test.rb | 2 | ||||
-rw-r--r-- | activemodel/test/cases/errors_test.rb | 103 | ||||
-rw-r--r-- | activemodel/test/cases/naming_test.rb | 2 | ||||
-rw-r--r-- | activemodel/test/cases/secure_password_test.rb | 6 | ||||
-rw-r--r-- | activemodel/test/cases/validations/confirmation_validation_test.rb | 31 | ||||
-rw-r--r-- | activemodel/test/cases/validations/with_validation_test.rb | 2 | ||||
-rw-r--r-- | activemodel/test/cases/validations_test.rb | 8 |
8 files changed, 97 insertions, 59 deletions
diff --git a/activemodel/test/cases/attribute_methods_test.rb b/activemodel/test/cases/attribute_methods_test.rb index baaf842222..d3ec78157c 100644 --- a/activemodel/test/cases/attribute_methods_test.rb +++ b/activemodel/test/cases/attribute_methods_test.rb @@ -194,7 +194,7 @@ class AttributeMethodsTest < ActiveModel::TestCase assert_raises(NoMethodError) { ModelWithAttributes.new.foo } end - test 'acessing a suffixed attribute' do + test 'accessing a suffixed attribute' do m = ModelWithAttributes2.new m.attributes = { 'foo' => 'bar' } diff --git a/activemodel/test/cases/callbacks_test.rb b/activemodel/test/cases/callbacks_test.rb index 086e7266ff..c4c34b0be7 100644 --- a/activemodel/test/cases/callbacks_test.rb +++ b/activemodel/test/cases/callbacks_test.rb @@ -107,7 +107,7 @@ class CallbacksTest < ActiveModel::TestCase test "after_create callbacks with both callbacks declared in one line" do assert_equal ["callback1", "callback2"], Violin1.new.create.history end - test "after_create callbacks with both callbacks declared in differnt lines" do + test "after_create callbacks with both callbacks declared in different lines" do assert_equal ["callback1", "callback2"], Violin2.new.create.history end diff --git a/activemodel/test/cases/errors_test.rb b/activemodel/test/cases/errors_test.rb index 51dcfc37d8..80ff97d03b 100644 --- a/activemodel/test/cases/errors_test.rb +++ b/activemodel/test/cases/errors_test.rb @@ -54,7 +54,7 @@ class ErrorsTest < ActiveModel::TestCase assert errors.has_key?(:foo), 'errors should have key :foo' end - test "should be able to clear the errors" do + test "clear errors" do person = Person.new person.validate! @@ -63,7 +63,7 @@ class ErrorsTest < ActiveModel::TestCase assert person.errors.empty? end - test "get returns the error by the provided key" do + test "get returns the errors for the provided key" do errors = ActiveModel::Errors.new(self) errors[:foo] = "omg" @@ -93,21 +93,7 @@ class ErrorsTest < ActiveModel::TestCase assert_equal [:foo, :baz], errors.keys end - test "as_json returns a json formatted representation of the errors hash" do - person = Person.new - person.validate! - - assert_equal({ name: ["can not be nil"] }, person.errors.as_json) - end - - test "as_json with :full_messages option" do - person = Person.new - person.validate! - - assert_equal({ name: ["name can not be nil"] }, person.errors.as_json(full_messages: true)) - end - - test "should return true if no errors" do + test "detecting whether there are errors with empty?, blank?, include?" do person = Person.new person.errors[:foo] assert person.errors.empty? @@ -115,139 +101,154 @@ class ErrorsTest < ActiveModel::TestCase assert !person.errors.include?(:foo) end - test "method validate! should work" do + test "adding errors using conditionals with Person#validate!" do person = Person.new person.validate! assert_equal ["name can not be nil"], person.errors.full_messages assert_equal ["can not be nil"], person.errors[:name] end - test 'should be able to assign error' do + test "assign error" do person = Person.new person.errors[:name] = 'should not be nil' assert_equal ["should not be nil"], person.errors[:name] end - test 'should be able to add an error on an attribute' do + test "add an error message on a specific attribute" do person = Person.new person.errors.add(:name, "can not be blank") assert_equal ["can not be blank"], person.errors[:name] end - test "should be able to add an error with a symbol" do + test "add an error with a symbol" do person = Person.new person.errors.add(:name, :blank) message = person.errors.generate_message(:name, :blank) assert_equal [message], person.errors[:name] end - test "should be able to add an error with a proc" do + test "add an error with a proc" do person = Person.new message = Proc.new { "can not be blank" } person.errors.add(:name, message) assert_equal ["can not be blank"], person.errors[:name] end - test "added? should be true if that error was added" do + test "added? detects if a specific error was added to the object" do person = Person.new person.errors.add(:name, "can not be blank") assert person.errors.added?(:name, "can not be blank") end - test "added? should handle when message is a symbol" do + test "added? handles symbol message" do person = Person.new person.errors.add(:name, :blank) assert person.errors.added?(:name, :blank) end - test "added? should handle when message is a proc" do + test "added? handles proc messages" do person = Person.new message = Proc.new { "can not be blank" } person.errors.add(:name, message) assert person.errors.added?(:name, message) end - test "added? should default message to :invalid" do + test "added? defaults message to :invalid" do person = Person.new person.errors.add(:name) assert person.errors.added?(:name) end - test "added? should be true when several errors are present, and we ask for one of them" do + test "added? matches the given message when several errors are present for the same attribute" do person = Person.new person.errors.add(:name, "can not be blank") person.errors.add(:name, "is invalid") assert person.errors.added?(:name, "can not be blank") end - test "added? should be false if no errors are present" do + test "added? returns false when no errors are present" do person = Person.new assert !person.errors.added?(:name) end - test "added? should be false when an error is present, but we check for another error" do + test "added? returns false when checking a nonexisting error and other errors are present for the given attribute" do person = Person.new person.errors.add(:name, "is invalid") assert !person.errors.added?(:name, "can not be blank") end - test 'should respond to size' do + test "size calculates the number of error messages" do person = Person.new person.errors.add(:name, "can not be blank") assert_equal 1, person.errors.size end - test 'to_a should return an array' do + test "to_a returns the list of errors with complete messages containing the attribute names" do person = Person.new person.errors.add(:name, "can not be blank") person.errors.add(:name, "can not be nil") assert_equal ["name can not be blank", "name can not be nil"], person.errors.to_a end - test 'to_hash should return a hash' do + test "to_hash returns the error messages hash" do person = Person.new person.errors.add(:name, "can not be blank") - assert_instance_of ::Hash, person.errors.to_hash + assert_equal({ name: ["can not be blank"] }, person.errors.to_hash) end - test 'full_messages should return an array of error messages, with the attribute name included' do + test "full_messages creates a list of error messages with the attribute name included" do person = Person.new person.errors.add(:name, "can not be blank") person.errors.add(:name, "can not be nil") assert_equal ["name can not be blank", "name can not be nil"], person.errors.full_messages end - test 'full_message should return the given message if attribute equals :base' do + test "full_messages_for contains all the error messages for the given attribute" do + person = Person.new + person.errors.add(:name, "can not be blank") + person.errors.add(:name, "can not be nil") + assert_equal ["name can not be blank", "name can not be nil"], person.errors.full_messages_for(:name) + end + + test "full_messages_for does not contain error messages from other attributes" do + person = Person.new + person.errors.add(:name, "can not be blank") + person.errors.add(:email, "can not be blank") + assert_equal ["name can not be blank"], person.errors.full_messages_for(:name) + end + + test "full_messages_for returns an empty list in case there are no errors for the given attribute" do + person = Person.new + person.errors.add(:name, "can not be blank") + assert_equal [], person.errors.full_messages_for(:email) + end + + test "full_message returns the given message when attribute is :base" do person = Person.new assert_equal "press the button", person.errors.full_message(:base, "press the button") end - test 'full_message should return the given message with the attribute name included' do + test "full_message returns the given message with the attribute name included" do person = Person.new assert_equal "name can not be blank", person.errors.full_message(:name, "can not be blank") + assert_equal "name_test can not be blank", person.errors.full_message(:name_test, "can not be blank") end - test 'should return a JSON hash representation of the errors' do + test "as_json creates a json formatted representation of the errors hash" do person = Person.new - person.errors.add(:name, "can not be blank") - person.errors.add(:name, "can not be nil") - person.errors.add(:email, "is invalid") - hash = person.errors.as_json - assert_equal ["can not be blank", "can not be nil"], hash[:name] - assert_equal ["is invalid"], hash[:email] + person.validate! + + assert_equal({ name: ["can not be nil"] }, person.errors.as_json) end - test 'should return a JSON hash representation of the errors with full messages' do + test "as_json with :full_messages option creates a json formatted representation of the errors containing complete messages" do person = Person.new - person.errors.add(:name, "can not be blank") - person.errors.add(:name, "can not be nil") - person.errors.add(:email, "is invalid") - hash = person.errors.as_json(:full_messages => true) - assert_equal ["name can not be blank", "name can not be nil"], hash[:name] - assert_equal ["email is invalid"], hash[:email] + person.validate! + + assert_equal({ name: ["name can not be nil"] }, person.errors.as_json(full_messages: true)) end - test "generate_message should work without i18n_scope" do + test "generate_message works without i18n_scope" do person = Person.new assert !Person.respond_to?(:i18n_scope) assert_nothing_raised { diff --git a/activemodel/test/cases/naming_test.rb b/activemodel/test/cases/naming_test.rb index 38ba3cc152..aa683f4152 100644 --- a/activemodel/test/cases/naming_test.rb +++ b/activemodel/test/cases/naming_test.rb @@ -245,7 +245,7 @@ class NamingHelpersTest < ActiveModel::TestCase end def test_uncountable - assert uncountable?(@uncountable), "Expected 'sheep' to be uncoutable" + assert uncountable?(@uncountable), "Expected 'sheep' to be uncountable" assert !uncountable?(@klass), "Expected 'contact' to be countable" end diff --git a/activemodel/test/cases/secure_password_test.rb b/activemodel/test/cases/secure_password_test.rb index 7783bb25d5..02cd3b8a93 100644 --- a/activemodel/test/cases/secure_password_test.rb +++ b/activemodel/test/cases/secure_password_test.rb @@ -88,4 +88,10 @@ class SecurePasswordTest < ActiveModel::TestCase @user.password = "secret" assert_equal BCrypt::Engine::MIN_COST, @user.password_digest.cost end + + test "blank password_confirmation does not result in a confirmation error" do + @user.password = "" + @user.password_confirmation = "" + assert @user.valid?(:update), "user should be valid" + end end diff --git a/activemodel/test/cases/validations/confirmation_validation_test.rb b/activemodel/test/cases/validations/confirmation_validation_test.rb index f7556a249f..814eec3f59 100644 --- a/activemodel/test/cases/validations/confirmation_validation_test.rb +++ b/activemodel/test/cases/validations/confirmation_validation_test.rb @@ -71,4 +71,35 @@ class ConfirmationValidationTest < ActiveModel::TestCase I18n.backend = @old_backend end + test "does not override confirmation reader if present" do + klass = Class.new do + include ActiveModel::Validations + + def title_confirmation + "expected title" + end + + validates_confirmation_of :title + end + + assert_equal "expected title", klass.new.title_confirmation, + "confirmation validation should not override the reader" + end + + test "does not override confirmation writer if present" do + klass = Class.new do + include ActiveModel::Validations + + def title_confirmation=(value) + @title_confirmation = "expected title" + end + + validates_confirmation_of :title + end + + model = klass.new + model.title_confirmation = "new title" + assert_equal "expected title", model.title_confirmation, + "confirmation validation should not override the writer" + end end diff --git a/activemodel/test/cases/validations/with_validation_test.rb b/activemodel/test/cases/validations/with_validation_test.rb index 457f553661..daa7a8d5c4 100644 --- a/activemodel/test/cases/validations/with_validation_test.rb +++ b/activemodel/test/cases/validations/with_validation_test.rb @@ -50,7 +50,7 @@ class ValidatesWithTest < ActiveModel::TestCase end end - test "vaidation with class that adds errors" do + test "validation with class that adds errors" do Topic.validates_with(ValidatorThatAddsErrors) topic = Topic.new assert topic.invalid?, "A class that adds errors causes the record to be invalid" diff --git a/activemodel/test/cases/validations_test.rb b/activemodel/test/cases/validations_test.rb index a9d32808da..2934e70c2f 100644 --- a/activemodel/test/cases/validations_test.rb +++ b/activemodel/test/cases/validations_test.rb @@ -26,11 +26,11 @@ class ValidationsTest < ActiveModel::TestCase def test_single_field_validation r = Reply.new r.title = "There's no content!" - assert r.invalid?, "A reply without content shouldn't be saveable" + assert r.invalid?, "A reply without content shouldn't be savable" assert r.after_validation_performed, "after_validation callback should be called" r.content = "Messa content!" - assert r.valid?, "A reply with content should be saveable" + assert r.valid?, "A reply with content should be savable" assert r.after_validation_performed, "after_validation callback should be called" end @@ -213,7 +213,7 @@ class ValidationsTest < ActiveModel::TestCase assert_equal 'is too short (minimum is 2 characters)', t.errors[key][0] end - def test_validaton_with_if_and_on + def test_validation_with_if_and_on Topic.validates_presence_of :title, :if => Proc.new{|x| x.author_name = "bad"; true }, :on => :update t = Topic.new(:title => "") @@ -361,7 +361,7 @@ class ValidationsTest < ActiveModel::TestCase def test_dup_validity_is_independent Topic.validates_presence_of :title - topic = Topic.new("title" => "Litterature") + topic = Topic.new("title" => "Literature") topic.valid? duped = topic.dup |