2 require 'models/contact'
4 class SerializationTest
< ActiveRecord
::TestCase
5 FORMATS
= [ :xml, :json ]
8 @contact_attributes = {
9 :name => 'aaron stack',
11 :avatar => 'binarydata',
12 :created_at => Time
.utc(2006, 8, 1),
14 :preferences => { :gem => '<strong>ruby</strong>' }
17 @contact = Contact
.new(@contact_attributes)
20 def test_serialize_should_be_reversible
22 @serialized = Contact
.new
.send("to_#{format}")
23 contact
= Contact
.new
.send("from_#{format}", @serialized)
25 assert_equal
@contact_attributes.keys
.collect(&:to_s).sort
, contact
.attributes
.keys
.collect(&:to_s).sort
, "For #{format}"
29 def test_serialize_should_allow_attribute_only_filtering
31 @serialized = Contact
.new(@contact_attributes).send("to_#{format}", :only => [ :age, :name ])
32 contact
= Contact
.new
.send("from_#{format}", @serialized)
33 assert_equal
@contact_attributes[:name], contact
.name
, "For #{format}"
34 assert_nil contact
.avatar
, "For #{format}"
38 def test_serialize_should_allow_attribute_except_filtering
40 @serialized = Contact
.new(@contact_attributes).send("to_#{format}", :except => [ :age, :name ])
41 contact
= Contact
.new
.send("from_#{format}", @serialized)
42 assert_nil contact
.name
, "For #{format}"
43 assert_nil contact
.age
, "For #{format}"
44 assert_equal
@contact_attributes[:awesome], contact
.awesome
, "For #{format}"