Sha256: 60d159dd89d150964e51d09c736726bf962301785642a4f80efec2e0842eb709

Contents?: true

Size: 1.4 KB

Versions: 5

Compression:

Stored size: 1.4 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 
      end
     
      it "coerces properties in #from_json" do
        song = Song.new.extend(SongRepresenter).from_json("{\"composed_at\":\"November 18th, 1983\"}")
        assert_kind_of DateTime, song.composed_at
        assert_equal DateTime.parse("Fri, 18 Nov 1983 00:00:00 +0000"), song.composed_at
      end
    end
    
    
    class ImmigrantSong
      include Representable::JSON
      include Virtus
      include Representable::Coercion
      
      property :composed_at, :type => DateTime, :default => "May 12th, 2012"
    end
    
    it "coerces into the provided type" do
      song = ImmigrantSong.new.from_json("{\"composed_at\":\"November 18th, 1983\"}")
      assert_equal DateTime.parse("Fri, 18 Nov 1983 00:00:00 +0000"), song.composed_at
    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

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
representable-1.2.7 test/coercion_test.rb
representable-1.2.6 test/coercion_test.rb
representable-1.2.5 test/coercion_test.rb
representable-1.2.4 test/coercion_test.rb
representable-1.2.3 test/coercion_test.rb