1 require 'abstract_unit'
2 require "fixtures/person"
4 class BaseErrorsTest
< Test
::Unit::TestCase
6 ActiveResource
::HttpMock.respond_to
do |mock
|
7 mock
.post
"/people.xml", {}, "<?xml version=\"1.0\" encoding=\"UTF-8\"?><errors><error>Age can't be blank</error><error>Name can't be blank</error><error>Name must start with a letter</error><error>Person quota full for today.</error></errors>", 422
9 @person = Person
.new(:name => '', :age => '')
10 assert_equal
@person.save
, false
13 def test_should_mark_as_invalid
14 assert
!@person.valid
?
17 def test_should_parse_xml_errors
18 assert_kind_of ActiveResource
::Errors, @person.errors
19 assert_equal
4, @person.errors
.size
22 def test_should_parse_errors_to_individual_attributes
23 assert
@person.errors
.invalid
?(:name)
24 assert_equal
"can't be blank", @person.errors
.on(:age)
25 assert_equal
["can't be blank", "must start with a letter"], @person.errors
[:name]
26 assert_equal
"Person quota full for today.", @person.errors
.on_base
29 def test_should_iterate_over_errors
31 @person.errors
.each
{ |attribute
, message
| errors
<< [attribute
, message
] }
32 assert errors
.include?(["name", "can't be blank"])
35 def test_should_iterate_over_full_errors
37 @person.errors
.each_full
{ |message
| errors
<< message
}
38 assert errors
.include?("Name can't be blank")
41 def test_should_format_full_errors
42 full
= @person.errors
.full_messages
43 assert full
.include?("Age can't be blank")
44 assert full
.include?("Name can't be blank")
45 assert full
.include?("Name must start with a letter")
46 assert full
.include?("Person quota full for today.")