Skip to content

Instantly share code, notes, and snippets.

@bernerdschaefer
Created July 23, 2010 16:17
Show Gist options
  • Save bernerdschaefer/487652 to your computer and use it in GitHub Desktop.
Save bernerdschaefer/487652 to your computer and use it in GitHub Desktop.
# ....
describe "#build" do
context "when the parent is new" do
let(:movie) { Movie.new }
let!(:rating) { movie.ratings.build }
it "appends the document to the association" do
movie.ratings.first.should == rating
end
it "sets the reverse association's ids" do
rating.ratable.should == movie
rating.ratable_id.should == movie.id
rating.ratable_type.should == movie.class.name
end
end
context "when the parent is not new" do
let(:avatar) do
Movie.new.tap do |movie|
movie.instance_variable_set(:@new_record, false)
end
end
let!(:rating) { avatar.ratings.build }
it "appends the document to the association" do
avatar.ratings.first.should == rating
end
it "sets the reverse association's ids" do
rating.ratable.should == avatar
rating.ratable_id.should == avatar.id
rating.ratable_type.should == avatar.class.name
end
end
end
# ....
__END__
1) Mongoid::Associations::Options polymorphic association #build when the parent is new sets the reverse association's ids
Failure/Error: rating.ratable.should == movie
Document not found for class Movie with id(s) 4c49c094f7e2da4ad7000020
# ./spec/../lib/mongoid/contexts/ids.rb:19:in `id_criteria'
# ./spec/../lib/mongoid/criteria.rb:31:in `__send__'
# ./spec/../lib/mongoid/criteria.rb:31:in `id_criteria'
# ./spec/../lib/mongoid/criteria.rb:195:in `translate'
# ./spec/../lib/mongoid/finders.rb:67:in `find'
# ./spec/../lib/mongoid/associations/referenced_in.rb:17:in `initialize'
# ./spec/../lib/mongoid/associations/referenced_in.rb:36:in `new'
# ./spec/../lib/mongoid/associations/referenced_in.rb:36:in `instantiate'
# ./spec/../lib/mongoid/associations.rb:261:in `ratable'
# ./spec/../lib/mongoid/memoization.rb:11:in `memoized'
# ./spec/../lib/mongoid/associations.rb:261:in `ratable'
# ./spec/unit/mongoid/associations/polymorphic_spec.rb:22
2) Mongoid::Associations::Options polymorphic association #build when the parent is not new appends the document to the association
Failure/Error: avatar.ratings.first.should == rating
expected: #<Rating _id: 4c49c094f7e2da4ad7000026, ratable_type: "Movie", ratable_id: "4c49c094f7e2da4ad7000025", score: nil>,
got: nil (using ==)
# ./spec/unit/mongoid/associations/polymorphic_spec.rb:37
3) Mongoid::Associations::Options polymorphic association #build when the parent is not new sets the reverse association's ids
Failure/Error: rating.ratable.should == avatar
Document not found for class Movie with id(s) 4c49c094f7e2da4ad700002a
# ./spec/../lib/mongoid/contexts/ids.rb:19:in `id_criteria'
# ./spec/../lib/mongoid/criteria.rb:31:in `__send__'
# ./spec/../lib/mongoid/criteria.rb:31:in `id_criteria'
# ./spec/../lib/mongoid/criteria.rb:195:in `translate'
# ./spec/../lib/mongoid/finders.rb:67:in `find'
# ./spec/../lib/mongoid/associations/referenced_in.rb:17:in `initialize'
# ./spec/../lib/mongoid/associations/referenced_in.rb:36:in `new'
# ./spec/../lib/mongoid/associations/referenced_in.rb:36:in `instantiate'
# ./spec/../lib/mongoid/associations.rb:261:in `ratable'
# ./spec/../lib/mongoid/memoization.rb:11:in `memoized'
# ./spec/../lib/mongoid/associations.rb:261:in `ratable'
# ./spec/unit/mongoid/associations/polymorphic_spec.rb:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment