Skip to content
This repository

Adding tests to cover untested methods in errors_tests.rb #8866

Merged
merged 1 commit into from over 1 year ago

4 participants

Attila Domokos Carlos Antonio da Silva Steve Klabnik Rafael Mendonça França
Attila Domokos

I noticed a couple of methods for ActiveModel::Errors were not covered in its unit test.

activemodel/test/cases/errors_test.rb
... ...
@@ -54,6 +54,58 @@ def test_has_key?
54 54
     assert errors.has_key?(:foo), 'errors should have key :foo'
55 55
   end
56 56
 
  57
+  test "should be able to clear the errors" do
  58
+    person = Person.new
  59
+    person.validate!
  60
+    assert_equal 1, person.errors.count
  61
+
  62
+    person.errors.clear
  63
+
  64
+    assert person.errors.empty?
  65
+  end
  66
+
  67
+  test "get returns the error by the provided the key" do
  68
+    errors = ActiveModel::Errors.new(self)
  69
+    errors[:foo] = "omg"
  70
+    assert_equal errors.get(:foo), ["omg"]
1

Invert the arguments.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
activemodel/test/cases/errors_test.rb
... ...
@@ -54,6 +54,58 @@ def test_has_key?
54 54
     assert errors.has_key?(:foo), 'errors should have key :foo'
55 55
   end
56 56
 
  57
+  test "should be able to clear the errors" do
  58
+    person = Person.new
  59
+    person.validate!
  60
+    assert_equal 1, person.errors.count
  61
+
  62
+    person.errors.clear
  63
+
  64
+    assert person.errors.empty?
  65
+  end
  66
+
  67
+  test "get returns the error by the provided the key" do
1

the provided key

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
activemodel/test/cases/errors_test.rb
((5 lines not shown))
  58
+    person = Person.new
  59
+    person.validate!
  60
+    assert_equal 1, person.errors.count
  61
+
  62
+    person.errors.clear
  63
+
  64
+    assert person.errors.empty?
  65
+  end
  66
+
  67
+  test "get returns the error by the provided the key" do
  68
+    errors = ActiveModel::Errors.new(self)
  69
+    errors[:foo] = "omg"
  70
+    assert_equal errors.get(:foo), ["omg"]
  71
+  end
  72
+
  73
+  test "sets the error with the provided the key" do
1

the provided key

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
activemodel/test/cases/errors_test.rb
((8 lines not shown))
  61
+
  62
+    person.errors.clear
  63
+
  64
+    assert person.errors.empty?
  65
+  end
  66
+
  67
+  test "get returns the error by the provided the key" do
  68
+    errors = ActiveModel::Errors.new(self)
  69
+    errors[:foo] = "omg"
  70
+    assert_equal errors.get(:foo), ["omg"]
  71
+  end
  72
+
  73
+  test "sets the error with the provided the key" do
  74
+    errors = ActiveModel::Errors.new(self)
  75
+    errors.set(:foo, "omg")
  76
+    assert_equal ({:foo => "omg"}), errors.messages
1

Put spaces around arguments in the method call, and use 1.9 style hash:

assert_equal({ foo: "omg" }, errors.messages)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
activemodel/test/cases/errors_test.rb
((25 lines not shown))
  78
+
  79
+  test "values returns an array of messages" do
  80
+    errors = ActiveModel::Errors.new(self)
  81
+    errors.set(:foo, "omg")
  82
+    errors.set(:baz, "zomg")
  83
+    assert_equal ["omg", "zomg"], errors.values
  84
+  end
  85
+
  86
+  test "keys returns the error keys" do
  87
+    errors = ActiveModel::Errors.new(self)
  88
+    errors.set(:foo, "omg")
  89
+    errors.set(:baz, "zomg")
  90
+    assert_equal [:foo, :baz], errors.keys
  91
+  end
  92
+
  93
+  test "as_json returns a json formatted representation of the Errors hash" do
1

Errors => errors

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
activemodel/test/cases/errors_test.rb
((29 lines not shown))
  82
+    errors.set(:baz, "zomg")
  83
