spec/mongoid/association/referenced/has_many/eager_spec.rb in mongoid-7.3.5 vs spec/mongoid/association/referenced/has_many/eager_spec.rb in mongoid-7.4.0

- old
+ new

@@ -1,9 +1,9 @@ # frozen_string_literal: true -# encoding: utf-8 require "spec_helper" +require_relative '../has_many_models' describe Mongoid::Association::Referenced::HasMany::Eager do describe ".grouped_docs" do @@ -120,11 +120,11 @@ require_no_multi_shard context "when the eager load has returned documents" do let!(:post) do - person.posts.create(title: "testing") + person.posts.create!(title: "testing") end let!(:eager) do Person.includes(:posts).last end @@ -174,11 +174,11 @@ end end it "returns the proxy" do expect do - eager.posts.create(title: "testing") + eager.posts.create!(title: "testing") end.to_not raise_error end end context "when the eager load has not returned documents for some" do @@ -186,15 +186,15 @@ let!(:person_one) do person end let!(:person_two) do - Person.create(username: "durran") + Person.create!(username: "durran") end let!(:post) do - person_one.posts.create(title: "testing") + person_one.posts.create!(title: "testing") end let!(:eager) do Person.includes(:posts).asc(:username).to_a end @@ -247,15 +247,15 @@ end context "when the relation is polymorphic" do let!(:movie) do - Movie.create(name: "Bladerunner") + Movie.create!(name: "Bladerunner") end let!(:rating) do - movie.ratings.create(value: 10) + movie.ratings.create!(value: 10) end let!(:eager) do Movie.includes(:ratings).first end @@ -266,9 +266,25 @@ it "does not query when touching the association" do expect_query(0) do expect(eager.ratings).to eq([rating]) end + end + end + + context "when the association has scope" do + let!(:trainer1) { HmmTrainer.create!(name: 'Dave') } + let!(:trainer2) { HmmTrainer.create!(name: 'Ash') } + let!(:animal1) { HmmAnimal.create!(taxonomy: 'reptile', trainer: trainer1) } + let!(:animal2) { HmmAnimal.create!(taxonomy: 'bird', trainer: trainer1) } + + let(:eager) do + HmmTrainer.includes(:animals).where(_id: trainer1._id).to_a.first + end + + it 'eager loads the included docs' do + expect(eager.animals._loaded).to eq(animal1._id => animal1) + expect(eager.animals).to eq [animal1] end end end end