Sha256: 9fad6033b0d171850376b6cb1a10870ba8c5d8291295715b3bca18c57524897c
Contents?: true
Size: 1.71 KB
Versions: 1
Compression:
Stored size: 1.71 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 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
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
representable-1.2.8 | test/coercion_test.rb |