Sha256: 1a66bcedd8c919fa35bb993630557bbb4c66513eedf2929146618780cb3aecf5

Contents?: true

Size: 1.63 KB

Versions: 1

Compression:

Stored size: 1.63 KB

Contents

require File.expand_path(File.dirname(__FILE__) + '/spec_helper')

describe JsonSerialize do
  describe "#json_serialize" do
    context "[getter]" do
      it "should JSON-decode the value" do
        require 'logger'
        object = Json.create!
        Json.update_all(data: '{"foo":"bar"}')
        object.reload.data.should eql('foo' => 'bar')
      end

      it "should return nil if the value is nil" do
        object = Json.create!
        Json.update_all(data: nil)
        object.reload.data.should be_nil
      end
      
      it "should return a default value if the value is nil and a default is set" do
        object = Json.create!
        Json.update_all(default: nil)
        object.reload.default.should eql("default value")
      end
    end

    context "[setter]" do
      it "should JSON-encode the value" do
        object = Json.create!(data: { foo: 'bar' })
        object.send(:read_attribute, :data).should eql({ foo: 'bar' }.to_json)
      end

      it "should leave nil as nil" do
        object = Json.create!(data: nil)
        object.send(:read_attribute, :data).should be_nil
      end
    end
    
    context "[database backing]" do
      it "should back the instance object with the database" do
        object = Json.create!(data: { foo: 'bar' })
        object.data[:foo2] = 'bar2'
        object.save!
        object.data.should eql('foo' => 'bar', 'foo2' => 'bar2')
      end
      
      it "should clear the in-memory reference on reload" do
        object = Json.create!(data: { foo: 'bar' })
        object.data[:foo2] = 'bar2'
        object.reload
        object.data.should eql('foo' => 'bar')
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
json_serialize-2.1.0 spec/json_serialize_spec.rb