diff --git a/spec/functional/mongoid/relations/referenced/many_to_many_spec.rb b/spec/functional/mongoid/relations/referenced/many_to_many_spec.rb index b06920ca05..091df5ed2f 100644 --- a/spec/functional/mongoid/relations/referenced/many_to_many_spec.rb +++ b/spec/functional/mongoid/relations/referenced/many_to_many_spec.rb @@ -20,6 +20,7 @@ context "when the relations are not polymorphic" do + context "when the inverse relation is not defined" do let(:person) do @@ -46,35 +47,78 @@ context "when the parent is a new record" do let(:person) do - Person.new + Person.new(:ssn => "423-12-0789") end - let(:preference) do - Preference.new - end + context "when the child is new" do - before do - person.preferences.send(method, preference) - end + let(:preference) do + Preference.new + end - it "adds the documents to the relation" do - person.preferences.should == [ preference ] - end + before do + person.preferences.send(method, preference) + end - it "sets the foreign key on the relation" do - person.preference_ids.should == [ preference.id ] - end + it "adds the documents to the relation" do + person.preferences.should == [ preference ] + end - it "sets the foreign key on the inverse relation" do - preference.person_ids.should == [ person.id ] - end + it "sets the foreign key on the relation" do + person.preference_ids.should == [ preference.id ] + end - it "does not save the target" do - preference.should be_new + it "sets the foreign key on the inverse relation" do + preference.person_ids.should == [ person.id ] + end + + it "does not save the target" do + preference.should be_new + end + + it "adds the correct number of documents" do + person.preferences.size.should == 1 + end end - it "adds the correct number of documents" do - person.preferences.size.should == 1 + context "when the child is already persisted" do + + let!(:persisted) do + Preference.create(:name => "testy") + end + + let(:preference) do + Preference.first + end + + before do + person.preferences.send(method, preference) + person.save + end + + it "adds the documents to the relation" do + person.preferences.should == [ preference ] + end + + it "sets the foreign key on the relation" do + person.preference_ids.should == [ preference.id ] + end + + it "sets the foreign key on the inverse relation" do + preference.person_ids.should == [ person.id ] + end + + it "saves the target" do + preference.should be_persisted + end + + it "adds the correct number of documents" do + person.preferences.size.should == 1 + end + + it "persists the link" do + person.reload.preferences.should eq([ preference ]) + end end end