spec/lib/serialism/collection_spec.rb in serialism-0.0.1 vs spec/lib/serialism/collection_spec.rb in serialism-0.2.0

- old
+ new

@@ -32,11 +32,16 @@ it 'should require serializer to implement class-level attributes' do invalid_serializer = Class.new expect { Serialism::Collection.new([], serializer: invalid_serializer) - }.to raise_error(ArgumentError, 'serializer must implement a class-level :attributes method') + }.to( + raise_error( + ArgumentError, + 'serializer must implement a class-level :attributes method' + ) + ) end it 'should require serializer to implement instance-level render' do invalid_serializer = Class.new do def self.attributes @@ -73,10 +78,16 @@ describe 'attributes' do it 'should return array of attributes' do expect(collection.attributes).to eq serializer.attributes end + + it 'should return empty array if no items exist' do + expect( + Serialism::Collection.new([], serializer: serializer).attributes + ).to eq [] + end end describe 'to_csv' do it 'should generate a csv string' do @@ -89,12 +100,12 @@ expect(collection.to_csv).to eq expected end it 'should encode complex cells as csv strings' do collection.items = [ - serialized.new([1,2,3]), - serialized.new([4,5,6]) + serialized.new([1, 2, 3]), + serialized.new([4, 5, 6]) ] expected = <<-EOF id,computed "1,2,3","computed - [1, 2, 3]" @@ -104,10 +115,40 @@ end end describe 'to_json' do it 'should generate json' do - expect(collection.to_json).to eq '[{"id":0,"computed":"computed - 0"},{"id":1,"computed":"computed - 1"},{"id":2,"computed":"computed - 2"}]' + expect(collection.to_json).to eq( + '[' \ + '{"id":0,"computed":"computed - 0"},' \ + '{"id":1,"computed":"computed - 1"},' \ + '{"id":2,"computed":"computed - 2"}' \ + ']' + ) + end + end + + describe 'to_a' do + it 'should generate an array' do + expected = [[:id, :computed], + [0, 'computed - 0'], + [1, 'computed - 1'], + [2, 'computed - 2'] + ] + expect(collection.to_a).to eq expected + end + + it 'should encode complex cells as csv strings' do + collection.items = [ + serialized.new([1, 2, 3]), + serialized.new([4, 5, 6]) + ] + + expected = [[:id, :computed], + ["1,2,3", "computed - [1, 2, 3]"], + ["4,5,6", "computed - [4, 5, 6]"] + ] + expect(collection.to_a).to eq expected end end end