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 |
2009-09-17 14:34:40 -04:00
mock . post " /people.xml " , { } , %q( <?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 , { 'Content-Type' = > 'application/xml; charset=utf-8' }
mock . post " /people.json " , { } , %q( {"errors":["Age can't be blank","Name can't be blank","Name must start with a letter","Person quota full for today."]} ) , 422 , { 'Content-Type' = > 'application/json; charset=utf-8' }
2006-09-07 20:07:30 -04:00
end
end
2007-09-21 19:31:21 -04:00
2006-09-07 20:07:30 -04:00
def test_should_mark_as_invalid
2009-08-09 09:24:06 -04:00
[ :json , :xml ] . each do | format |
invalid_user_using_format ( format ) do
assert ! @person . valid?
end
end
2006-09-07 20:07:30 -04:00
end
2007-09-21 19:31:21 -04:00
2010-06-26 13:16:17 -04:00
def test_should_parse_json_and_xml_errors
2009-08-09 09:24:06 -04:00
[ :json , :xml ] . each do | format |
invalid_user_using_format ( format ) do
assert_kind_of ActiveResource :: Errors , @person . errors
assert_equal 4 , @person . errors . size
end
end
2006-09-07 20:07:30 -04:00
end
2010-06-26 13:16:17 -04:00
def test_should_parse_json_errors_when_no_errors_key
ActiveResource :: HttpMock . respond_to do | mock |
mock . post " /people.json " , { } , '{}' , 422 , { 'Content-Type' = > 'application/json; charset=utf-8' }
end
invalid_user_using_format ( :json ) do
assert_kind_of ActiveResource :: Errors , @person . errors
assert_equal 0 , @person . errors . size
end
end
2006-09-07 20:07:30 -04:00
def test_should_parse_errors_to_individual_attributes
2009-08-09 09:24:06 -04:00
[ :json , :xml ] . each do | format |
invalid_user_using_format ( format ) do
assert @person . errors [ :name ] . any?
assert_equal [ " can't be blank " ] , @person . errors [ :age ]
assert_equal [ " can't be blank " , " must start with a letter " ] , @person . errors [ :name ]
assert_equal [ " Person quota full for today. " ] , @person . errors [ :base ]
end
end
2006-09-07 20:07:30 -04:00
end
2007-09-21 19:31:21 -04:00
def test_should_iterate_over_errors
2009-08-09 09:24:06 -04:00
[ :json , :xml ] . each do | format |
invalid_user_using_format ( format ) do
errors = [ ]
@person . errors . each { | attribute , message | errors << [ attribute , message ] }
assert errors . include? ( [ :name , " can't be blank " ] )
end
end
2007-09-21 19:31:21 -04:00
end
def test_should_iterate_over_full_errors
2009-08-09 09:24:06 -04:00
[ :json , :xml ] . each do | format |
invalid_user_using_format ( format ) do
errors = [ ]
@person . errors . to_a . each { | message | errors << message }
assert errors . include? ( " Name can't be blank " )
end
end
2007-09-21 19:31:21 -04:00
end
2006-09-07 20:07:30 -04:00
def test_should_format_full_errors
2009-08-09 09:24:06 -04:00
[ :json , :xml ] . each do | format |
invalid_user_using_format ( format ) do
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
end
end
2010-01-27 18:28:32 -05:00
def test_should_mark_as_invalid_when_content_type_is_unavailable_in_response_header
ActiveResource :: HttpMock . respond_to do | mock |
mock . post " /people.xml " , { } , %q( <?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 , { }
mock . post " /people.json " , { } , %q( {"errors":["Age can't be blank","Name can't be blank","Name must start with a letter","Person quota full for today."]} ) , 422 , { }
end
[ :json , :xml ] . each do | format |
invalid_user_using_format ( format ) do
assert ! @person . valid?
end
end
end
2009-08-09 09:24:06 -04:00
private
def invalid_user_using_format ( mime_type_reference )
previous_format = Person . format
Person . format = mime_type_reference
@person = Person . new ( :name = > '' , :age = > '' )
assert_equal false , @person . save
yield
ensure
Person . format = previous_format
2006-09-07 20:07:30 -04:00
end
2007-10-02 01:32:14 -04:00
end