Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

style(RSpec/AnyInstance): manual #105

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
15 changes: 0 additions & 15 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
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 @@ -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
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
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
Loading