aboutsummaryrefslogtreecommitdiffstats
path: root/activeresource/test/base_errors_test.rb
blob: 5125adf21873bd95060f7bfbf6848a8868ee05aa (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
require "#{File.dirname(__FILE__)}/abstract_unit"
require "fixtures/person"

class BaseErrorsTest < Test::Unit::TestCase
  def setup
    ActiveResource::HttpMock.respond_to do |mock|
      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>", 400
    end
    @exception = nil
    @person    = Person.new(:name => '', :age => '')
    @person.save
  rescue ActiveResource::ResourceInvalid
    @exception = $!
  end
  
  def test_should_mark_as_invalid
    assert !@person.valid?
  end
  
  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
    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

  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
end