Skip to content

Instantly share code, notes, and snippets.

@JonRowe
Created February 2, 2011 16:30
Show Gist options
  • Save JonRowe/807933 to your computer and use it in GitHub Desktop.
Save JonRowe/807933 to your computer and use it in GitHub Desktop.
embedded_document_problem_spec.rb
require 'spec_helper'
class Thing
include Mongoid::Document
field :name
embedded_in :person, inverse_of: :things
end
class EMP_Person
include Mongoid::Document
field :name
embeds_many :things
end
describe Person do
let(:thing_1) { Thing.new name: 'one' }
let(:thing_2) { Thing.new name: 'two' }
let(:thing_3) { Thing.new name: 'three' }
let(:thing_4) { Thing.new name: 'four' }
let(:bob) { EMP_Person.create! name: 'bob', things: [thing_1,thing_2] }
subject { bob }
it { should have(2).things }
its(:things) { should include thing_1 }
its(:things) { should include thing_2 }
context "where bob's things change" do
before do
bobalike = EMP_Person.find(bob.id)
bobalike.things = [thing_3,thing_4]
bobalike.safely.save!
bob.reload
end
it { should have(2).things }
its(:things) { should include thing_3 }
its(:things) { should include thing_4 }
end
context "where bob's things change" do
before do
bobalike = EMP_Person.find(bob.id)
bobalike.things = []
bobalike.safely.save!
bob.reload
end
it { should have(0).things }
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment