Skip to content

Commit

Permalink
style(RSpec/AnyInstance): manual
Browse files Browse the repository at this point in the history
  • Loading branch information
antonsavitskiy committed May 8, 2023
1 parent 61ab07e commit 547266f
Show file tree
Hide file tree
Showing 10 changed files with 18 additions and 127 deletions.
6 changes: 6 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,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'
Expand Down
15 changes: 0 additions & 15 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -62,21 +62,6 @@ Naming/MemoizedInstanceVariableName:
- 'lib/mongoid/document.rb'
- 'lib/mongoid/traversable.rb'

# Offense count: 25
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
Expand Down
16 changes: 0 additions & 16 deletions spec/mongoid/association/referenced/belongs_to/buildable_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
9 changes: 0 additions & 9 deletions spec/mongoid/association/referenced/has_one/buildable_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
5 changes: 4 additions & 1 deletion spec/mongoid/interceptable_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -601,12 +601,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
Expand Down
11 changes: 8 additions & 3 deletions spec/mongoid/persistable/savable_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
25 changes: 0 additions & 25 deletions spec/mongoid/persistable_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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|
Expand Down Expand Up @@ -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
Expand Down
5 changes: 0 additions & 5 deletions spec/mongoid/tasks/database_rake_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -349,11 +349,6 @@

before do
Mongoid::Config.send(:clients=, config)

expect_any_instance_of(Mongo::ClientEncryption)
.to receive(:create_data_key)
.with('local')
.and_call_original
end

it 'creates the key' do
Expand Down
7 changes: 0 additions & 7 deletions spec/mongoid/tasks/encryption_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,6 @@
end

context 'when all parameters are correct' do
before do
expect_any_instance_of(Mongo::ClientEncryption)
.to receive(:create_data_key)
.with('local')
.and_return(data_key_id)
end

context 'when all parameters are provided' do
it 'creates a data key' do
result = Mongoid::Tasks::Encryption.create_data_key(kms_provider_name: 'local', client_name: :encrypted)
Expand Down
46 changes: 0 additions & 46 deletions spec/mongoid/validatable/uniqueness_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 547266f

Please sign in to comment.