Sha256: f8d2a09dc5a289772681170773918dce176c0e18866f25c2641cf2d181b81470

Contents?: true

Size: 1.99 KB

Versions: 38

Compression:

Stored size: 1.99 KB

Contents

require 'spec_helper'

module MongoModel
  specs_for(Document, EmbeddedDocument) do
    define_class(:TestModel, described_class) do
      property :name, String
      property :age, Integer
      property :paid, Boolean
      property :prefs, Hash
      property :internal, String, :internal => true
      
      def hello
        "Hi friend!"
      end
    end
    
    let(:instance) do
      TestModel.new(:name => 'Hello World', :age => 25, :paid => true, :prefs => { :foo => 'bar' }, :internal => 'hideme')
    end
    
    it "should include root in json" do
      begin
        TestModel.include_root_in_json = true
        
        json = instance.to_json
        json.should match(/^\{"test_model":\{/)
      ensure
        TestModel.include_root_in_json = false
      end
    end
    
    it "should encode all public attributes" do
      json = instance.to_json
      json.should match(/"name":"Hello World"/)
      json.should match(/"age":25/)
      json.should match(/"paid":true/)
      json.should match(/"prefs":\{"foo":"bar"\}/)
    end
    
    it "should not encode internal attributes" do
      json = instance.to_json
      json.should_not match(/"internal":"hideme"/)
    end
    
    it "should allow attribute filtering with only" do
      json = instance.to_json(:only => [:name, :age])
      json.should match(/"name":"Hello World"/)
      json.should match(/"age":25/)
      json.should_not match(/"paid":true/)
      json.should_not match(/"prefs":\{"foo":"bar"\}/)
    end
    
    it "should allow attribute filtering with except" do
      json = instance.to_json(:except => [:name, :age])
      json.should_not match(/"name":"Hello World"/)
      json.should_not match(/"age":25/)
      json.should match(/"paid":true/)
      json.should match(/"prefs":\{"foo":"bar"\}/)
    end
    
    it "should allow methods to be included" do
      json = instance.to_json(:methods => [:hello, :type])
      json.should match(/"hello":"Hi friend!"/)
      json.should match(/"type":"TestModel"/)
    end
  end
end

Version data entries

38 entries across 38 versions & 1 rubygems

Version Path
mongomodel-0.4.6 spec/mongomodel/concerns/serialization/json_serialization_spec.rb
mongomodel-0.4.5 spec/mongomodel/concerns/serialization/json_serialization_spec.rb
mongomodel-0.4.4 spec/mongomodel/concerns/serialization/json_serialization_spec.rb
mongomodel-0.4.3 spec/mongomodel/concerns/serialization/json_serialization_spec.rb
mongomodel-0.4.2 spec/mongomodel/concerns/serialization/json_serialization_spec.rb
mongomodel-0.4.1 spec/mongomodel/concerns/serialization/json_serialization_spec.rb
mongomodel-0.4.0 spec/mongomodel/concerns/serialization/json_serialization_spec.rb
mongomodel-0.3.6 spec/mongomodel/concerns/serialization/json_serialization_spec.rb
mongomodel-0.3.5 spec/mongomodel/concerns/serialization/json_serialization_spec.rb
mongomodel-0.3.4 spec/mongomodel/concerns/serialization/json_serialization_spec.rb
mongomodel-0.3.3 spec/mongomodel/concerns/serialization/json_serialization_spec.rb
mongomodel-0.3.2 spec/mongomodel/concerns/serialization/json_serialization_spec.rb
mongomodel-0.3.1 spec/mongomodel/concerns/serialization/json_serialization_spec.rb
mongomodel-0.3.0 spec/mongomodel/concerns/serialization/json_serialization_spec.rb
mongomodel-0.2.20 spec/mongomodel/concerns/serialization/json_serialization_spec.rb
mongomodel-0.2.19 spec/mongomodel/concerns/serialization/json_serialization_spec.rb
mongomodel-0.2.18 spec/mongomodel/concerns/serialization/json_serialization_spec.rb
mongomodel-0.2.17 spec/mongomodel/concerns/serialization/json_serialization_spec.rb
mongomodel-0.2.16 spec/mongomodel/concerns/serialization/json_serialization_spec.rb
mongomodel-0.2.15 spec/mongomodel/concerns/serialization/json_serialization_spec.rb