Sha256: 53071a206d305c9b7703f1e7a6d5e9067c5518e9299a46a0d9a987c231b7ab35

Contents?: true

Size: 1 KB

Versions: 16

Compression:

Stored size: 1 KB

Contents

# frozen_string_literal: true
# encoding: utf-8

require "spec_helper"

describe Mongoid::Document do

  context 'when projecting with #only' do
    before do
      Person.create!(username: 'Dev', title: 'CEO')
    end

    let(:person) { Person.where(username: 'Dev').only(:_id, :username).first }

    it 'populates specified fields only' do
      expect do
        person.title
      end.to raise_error(ActiveModel::MissingAttributeError)
      # has a default value specified in the model
      expect do
        person.age
      end.to raise_error(ActiveModel::MissingAttributeError)
      expect(person.attributes.keys).to eq(['_id', 'username'])
    end

    it 'allows writing omitted fields' do
      pending 'https://jira.mongodb.org/browse/MONGOID-4701'

      expect do
        person.age
      end.to raise_error(ActiveModel::MissingAttributeError)
      person.age = 42
      expect(person.age).to be 42
      person.save!
      _person = Person.find(person.id)
      expect(_person.age).to be 42
    end
  end
end

Version data entries

16 entries across 16 versions & 2 rubygems

Version Path
tdiary-5.1.6 vendor/bundle/ruby/2.7.0/gems/mongoid-7.1.7/spec/mongoid/document_query_spec.rb
mongoid-7.3.0 spec/mongoid/document_query_spec.rb
mongoid-7.2.3 spec/mongoid/document_query_spec.rb
mongoid-7.1.8 spec/mongoid/document_query_spec.rb
mongoid-7.2.2 spec/mongoid/document_query_spec.rb
mongoid-7.2.1 spec/mongoid/document_query_spec.rb
mongoid-7.1.7 spec/mongoid/document_query_spec.rb
mongoid-7.2.0 spec/mongoid/document_query_spec.rb
mongoid-7.1.6 spec/mongoid/document_query_spec.rb
mongoid-7.1.5 spec/mongoid/document_query_spec.rb
mongoid-7.2.0.rc1 spec/mongoid/document_query_spec.rb
mongoid-7.1.4 spec/mongoid/document_query_spec.rb
mongoid-7.1.2 spec/mongoid/document_query_spec.rb
mongoid-7.1.1 spec/mongoid/document_query_spec.rb
mongoid-7.1.0 spec/mongoid/document_query_spec.rb
mongoid-7.1.0.rc0 spec/mongoid/document_query_spec.rb