+    assert_equal ["omg", "zomg"], errors.values
  84
+  end
  85
+
  86
+  test "keys returns the error keys" do
  87
+    errors = ActiveModel::Errors.new(self)
  88
+    errors.set(:foo, "omg")
  89
+    errors.set(:baz, "zomg")
  90
+    assert_equal [:foo, :baz], errors.keys
  91
+  end
  92
+
  93
+  test "as_json returns a json formatted representation of the Errors hash" do
  94
+    person = Person.new
  95
+    person.validate!
  96
+
  97
+    assert_equal ({:name=>["can not be nil"]}), person.errors.as_json
1

Ditto.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
activemodel/test/cases/errors_test.rb
((37 lines not shown))
  90
+    assert_equal [:foo, :baz], errors.keys
  91
+  end
  92
+
  93
+  test "as_json returns a json formatted representation of the Errors hash" do
  94
+    person = Person.new
  95
+    person.validate!
  96
+
  97
+    assert_equal ({:name=>["can not be nil"]}), person.errors.as_json
  98
+  end
  99
+
  100
+  test "as_json full_messages returns a json with the name of the field" do
  101
+    person = Person.new
  102
+    person.validate!
  103
+
  104
+    assert_equal ({:name=>["name can not be nil"]}), \
  105
+                  person.errors.as_json(full_messages: true)
1

Ditto, can be in one line then.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
activemodel/test/cases/errors_test.rb
((40 lines not shown))
  93
+  test "as_json returns a json formatted representation of the Errors hash" do
  94
+    person = Person.new
  95
+    person.validate!
  96
+
  97
+    assert_equal ({:name=>["can not be nil"]}), person.errors.as_json
  98
+  end
  99
+
  100
+  test "as_json full_messages returns a json with the name of the field" do
  101
+    person = Person.new
  102
+    person.validate!
  103
+
  104
+    assert_equal ({:name=>["name can not be nil"]}), \
  105
+                  person.errors.as_json(full_messages: true)
  106
+  end
  107
+
  108
+
1

Remove extra line.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
activemodel/test/cases/errors_test.rb
((32 lines not shown))
  85
+
  86
+  test "keys returns the error keys" do
  87
+    errors = ActiveModel::Errors.new(self)
  88
+    errors.set(:foo, "omg")
  89
+    errors.set(:baz, "zomg")
  90
+    assert_equal [:foo, :baz], errors.keys
  91
+  end
  92
+
  93
+  test "as_json returns a json formatted representation of the Errors hash" do
  94
+    person = Person.new
  95
+    person.validate!
  96
+
  97
+    assert_equal ({:name=>["can not be nil"]}), person.errors.as_json
  98
+  end
  99
+
  100
+  test "as_json full_messages returns a json with the name of the field" do
1

as_json with :full_messages option

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
activemodel/test/cases/errors_test.rb
... ...
@@ -54,6 +54,58 @@ def test_has_key?
54 54
     assert errors.has_key?(:foo), 'errors should have key :foo'
55 55
   end
56 56
 
  57
+  test "should be able to clear the errors" do
  58
+    person = Person.new
  59
+    person.validate!
  60
+    assert_equal 1, person.errors.count
  61
+
  62
+    person.errors.clear
  63
+
  64
+    assert person.errors.empty?
1

I think it might look better with a blank line after person.validate!, and no blank lines between the assertions/clear calls.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Carlos Antonio da Silva

Some minor style comments, thanks!

Steve Klabnik
Collaborator

There's still a merge commit in there.

Attila Domokos

I removed the merge commit, I hope this can be pulled in.

activemodel/test/cases/errors_test.rb
((9 lines not shown))
  62
+    person.errors.clear
  63
+    assert person.errors.empty?
  64
+  end
  65
+
  66
+  test "get returns the error by the provided key" do
  67
+    errors = ActiveModel::Errors.new(self)
  68
+    errors[:foo] = "omg"
  69
+
  70
+    assert_equal ["omg"], errors.get(:foo)
  71
+  end
  72
+
  73
