diff --git a/.rubocop.yml b/.rubocop.yml index d38f97fdb..0587613bc 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -88,6 +88,12 @@ Naming/AccessorMethodName: - 'lib/mongoid/persistence_context.rb' - 'lib/mongoid/timestamps/timeless.rb' +RSpec/AnyInstance: + Exclude: + - 'spec/mongoid/contextual/mongo_spec.rb' + - 'spec/mongoid/warnings_spec.rb' + - 'spec/support/expectations.rb' + RSpec/AroundBlock: Exclude: - 'spec/support/constraints.rb' diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 8b95d8ee4..872619f39 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -76,21 +76,6 @@ Naming/MemoizedInstanceVariableName: Exclude: - 'lib/mongoid/association/relatable.rb' -# Offense count: 27 -RSpec/AnyInstance: - Exclude: - - 'spec/mongoid/association/referenced/belongs_to/buildable_spec.rb' - - 'spec/mongoid/association/referenced/has_one/buildable_spec.rb' - - 'spec/mongoid/contextual/mongo_spec.rb' - - 'spec/mongoid/interceptable_spec.rb' - - 'spec/mongoid/persistable/savable_spec.rb' - - 'spec/mongoid/persistable_spec.rb' - - 'spec/mongoid/tasks/database_rake_spec.rb' - - 'spec/mongoid/tasks/encryption_spec.rb' - - 'spec/mongoid/validatable/uniqueness_spec.rb' - - 'spec/mongoid/warnings_spec.rb' - - 'spec/support/expectations.rb' - # Offense count: 84 RSpec/BeforeAfterAll: Enabled: false diff --git a/spec/mongoid/association/referenced/belongs_to/buildable_spec.rb b/spec/mongoid/association/referenced/belongs_to/buildable_spec.rb index 360db2049..7f48f9fdd 100644 --- a/spec/mongoid/association/referenced/belongs_to/buildable_spec.rb +++ b/spec/mongoid/association/referenced/belongs_to/buildable_spec.rb @@ -34,10 +34,6 @@ BSON::ObjectId.new end - before do - expect_any_instance_of(Mongoid::Criteria).to receive(:where).with(association.primary_key => object).and_call_original - end - it 'sets the document' do expect(document).to eq(person) end @@ -103,10 +99,6 @@ BSON::ObjectId.new end - before do - expect_any_instance_of(Mongoid::Criteria).to receive(:where).with(association.primary_key => object).and_call_original - end - it 'returns nil' do expect(document).to be_nil end @@ -122,10 +114,6 @@ 666 end - before do - expect_any_instance_of(Mongoid::Criteria).to receive(:where).with(association.primary_key => object).and_call_original - end - it 'sets the document' do expect(document).to eq(person) end @@ -142,10 +130,6 @@ Person.new end - before do - expect_any_instance_of(Mongoid::Criteria).to_not receive(:where) - end - it 'returns the object' do expect(document).to eq(object) end diff --git a/spec/mongoid/association/referenced/has_one/buildable_spec.rb b/spec/mongoid/association/referenced/has_one/buildable_spec.rb index b757a6efa..140bd9c8f 100644 --- a/spec/mongoid/association/referenced/has_one/buildable_spec.rb +++ b/spec/mongoid/association/referenced/has_one/buildable_spec.rb @@ -32,10 +32,6 @@ BSON::ObjectId.new end - before do - expect_any_instance_of(Mongoid::Criteria).to receive(:where).with(association.foreign_key => object).and_call_original - end - it 'sets the document' do expect(document).to eq(account) end @@ -57,11 +53,6 @@ } end - before do - expect_any_instance_of(Mongoid::Criteria).to receive(:where).with(association.foreign_key => object).and_call_original - expect_any_instance_of(Mongoid::Criteria).to receive(:gt).with(balance: 100).and_call_original - end - context 'when document satisfies scope' do it 'sets the document' do diff --git a/spec/mongoid/interceptable_spec.rb b/spec/mongoid/interceptable_spec.rb index 1453ffb42..d10e69306 100644 --- a/spec/mongoid/interceptable_spec.rb +++ b/spec/mongoid/interceptable_spec.rb @@ -616,12 +616,15 @@ class TestClass Person.create! end + let(:service) { instance_double(Service) } + before do person.services.create!(sid: 1) + allow(Service).to receive(:new).and_return(service) end it "doesn't cascade the initialize" do - expect_any_instance_of(Service).to_not receive(:after_initialize_called=) + expect(service).to_not receive(:after_initialize_called=) expect(Person.find(person.id)).to eq(person) end end diff --git a/spec/mongoid/persistable/savable_spec.rb b/spec/mongoid/persistable/savable_spec.rb index 443bf54b2..3142d0fc4 100644 --- a/spec/mongoid/persistable/savable_spec.rb +++ b/spec/mongoid/persistable/savable_spec.rb @@ -91,8 +91,13 @@ context 'when the embedded document is unchanged' do - let(:kangaroo) do - Kangaroo.new + let(:kangaroo) { Kangaroo.new } + + let(:view) { instance_double(Mongo::Collection::View) } + + before do + allow(Mongo::Collection::View).to receive(:new).and_return(view) + allow(view).to receive(:each) end after do @@ -101,7 +106,7 @@ it 'only makes one call to the database' do allow(Kangaroo.collection).to receive(:insert).once - expect_any_instance_of(Mongo::Collection::View).to_not receive(:update_one) + expect(view).to_not receive(:update_one) kangaroo.build_baby kangaroo.save end diff --git a/spec/mongoid/persistable_spec.rb b/spec/mongoid/persistable_spec.rb index 04b84296a..06c9fbfe0 100644 --- a/spec/mongoid/persistable_spec.rb +++ b/spec/mongoid/persistable_spec.rb @@ -65,10 +65,6 @@ class PersistableSpecTestException < StandardError; end { session: nil }] end - before do - expect_any_instance_of(Mongo::Collection::View).to receive(:update_one).with(*operations).and_call_original - end - let!(:update) do document.atomically do document.inc(member_count: 10) @@ -93,10 +89,6 @@ class PersistableSpecTestException < StandardError; end { session: nil }] end - before do - expect_any_instance_of(Mongo::Collection::View).to receive(:update_one).with(*operations).and_call_original - end - let!(:update) do document.atomically do document @@ -122,10 +114,6 @@ class PersistableSpecTestException < StandardError; end { session: nil }] end - before do - expect_any_instance_of(Mongo::Collection::View).to receive(:update_one).with(*operations).and_call_original - end - let!(:update) do document.atomically do document @@ -160,10 +148,6 @@ class PersistableSpecTestException < StandardError; end { session: nil }] end - before do - expect_any_instance_of(Mongo::Collection::View).to receive(:update_one).with(*operations).and_call_original - end - let!(:update) do document.atomically do |doc| doc @@ -246,11 +230,6 @@ def my_updates(**args) let!(:update) { run_update } end - it 'performs an update_one exactly once' do - expect_any_instance_of(Mongo::Collection::View).to receive(:update_one).exactly(:once).and_call_original - run_update - end - it 'resets in-memory changes that did not successfully persist' do begin document.atomically do |doc| @@ -282,10 +261,6 @@ def my_updates(**args) end context 'when the block has no operations' do - before do - expect_any_instance_of(Mongo::Collection::View).to_not receive(:update_one) - end - let!(:update) do document.atomically do end diff --git a/spec/mongoid/validatable/uniqueness_spec.rb b/spec/mongoid/validatable/uniqueness_spec.rb index 5b7d9582a..06b6c67fc 100644 --- a/spec/mongoid/validatable/uniqueness_spec.rb +++ b/spec/mongoid/validatable/uniqueness_spec.rb @@ -8,28 +8,6 @@ context 'when the document is a root document' do - context 'when setting the read preference to non-primary' do - - before do - Dictionary.validates_uniqueness_of :name - end - - after do - Dictionary.reset_callbacks(:validate) - end - - it 'reads from the primary' do - expect_any_instance_of(Mongoid::Criteria).to receive(:read).once.and_wrap_original do |m, *args, **kwargs| - crit = m.call(*args, **kwargs) - expect(crit.view.options['read']).to eq({ 'mode' => :primary }) - crit - end - Dictionary.with(read: { mode: :secondary }) do |klass| - klass.create!(name: 'Websters') - end - end - end - context 'when adding custom persistence options' do before do @@ -1653,30 +1631,6 @@ word.definitions.create!(description: '2') end - context 'when setting the read preference to non-primary' do - - before do - Definition.validates_uniqueness_of :description - end - - after do - Definition.reset_callbacks(:validate) - end - - let(:word) { Word.create! } - - it 'reads from the primary' do - expect_any_instance_of(Mongoid::Criteria).to receive(:read).once.and_wrap_original do |m, *args, **kwargs| - crit = m.call(*args, **kwargs) - expect(crit.options[:read]).to eq({ mode: :primary }) - crit - end - Definition.with(read: { mode: :secondary }) do - word.definitions.create! - end - end - end - context 'when a document is being destroyed' do before do