Sha256: 276e439706d79abd4adee318c2d224f24def886b5a6357012aad41e2b9a98415
Contents?: true
Size: 1.12 KB
Versions: 15
Compression:
Stored size: 1.12 KB
Contents
require "test_helper" class DefaultTest < MiniTest::Spec Song = Struct.new(:id, :title) representer! do property :id property :title, default: "Huber Breeze" #->(options) { options[:default] } end describe "#from_hash" do let (:song) { Song.new.extend(representer) } it { song.from_hash({}).must_equal Song.new(nil, "Huber Breeze") } # default doesn't apply when empty string. it { song.from_hash({"title"=>""}).must_equal Song.new(nil, "") } it { song.from_hash({"title"=>nil}).must_equal Song.new(nil, nil) } it { song.from_hash({"title"=>"Blindfold"}).must_equal Song.new(nil, "Blindfold") } end describe "#to_json" do it "uses :default when not available from object" do Song.new.extend(representer).to_hash.must_equal({"title"=>"Huber Breeze"}) end it "uses value from represented object when present" do Song.new(nil, "After The War").extend(representer).to_hash.must_equal({"title"=>"After The War"}) end it "uses value from represented object when emtpy string" do Song.new(nil, "").extend(representer).to_hash.must_equal({"title"=>""}) end end end
Version data entries
15 entries across 11 versions & 1 rubygems