Sha256: 5fff9155124709f5eada7fe3b3bb7cbfd5da1871132a16592496631fe33fefa3

Contents?: true

Size: 1.96 KB

Versions: 14

Compression:

Stored size: 1.96 KB

Contents

require File.expand_path(File.join(File.dirname(__FILE__), "/../../../spec_helper.rb"))

describe Mongoid::Associations::BelongsTo do

  describe "#find" do

    before do
      @parent = Name.new(:first_name => "Drexel")
      @document = stub(:parent => @parent)
      @association = Mongoid::Associations::BelongsTo.new(:person, @document)
    end

    context "when finding by id" do

      it "returns the document in the array with that id" do
        name = @association.find(Mongo::ObjectID.new.to_s)
        name.should == @parent
      end

    end

  end

  context "when decorating" do

    before do
      @parent = Name.new(:first_name => "Drexel")
      @document = stub(:parent => @parent)
      @association = Mongoid::Associations::BelongsTo.new(:person, @document)
    end

    context "when getting values" do

      it "delegates to the document" do
        @association.first_name.should == "Drexel"
      end

    end

    context "when setting values" do

      it "delegates to the document" do
        @association.first_name = "Test"
        @association.first_name.should == "Test"
      end

    end

  end

  describe "#update" do

    context "when child is a has one" do

      before do
        @name = Name.new(:first_name => "Test", :last_name => "User")
        @person = Person.new(:title => "Mrs")
        Mongoid::Associations::BelongsTo.update(@person, @name, :person)
      end

      it "updates the parent document" do
        @person.name.should == @name
        @person.attributes[:name].except(:_id).should ==
          { "first_name" => "Test", "last_name" => "User" }
      end

    end

    context "when child is a has many" do

      before do
        @address = Address.new(:street => "Broadway")
        @person = Person.new(:title => "Mrs")
        Mongoid::Associations::BelongsTo.update(@person, @address, :person)
      end

      it "updates the parent document" do
        @person.addresses.first.should == @address
      end

    end

  end

end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
mongoid-0.7.9 spec/unit/mongoid/associations/belongs_to_spec.rb
mongoid-0.7.8 spec/unit/mongoid/associations/belongs_to_spec.rb
mongoid-0.7.7 spec/unit/mongoid/associations/belongs_to_spec.rb
mongoid-0.7.6 spec/unit/mongoid/associations/belongs_to_spec.rb
mongoid-0.7.5 spec/unit/mongoid/associations/belongs_to_spec.rb
mongoid-0.7.4 spec/unit/mongoid/associations/belongs_to_spec.rb
mongoid-0.7.3 spec/unit/mongoid/associations/belongs_to_spec.rb
mongoid-0.7.2 spec/unit/mongoid/associations/belongs_to_spec.rb
mongoid-0.7.1 spec/unit/mongoid/associations/belongs_to_spec.rb
mongoid-0.7.0 spec/unit/mongoid/associations/belongs_to_spec.rb
mongoid-0.6.10 spec/unit/mongoid/associations/belongs_to_spec.rb
mongoid-0.6.9 spec/unit/mongoid/associations/belongs_to_spec.rb
mongoid-0.6.8 spec/unit/mongoid/associations/belongs_to_spec.rb
mongoid-0.6.7 spec/unit/mongoid/associations/belongs_to_spec.rb