Upgraded Rails and RSpec
[monkeycharger.git] / vendor / rails / activeresource / test / format_test.rb
blob609030250ef2c520d23d5b6ea3d8c1b0e16dbbdd
1 require "#{File.dirname(__FILE__)}/abstract_unit"
2 require "fixtures/person"
4 class FormatTest < Test::Unit::TestCase
5   def setup
6     @matz  = { :id => 1, :name => 'Matz' }
7     @david = { :id => 2, :name => 'David' }
8     
9     @programmers = [ @matz, @david ]
10   end
11   
12   def test_formats_on_single_element
13     for format in [ :json, :xml ]
14       using_format(Person, format) do
15         ActiveResource::HttpMock.respond_to.get "/people/1.#{format}", {}, ActiveResource::Formats[format].encode(@david)
16         assert_equal @david[:name], Person.find(1).name
17       end
18     end
19   end
21   def test_formats_on_collection
22     for format in [ :json, :xml ]
23       using_format(Person, format) do
24         ActiveResource::HttpMock.respond_to.get "/people.#{format}", {}, ActiveResource::Formats[format].encode(@programmers)
25         remote_programmers = Person.find(:all)
26         assert_equal 2, remote_programmers.size
27         assert remote_programmers.select { |p| p.name == 'David' }
28       end
29     end
30   end
32   
33   private
34     def using_format(klass, mime_type_reference)
35       previous_format = klass.format
36       klass.format = mime_type_reference
38       yield
39     ensure
40       klass.format = previous_format
41     end
42 end