2008-01-05 08:32:38 -05:00
require 'abstract_unit'
2006-09-07 20:07:30 -04:00
require " fixtures/person "
class BaseErrorsTest < Test :: Unit :: TestCase
def setup
ActiveResource :: HttpMock . respond_to do | mock |
2007-01-16 19:46:32 -05:00
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
2006-09-07 20:07:30 -04:00
end
2006-10-02 18:14:15 -04:00
@person = Person . new ( :name = > '' , :age = > '' )
assert_equal @person . save , false
2006-09-07 20:07:30 -04:00
end
2007-09-21 19:31:21 -04:00
2006-09-07 20:07:30 -04:00
def test_should_mark_as_invalid
assert ! @person . valid?
end
2007-09-21 19:31:21 -04:00
2006-09-07 20:07:30 -04:00
def test_should_parse_xml_errors
assert_kind_of ActiveResource :: Errors , @person . errors
assert_equal 4 , @person . errors . size
end
def test_should_parse_errors_to_individual_attributes
2007-09-21 19:31:21 -04:00
assert @person . errors . invalid? ( :name )
2006-09-07 20:07:30 -04:00
assert_equal " can't be blank " , @person . errors . on ( :age )
assert_equal [ " can't be blank " , " must start with a letter " ] , @person . errors [ :name ]
assert_equal " Person quota full for today. " , @person . errors . on_base
end
2007-09-21 19:31:21 -04:00
def test_should_iterate_over_errors
errors = [ ]
@person . errors . each { | attribute , message | errors << [ attribute , message ] }
2007-10-02 01:32:14 -04:00
assert errors . include? ( [ " name " , " can't be blank " ] )
2007-09-21 19:31:21 -04:00
end
def test_should_iterate_over_full_errors
errors = [ ]
@person . errors . each_full { | message | errors << message }
2007-10-02 01:32:14 -04:00
assert errors . include? ( " Name can't be blank " )
2007-09-21 19:31:21 -04:00
end
2006-09-07 20:07:30 -04:00
def test_should_format_full_errors
full = @person . errors . full_messages
assert full . include? ( " Age can't be blank " )
assert full . include? ( " Name can't be blank " )
assert full . include? ( " Name must start with a letter " )
assert full . include? ( " Person quota full for today. " )
end
2007-10-02 01:32:14 -04:00
end