require 'spec_helper' describe Encore::Entity::Input::Associations do describe :ClassMethods do describe :has_one do before do spawn_model(:User) { belongs_to :organization } spawn_model(:Organization) { has_many :users } spawn_entity :UserEntity do expose :name expose_one :organization end run_migration do create_table(:users, force: true) do |t| t.string :name t.integer :organization_id t.timestamps end create_table(:organizations, force: true) end end let(:organization) { Organization.create } let(:other_organization) { Organization.create } let(:user) { organization.users.create(name: 'Art Vandelay') } let(:entity) { UserEntity.new(user) } context 'when assigning attributes' do before { entity.assign_attributes organization: other_organization.id } it { expect(entity.object.organization_id).to eql organization.id } it { expect(entity.encore_tmp_organization_id).to eql other_organization.id } end context 'when saving the entity' do before do entity.assign_attributes organization: other_organization.id entity.save end it { expect(entity.object.organization_id).to eql other_organization.id } it { expect(entity.encore_tmp_organization_id).to be_nil } end end end end