+  test "sets the error with the provided key" do
  74
+    errors = ActiveModel::Errors.new(self)
  75
+    errors.set(:foo, "omg")
  76
+
  77
+    assert_equal ({ foo: "omg" }), errors.messages
1

Just this case missing in a few places:

assert_equal ({ foo: "omg" }), errors.messages
# to
assert_equal({ foo: "omg" }, errors.messages)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Carlos Antonio da Silva

Looks good, just another minor comment and we'll merge. Thanks.

Attila Domokos

@carlosantoniodasilva: Thanks for the comments. I just fixed them. Hope all is good now.

Rafael Mendonça França
Owner

Just remember to squash your commits and update the commit message to something more descriptive.

Attila Domokos

@rafaelfranca: I squashed my commits. What should be my commit message if this does not work?

Rafael Mendonça França
Owner

Now the commit message is good, but we have three commits

Attila Domokos

@rafaelfranca: Sorry, I don't see those commit. Should I close this and create a new pull request with my changes as a single commit? Thanks!

Rafael Mendonça França
Owner

Ahhh, now it is fine, I think was the force push.

@carlosantoniodasilva :shipit:

Attila Domokos

Yeah, I saw it a couple of times. Github's backend job had to catch up.

Carlos Antonio da Silva carlosantoniodasilva merged commit fc6f5a4 into from January 11, 2013
Carlos Antonio da Silva carlosantoniodasilva closed this January 11, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 1 unique commit by 1 author.

Jan 10, 2013
Attila Domokos Covering untested methods for ActiveModel::Errors 7171f9c
This page is out of date. Refresh to see the latest.

Showing 1 changed file with 53 additions and 0 deletions. Show diff stats Hide diff stats

  1. 53  activemodel/test/cases/errors_test.rb
53  activemodel/test/cases/errors_test.rb
@@ -54,6 +54,59 @@ def test_has_key?
54 54
     assert errors.has_key?(:foo), 'errors should have key :foo'
55 55
   end
56 56
 
  57
+  test "should be able to clear the errors" do
  58
+    person = Person.new
  59
+    person.validate!
  60
+
  61
+    assert_equal 1, person.errors.count
  62
+    person.errors.clear
  63
+    assert person.errors.empty?
  64
+  end
  65
+
  66
+  test "get returns the error by the provided key" do
  67
+    errors = ActiveModel::Errors.new(self)
  68
+    errors[:foo] = "omg"
  69
+
  70
+    assert_equal ["omg"], errors.get(:foo)
  71
+  end
  72
+
  73
+  test "sets the error with the provided key" do
  74
+    errors = ActiveModel::Errors.new(self)
  75
+    errors.set(:foo, "omg")
  76
+
  77
+    assert_equal({ foo: "omg" }, errors.messages)
  78
+  end
  79
+
  80
+  test "values returns an array of messages" do
  81
+    errors = ActiveModel::Errors.new(self)
  82
+    errors.set(:foo, "omg")
  83
+    errors.set(:baz, "zomg")
  84
+
  85
+    assert_equal ["omg", "zomg"], errors.values
  86
+  end
  87
+
  88
+  test "keys returns the error keys" do
  89
+    errors = ActiveModel::Errors.new(self)
  90
+    errors.set(:foo, "omg")
  91
+    errors.set(:baz, "zomg")
  92
+
  93
+    assert_equal [:foo, :baz], errors.keys
  94
+  end
  95
+
  96
+  test "as_json returns a json formatted representation of the errors hash" do
  97
+    person = Person.new
  98
+    person.validate!
  99
+
  100
+    assert_equal({ name: ["can not be nil"] }, person.errors.as_json)
  101
+  end
  102
+
  103
+  test "as_json with :full_messages option" do
  104
+    person = Person.new
  105
+    person.validate!
  106
+
  107
+    assert_equal({ name: ["name can not be nil"] }, person.errors.as_json(full_messages: true))
  108
+  end
  109
+
57 110
   test "should return true if no errors" do
58 111
     person = Person.new
59 112
     person.errors[:foo]
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.