diff options
author | Attila Domokos <adomokos@gmail.com> | 2013-01-10 14:49:47 -0500 |
---|---|---|
committer | Attila Domokos <adomokos@gmail.com> | 2013-01-10 21:41:35 -0500 |
commit | 7171f9cc8ff99731dbfb736f16c3f7dae7058dfe (patch) | |
tree | 7bd7ae205a2dd4432bfb3047b268e21a24e037c0 /activemodel/test | |
parent | 8d926f58d91020f9a8936ad6908939c0e298907b (diff) | |
download | rails-7171f9cc8ff99731dbfb736f16c3f7dae7058dfe.tar.gz rails-7171f9cc8ff99731dbfb736f16c3f7dae7058dfe.tar.bz2 rails-7171f9cc8ff99731dbfb736f16c3f7dae7058dfe.zip |
Covering untested methods for ActiveModel::Errors
Diffstat (limited to 'activemodel/test')
-rw-r--r-- | activemodel/test/cases/errors_test.rb | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/activemodel/test/cases/errors_test.rb b/activemodel/test/cases/errors_test.rb index 1ffce1ae47..cc0c3f16d2 100644 --- a/activemodel/test/cases/errors_test.rb +++ b/activemodel/test/cases/errors_test.rb @@ -54,6 +54,59 @@ class ErrorsTest < ActiveModel::TestCase assert errors.has_key?(:foo), 'errors should have key :foo' end + test "should be able to clear the errors" do + person = Person.new + person.validate! + + assert_equal 1, person.errors.count + person.errors.clear + assert person.errors.empty? + end + + test "get returns the error by the provided key" do + errors = ActiveModel::Errors.new(self) + errors[:foo] = "omg" + + assert_equal ["omg"], errors.get(:foo) + end + + test "sets the error with the provided key" do + errors = ActiveModel::Errors.new(self) + errors.set(:foo, "omg") + + assert_equal({ foo: "omg" }, errors.messages) + end + + test "values returns an array of messages" do + errors = ActiveModel::Errors.new(self) + errors.set(:foo, "omg") + errors.set(:baz, "zomg") + + assert_equal ["omg", "zomg"], errors.values + end + + test "keys returns the error keys" do + errors = ActiveModel::Errors.new(self) + errors.set(:foo, "omg") + errors.set(:baz, "zomg") + + 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 person = Person.new person.errors[:foo] |