Skip to content

Commit 883f27a

Browse files
Neeraj Singhjosevalim
authored andcommitted
test cases for record.to_xml [#458 state:resolved]
Signed-off-by: José Valim <jose.valim@gmail.com>
1 parent 34908e4 commit 883f27a

File tree

4 files changed

+28
-0
lines changed

4 files changed

+28
-0
lines changed

activemodel/test/cases/serializeration/xml_serialization_test.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,14 @@ def setup
9595
assert_match %r{<awesome type=\"boolean\">false</awesome>}, @contact.to_xml
9696
end
9797

98+
test "should serialize array" do
99+
assert_match %r{<social type=\"array\">\s*<social>twitter</social>\s*<social>github</social>\s*</social>}, @contact.to_xml(:methods => :social)
100+
end
101+
102+
test "should serialize hash" do
103+
assert_match %r{<network>\s*<git type=\"symbol\">github</git>\s*</network>}, @contact.to_xml(:methods => :network)
104+
end
105+
98106
test "should serialize yaml" do
99107
assert_match %r{<preferences type=\"yaml\">--- !ruby/object:OpenStruct \ntable:\s*:name: John\n</preferences>}, @contact.to_xml
100108
end

activemodel/test/models/contact.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,14 @@ class Contact
33

44
attr_accessor :id, :name, :age, :created_at, :awesome, :preferences
55

6+
def social
7+
%w(twitter github)
8+
end
9+
10+
def network
11+
{:git => :github}
12+
end
13+
614
def initialize(options = {})
715
options.each { |name, value| send("#{name}=", value) }
816
end

activerecord/test/cases/xml_serialization_test.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,4 +234,12 @@ def test_should_has_many_array_elements_should_include_type_when_different_from_
234234
assert types.include?('StiPost')
235235
end
236236

237+
def test_should_produce_xml_for_methods_returning_array
238+
xml = authors(:david).to_xml(:methods => :social)
239+
array = Hash.from_xml(xml)['author']['social']
240+
assert_equal 2, array.size
241+
assert array.include? 'twitter'
242+
assert array.include? 'github'
243+
end
244+
237245
end

activerecord/test/models/author.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,10 @@ def label
104104
"#{id}-#{name}"
105105
end
106106

107+
def social
108+
%w(twitter github)
109+
end
110+
107111
private
108112
def log_before_adding(object)
109113
@post_log << "before_adding#{object.id || '<new>'}"

0 commit comments

Comments
 (0)