diff options
author | David Heinemeier Hansson <david@loudthinking.com> | 2007-09-20 23:18:05 +0000 |
---|---|---|
committer | David Heinemeier Hansson <david@loudthinking.com> | 2007-09-20 23:18:05 +0000 |
commit | dc399b96c84bc66b7c20e92fb40e9ed00daf99c2 (patch) | |
tree | 57edd3b3d5cf446392aaecfebc5a1831e960e4c8 /activeresource/test | |
parent | bd311ce9221f1df9711c791d9abfae89e029924d (diff) | |
download | rails-dc399b96c84bc66b7c20e92fb40e9ed00daf99c2.tar.gz rails-dc399b96c84bc66b7c20e92fb40e9ed00daf99c2.tar.bz2 rails-dc399b96c84bc66b7c20e92fb40e9ed00daf99c2.zip |
Added ActiveResource.format= which defaults to :xml but can also be set to :json [DHH]. Added one-off declarations of mock behavior [DHH]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@7518 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activeresource/test')
-rw-r--r-- | activeresource/test/format_test.rb | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/activeresource/test/format_test.rb b/activeresource/test/format_test.rb new file mode 100644 index 0000000000..609030250e --- /dev/null +++ b/activeresource/test/format_test.rb @@ -0,0 +1,42 @@ +require "#{File.dirname(__FILE__)}/abstract_unit" +require "fixtures/person" + +class FormatTest < Test::Unit::TestCase + def setup + @matz = { :id => 1, :name => 'Matz' } + @david = { :id => 2, :name => 'David' } + + @programmers = [ @matz, @david ] + end + + def test_formats_on_single_element + for format in [ :json, :xml ] + using_format(Person, format) do + ActiveResource::HttpMock.respond_to.get "/people/1.#{format}", {}, ActiveResource::Formats[format].encode(@david) + assert_equal @david[:name], Person.find(1).name + end + end + end + + def test_formats_on_collection + for format in [ :json, :xml ] + using_format(Person, format) do + ActiveResource::HttpMock.respond_to.get "/people.#{format}", {}, ActiveResource::Formats[format].encode(@programmers) + remote_programmers = Person.find(:all) + assert_equal 2, remote_programmers.size + assert remote_programmers.select { |p| p.name == 'David' } + end + end + end + + + private + def using_format(klass, mime_type_reference) + previous_format = klass.format + klass.format = mime_type_reference + + yield + ensure + klass.format = previous_format + end +end
\ No newline at end of file |