Sha256: a14713dac0d770f000bcd135c29d4e44b83d55c43bdeef840f65b1ac1ce5554a

Contents?: true

Size: 1.79 KB

Versions: 2

Compression:

Stored size: 1.79 KB

Contents

require 'test_helper'
require 'representable/coercion'

class VirtusCoercionTest < MiniTest::Spec
  class Song  # note that we don't define accessors for the properties here.
  end
    
  describe "Coercion with Virtus" do
    describe "on object level" do
      module SongRepresenter
        include Representable::JSON
        include Representable::Coercion
        property :composed_at,  :type => DateTime
        property :track,        :type => Integer
      end
     
      it "coerces properties in #from_json" do
        song = Song.new.extend(SongRepresenter).from_json("{\"composed_at\":\"November 18th, 1983\",\"track\":\"18\"}")
        assert_kind_of DateTime, song.composed_at
        assert_equal 18, song.track
        assert_equal DateTime.parse("Fri, 18 Nov 1983 00:00:00 +0000"), song.composed_at
      end
    end
    
    
    describe "on class level" do
      class ImmigrantSong
        attr_accessor :track
        include Representable::JSON
        include Virtus
        include Representable::Coercion
        
        property :composed_at,  :type => DateTime, :default => "May 12th, 2012"
        property :track,        :type => Integer
      end
      
      it "coerces into the provided type" do
        skip "Virtus is still not correctly treating coercion on class level?"
        
        song = ImmigrantSong.new.from_json("{\"composed_at\":\"November 18th, 1983\",\"track\":\"18\"}")
        assert_equal DateTime.parse("Fri, 18 Nov 1983 00:00:00 +0000"), song.composed_at
        assert_equal 18, song.track
      end
      
      it "respects the :default options" do
        song = ImmigrantSong.new.from_json("{}")
        assert_kind_of DateTime, song.composed_at
        assert_equal DateTime.parse("Mon, 12 May 2012 00:00:00 +0000"), song.composed_at
      end
    end
    
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
representable-1.3.0 test/coercion_test.rb
representable-1.2.9 test/coercion_test.rb