Sha256: 3669d70d083a97ae96fced79db5639dbbc72262d814053565e363a58877b371a

Contents?: true

Size: 1.8 KB

Versions: 27

Compression:

Stored size: 1.8 KB

Contents

# frozen_string_literal: true
# encoding: utf-8

require "spec_helper"

describe Mongoid::Association::Reflections do

  class TestClass
    include Mongoid::Document
  end

  let(:klass) do
    TestClass
  end

  before do
    klass.relations.clear
  end

  describe ".reflect_on_association" do

    before do
      klass.embeds_many(:addresses)
    end

    context "when the name does not exist" do

      let(:relation) do
        klass.reflect_on_association(:nonexistent)
      end

      it "returns nil" do
        expect(relation).to be_nil
      end
    end
  end

  describe ".reflect_on_all_associations" do

    context "when relations exist for the macros" do

      before do
        klass.embeds_one(:name)
        klass.embeds_many(:addresses)
        klass.has_one(:user)
      end

      context "when passing multiple arguments" do

        let(:relations) do
          klass.reflect_on_all_associations(:embeds_one, :has_one)
        end

        it "returns an array of the relations" do
          expect(relations.size).to eq(2)
        end
      end

      context "when passing a single argument" do

        let(:relations) do
          klass.reflect_on_all_associations(:embeds_one)
        end

        it "returns an array of the relations" do
          expect(relations.size).to eq(1)
        end
      end

      context "when no argument supplied" do

        let(:relations) do
          klass.reflect_on_all_associations
        end

        it "returns an array of all relations" do
          expect(relations.size).to eq(3)
        end
      end
    end

    context "when no relations exist for the macros" do

      let(:relations) do
        klass.reflect_on_all_associations(:embeds_one)
      end

      it "returns an empty array" do
        expect(relations).to be_empty
      end
    end
  end
end

Version data entries

27 entries across 27 versions & 2 rubygems

Version Path
mongoid-7.3.5 spec/mongoid/association/reflections_spec.rb
mongoid-7.3.4 spec/mongoid/association/reflections_spec.rb
mongoid-7.1.11 spec/mongoid/association/reflections_spec.rb
mongoid-7.2.6 spec/mongoid/association/reflections_spec.rb
mongoid-7.3.3 spec/mongoid/association/reflections_spec.rb
mongoid-7.3.2 spec/mongoid/association/reflections_spec.rb
mongoid-7.2.5 spec/mongoid/association/reflections_spec.rb
mongoid-7.1.10 spec/mongoid/association/reflections_spec.rb
mongoid-7.1.9 spec/mongoid/association/reflections_spec.rb
mongoid-7.2.4 spec/mongoid/association/reflections_spec.rb
mongoid-7.3.1 spec/mongoid/association/reflections_spec.rb
tdiary-5.1.6 vendor/bundle/ruby/2.7.0/gems/mongoid-7.1.7/spec/mongoid/association/reflections_spec.rb
mongoid-7.3.0 spec/mongoid/association/reflections_spec.rb
mongoid-7.2.3 spec/mongoid/association/reflections_spec.rb
mongoid-7.1.8 spec/mongoid/association/reflections_spec.rb
mongoid-7.2.2 spec/mongoid/association/reflections_spec.rb
mongoid-7.2.1 spec/mongoid/association/reflections_spec.rb
mongoid-7.1.7 spec/mongoid/association/reflections_spec.rb
mongoid-7.2.0 spec/mongoid/association/reflections_spec.rb
mongoid-7.1.6 spec/mongoid/association/reflections_spec.rb