Sha256: 0a9b454e9a3b36c79e1be5b866f541d64019e3b2c4945cd9845468d69f8a00d1

Contents?: true

Size: 1.85 KB

Versions: 5

Compression:

Stored size: 1.85 KB

Contents

require "spec_helper"

describe Mongoid::Associations do

  before do
    Mongoid.database.collection(:people).drop
    Mongoid.database.collection(:games).drop
    Mongoid.database.collection(:posts).drop
  end

  context "one-to-one relational associations" do

    before do
      @person = Person.new(:title => "Sir")
      @game = Game.new(:score => 1)
      @person.game = @game
      @person.save
    end

    it "sets the association on save" do
      @from_db = Person.find(@person.id)
      @from_db.game.should == @game
    end

    it "sets the reverse association" do
      @from_db = Game.find(@game.id)
      @game.person.should == @person
    end

  end

  context "one-to-many relational associations" do

    before do
      @person = Person.new(:title => "Sir")
      @post = Post.new(:title => "Testing")
      @person.posts = [@post]
      @person.save
    end

    it "sets the association on save" do
      @from_db = Person.find(@person.id)
      @from_db.posts.should == [@post]
    end

  end

  context "nested embedded associations" do

    before do
      @person = Person.create(:title => "Mr")
    end

    context "one level nested" do

      before do
        @address = @person.addresses.create(:street => "Oxford St")
        @name = @person.name.create(:first_name => "Gordon")
      end

      it "persists all the associations properly" do
        @name.last_name = "Brown"
        @person.name.last_name.should == "Brown"
      end

    end

    context "multiple levels nested" do

      before do
        @person.phone_numbers.create(:number => "4155551212")
      end

      it "persists all the associations properly" do
        from_db = Person.find(@person.id)
        phone = from_db.phone_numbers.first
        phone.country_code.create(:code => 1)
        from_db.phone_numbers.first.country_code.code.should == 1
      end

    end

  end

end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
mongoid-0.10.1 spec/integration/mongoid/associations_spec.rb
mongoid-0.10.0 spec/integration/mongoid/associations_spec.rb
mongoid-0.9.12 spec/integration/mongoid/associations_spec.rb
mongoid-0.9.11 spec/integration/mongoid/associations_spec.rb
mongoid-0.9.10 spec/integration/mongoid/associations_spec.rb