require 'spec_helper' describe 'Mongoid Relations' do before :all do class Should include Remarkable::Mongoid::Matchers end @should = Should.new end context 'embedding' do before :all do class SingleBook include Mongoid::Document embeds_many :pages, :class_name => "SinglePage" embeds_one :author, :class_name => "SingleAuthor" end class SinglePage include Mongoid::Document embedded_in :book, :clas_name => "SingleBook", :inverse_of => :pages end end describe 'embeds_one' do it 'should be true for a book embedding one author' do matcher = @should.embed_one :author matcher.matches?(SingleBook.new).should be_true end it 'should be false for a book embedding one publisher' do matcher = @should.embed_one :publisher matcher.matches?(SingleBook.new).should be_false end end describe 'embed_many' do it 'should be true for a book embedding many pages' do matcher = @should.embed_many :pages matcher.matches?(SingleBook.new).should be_true end it 'should be false for a book embedding many words' do matcher = @should.embed_many :words matcher.matches?(SingleBook.new).should be_false end end describe 'embedded_in' do it 'should be true for a page embedded in a book' do matcher = @should.be_embedded_in :book matcher.matches?(SinglePage.new).should be_true end it 'should be false for a page embedded in a newspaper' do matcher = @should.be_embedded_in :newspaper matcher.matches?(SinglePage.new).should be_false end end end context 'referencing' do before :all do class SingleOwner include Mongoid::Document references_many :dogs, :class_name => "SingleDog", :inverse_of => :owner references_one :friend, :class_name => "SingleFriend" references_and_referenced_in_many :turtles, :class_name => "SingleTurtle" end class SingleDog include Mongoid::Document referenced_in :owner, :class_name => "SingleOwner", :inverse_of => :dogs end class SingleTurtle include Mongoid::Document end end describe 'reference_one' do it 'should be true for an owner having one friend' do matcher = @should.reference_one :friend matcher.matches?(SingleOwner.new).should be_true end it 'should be false for an owner having one boss' do matcher = @should.reference_one :boss matcher.matches?(SingleOwner.new).should be_false end end describe 'reference_many' do it 'should be true for an owner having many dogs' do matcher = @should.reference_many :dogs matcher.matches?(SingleOwner.new).should be_true end it 'should be false for an owner having many cats' do matcher = @should.reference_many :cats matcher.matches?(SingleOwner.new).should be_false end end describe 'reference_many_and_be_referenced_in' do it 'should be true for an owner having many turtles as an array' do matcher = @should.reference_many_and_be_referenced_in :turtles matcher.matches?(SingleOwner.new).should be_true end it 'should be false for an owner having many dogs as an array' do matcher = @should.reference_many_and_be_referenced_in :dogs matcher.matches?(SingleOwner.new).should be_false end it 'should be false for an owner having many cats as an array' do matcher = @should.reference_many_and_be_referenced_in :cats matcher.matches?(SingleOwner.new).should be_false end end describe 'be_referenced_in' do it 'should be true for a dog belonging to an owner' do matcher = @should.be_referenced_in :owner matcher.matches?(SingleDog.new).should be_true end it 'should be false for a dog belonging to a cat' do matcher = @should.be_referenced_in :stranger matcher.matches?(SingleDog.new).should be_false end end end context 'messages' do before do @relation_matcher = Remarkable::Mongoid::Matchers::RelationMatcher.new(:test, "Some::Test") end describe 'description' do subject { @relation_matcher.description } it { should == "has Test relation :test" } end describe 'failure_message_for_should' do subject { @relation_matcher.failure_message_for_should } it { should == "\nTest relation failure\nExpected: 'test'"} end end end