spec/mongoid/association/referenced/has_one_spec.rb in mongoid-7.0.2 vs spec/mongoid/association/referenced/has_one_spec.rb in mongoid-7.0.3

- old
+ new

@@ -1,6 +1,7 @@ require "spec_helper" +require_relative './has_one_models' describe Mongoid::Association::Referenced::HasOne do before do class OwnerObject; include Mongoid::Document; end @@ -924,21 +925,31 @@ end it 'returns the class name option' do expect(association.relation_class_name).to eq('OtherBelongingObject') end + + context 'when the class is namespaced' do + let(:association) do + HomNs::PrefixedParent.relations['child'] + end + + it 'returns unqualified class name as given in the :class_name option' do + expect(association.relation_class_name).to eq('PrefixedChild') + end + end end context 'when the class_name option is not specified' do it 'uses the name of the relation to deduce the class name' do expect(association.relation_class_name).to eq('BelongingObject') end end end - describe '#klass' do + describe '#relation_class' do context 'when the :class_name option is specified' do let!(:_class) do class OtherBelongingObject; end @@ -948,32 +959,68 @@ let(:options) do { class_name: 'OtherBelongingObject' } end it 'returns the class name option' do - expect(association.klass).to eq(_class) + expect(association.relation_class).to eq(_class) end + + context 'when the class is namespaced' do + let(:association) do + HomNs::PrefixedParent.relations['child'] + end + + it 'returns resolved class instance' do + expect(association.relation_class).to eq(HomNs::PrefixedChild) + end + end end context 'when the class_name option is not specified' do it 'uses the name of the relation to deduce the class name' do - expect(association.klass).to eq(BelongingObject) + expect(association.relation_class).to eq(BelongingObject) end end end + describe '#klass' do + it 'is the target class' do + expect(association.klass).to eq(BelongingObject) + end + end + describe '#inverse_class_name' do it 'returns the name of the owner class' do - expect(association.inverse_class_name).to eq(OwnerObject.name) + expect(association.inverse_class_name).to eq('OwnerObject') end + + context 'polymorphic association' do + let(:association) do + has_one_class.has_one :belonging_object, as: :bar + end + + it 'returns the name of the owner class' do + expect(association.inverse_class_name).to eq(OwnerObject.name) + end + end end describe '#inverse_class' do it 'returns the owner class' do expect(association.inverse_class).to be(OwnerObject) + end + + context 'polymorphic association' do + let(:association) do + has_one_class.has_one :belonging_object, as: :bar + end + + it 'returns the owner class' do + expect(association.inverse_class).to be(OwnerObject) + end end end describe '#inverse_of' do