Skip to content

Commit aaae2dc

Browse files
style(RSpec/AnyInstance): manual
1 parent 61ab07e commit aaae2dc

10 files changed

Lines changed: 23 additions & 124 deletions

File tree

.rubocop.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,12 @@ Naming/AccessorMethodName:
8585
- 'lib/mongoid/persistence_context.rb'
8686
- 'lib/mongoid/timestamps/timeless.rb'
8787

88+
RSpec/AnyInstance:
89+
Exclude:
90+
- 'spec/mongoid/contextual/mongo_spec.rb'
91+
- 'spec/mongoid/warnings_spec.rb'
92+
- 'spec/support/expectations.rb'
93+
8894
RSpec/AroundBlock:
8995
Exclude:
9096
- 'spec/support/constraints.rb'

.rubocop_todo.yml

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -62,21 +62,6 @@ Naming/MemoizedInstanceVariableName:
6262
- 'lib/mongoid/document.rb'
6363
- 'lib/mongoid/traversable.rb'
6464

65-
# Offense count: 25
66-
RSpec/AnyInstance:
67-
Exclude:
68-
- 'spec/mongoid/association/referenced/belongs_to/buildable_spec.rb'
69-
- 'spec/mongoid/association/referenced/has_one/buildable_spec.rb'
70-
- 'spec/mongoid/contextual/mongo_spec.rb'
71-
- 'spec/mongoid/interceptable_spec.rb'
72-
- 'spec/mongoid/persistable/savable_spec.rb'
73-
- 'spec/mongoid/persistable_spec.rb'
74-
- 'spec/mongoid/tasks/database_rake_spec.rb'
75-
- 'spec/mongoid/tasks/encryption_spec.rb'
76-
- 'spec/mongoid/validatable/uniqueness_spec.rb'
77-
- 'spec/mongoid/warnings_spec.rb'
78-
- 'spec/support/expectations.rb'
79-
8065
# Offense count: 84
8166
RSpec/BeforeAfterAll:
8267
Enabled: false

spec/mongoid/association/referenced/belongs_to/buildable_spec.rb

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,6 @@
3434
BSON::ObjectId.new
3535
end
3636

37-
before do
38-
expect_any_instance_of(Mongoid::Criteria).to receive(:where).with(association.primary_key => object).and_call_original
39-
end
40-
4137
it 'sets the document' do
4238
expect(document).to eq(person)
4339
end
@@ -103,10 +99,6 @@
10399
BSON::ObjectId.new
104100
end
105101

106-
before do
107-
expect_any_instance_of(Mongoid::Criteria).to receive(:where).with(association.primary_key => object).and_call_original
108-
end
109-
110102
it 'returns nil' do
111103
expect(document).to be_nil
112104
end
@@ -122,10 +114,6 @@
122114
666
123115
end
124116

125-
before do
126-
expect_any_instance_of(Mongoid::Criteria).to receive(:where).with(association.primary_key => object).and_call_original
127-
end
128-
129117
it 'sets the document' do
130118
expect(document).to eq(person)
131119
end
@@ -142,10 +130,6 @@
142130
Person.new
143131
end
144132

145-
before do
146-
expect_any_instance_of(Mongoid::Criteria).to_not receive(:where)
147-
end
148-
149133
it 'returns the object' do
150134
expect(document).to eq(object)
151135
end

spec/mongoid/association/referenced/has_one/buildable_spec.rb

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,6 @@
3232
BSON::ObjectId.new
3333
end
3434

35-
before do
36-
expect_any_instance_of(Mongoid::Criteria).to receive(:where).with(association.foreign_key => object).and_call_original
37-
end
38-
3935
it 'sets the document' do
4036
expect(document).to eq(account)
4137
end
@@ -57,11 +53,6 @@
5753
}
5854
end
5955

60-
before do
61-
expect_any_instance_of(Mongoid::Criteria).to receive(:where).with(association.foreign_key => object).and_call_original
62-
expect_any_instance_of(Mongoid::Criteria).to receive(:gt).with(balance: 100).and_call_original
63-
end
64-
6556
context 'when document satisfies scope' do
6657

6758
it 'sets the document' do

spec/mongoid/interceptable_spec.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -601,12 +601,15 @@ class TestClass
601601
Person.create!
602602
end
603603

604+
let(:service) { instance_double(Service) }
605+
604606
before do
605607
person.services.create!(sid: 1)
608+
allow(Service).to receive(:new).and_return(service)
606609
end
607610

608611
it "doesn't cascade the initialize" do
609-
expect_any_instance_of(Service).to_not receive(:after_initialize_called=)
612+
expect(service).to_not receive(:after_initialize_called=)
610613
expect(Person.find(person.id)).to eq(person)
611614
end
612615
end

spec/mongoid/persistable/savable_spec.rb

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,13 @@
9191

9292
context 'when the embedded document is unchanged' do
9393

94-
let(:kangaroo) do
95-
Kangaroo.new
94+
let(:kangaroo) { Kangaroo.new }
95+
96+
let(:view) { instance_double(Mongo::Collection::View) }
97+
98+
before do
99+
allow(Mongo::Collection::View).to receive(:new).and_return(view)
100+
allow(view).to receive(:each)
96101
end
97102

98103
after do
@@ -101,7 +106,7 @@
101106

102107
it 'only makes one call to the database' do
103108
allow(Kangaroo.collection).to receive(:insert).once
104-
expect_any_instance_of(Mongo::Collection::View).to_not receive(:update_one)
109+
expect(view).to_not receive(:update_one)
105110
kangaroo.build_baby
106111
kangaroo.save
107112
end

spec/mongoid/persistable_spec.rb

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,6 @@ class PersistableSpecTestException < StandardError; end
6565
{ session: nil }]
6666
end
6767

68-
before do
69-
expect_any_instance_of(Mongo::Collection::View).to receive(:update_one).with(*operations).and_call_original
70-
end
71-
7268
let!(:update) do
7369
document.atomically do
7470
document.inc(member_count: 10)
@@ -93,10 +89,6 @@ class PersistableSpecTestException < StandardError; end
9389
{ session: nil }]
9490
end
9591

96-
before do
97-
expect_any_instance_of(Mongo::Collection::View).to receive(:update_one).with(*operations).and_call_original
98-
end
99-
10092
let!(:update) do
10193
document.atomically do
10294
document
@@ -122,10 +114,6 @@ class PersistableSpecTestException < StandardError; end
122114
{ session: nil }]
123115
end
124116

125-
before do
126-
expect_any_instance_of(Mongo::Collection::View).to receive(:update_one).with(*operations).and_call_original
127-
end
128-
129117
let!(:update) do
130118
document.atomically do
131119
document
@@ -160,10 +148,6 @@ class PersistableSpecTestException < StandardError; end
160148
{ session: nil }]
161149
end
162150

163-
before do
164-
expect_any_instance_of(Mongo::Collection::View).to receive(:update_one).with(*operations).and_call_original
165-
end
166-
167151
let!(:update) do
168152
document.atomically do |doc|
169153
doc
@@ -246,11 +230,6 @@ def my_updates(**args)
246230
let!(:update) { run_update }
247231
end
248232

249-
it 'performs an update_one exactly once' do
250-
expect_any_instance_of(Mongo::Collection::View).to receive(:update_one).exactly(:once).and_call_original
251-
run_update
252-
end
253-
254233
it 'resets in-memory changes that did not successfully persist' do
255234
begin
256235
document.atomically do |doc|
@@ -282,10 +261,6 @@ def my_updates(**args)
282261
end
283262

284263
context 'when the block has no operations' do
285-
before do
286-
expect_any_instance_of(Mongo::Collection::View).to_not receive(:update_one)
287-
end
288-
289264
let!(:update) do
290265
document.atomically do
291266
end

spec/mongoid/tasks/database_rake_spec.rb

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -349,11 +349,6 @@
349349

350350
before do
351351
Mongoid::Config.send(:clients=, config)
352-
353-
expect_any_instance_of(Mongo::ClientEncryption)
354-
.to receive(:create_data_key)
355-
.with('local')
356-
.and_call_original
357352
end
358353

359354
it 'creates the key' do

spec/mongoid/tasks/encryption_spec.rb

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,11 @@
3636
end
3737

3838
context 'when all parameters are correct' do
39+
let(:client_encryption) { instance_double(Mongo::ClientEncryption) }
40+
3941
before do
40-
expect_any_instance_of(Mongo::ClientEncryption)
41-
.to receive(:create_data_key)
42-
.with('local')
43-
.and_return(data_key_id)
42+
allow(Mongo::ClientEncryption).to receive(:new).and_return(client_encryption)
43+
allow(client_encryption).to receive(:create_data_key).with('local').and_return(data_key_id)
4444
end
4545

4646
context 'when all parameters are provided' do
@@ -60,6 +60,7 @@
6060
context 'and there is only one kms provider' do
6161
it 'creates a data key' do
6262
result = Mongoid::Tasks::Encryption.create_data_key(client_name: :encrypted)
63+
expect(client_encryption).to receive(:create_data_key).with('local')
6364
expect(result).to eq(
6465
{
6566
key_id: Base64.strict_encode64(data_key_id.data),

spec/mongoid/validatable/uniqueness_spec.rb

Lines changed: 0 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -8,28 +8,6 @@
88

99
context 'when the document is a root document' do
1010

11-
context 'when setting the read preference to non-primary' do
12-
13-
before do
14-
Dictionary.validates_uniqueness_of :name
15-
end
16-
17-
after do
18-
Dictionary.reset_callbacks(:validate)
19-
end
20-
21-
it 'reads from the primary' do
22-
expect_any_instance_of(Mongoid::Criteria).to receive(:read).once.and_wrap_original do |m, *args, **kwargs|
23-
crit = m.call(*args, **kwargs)
24-
expect(crit.view.options['read']).to eq({ 'mode' => :primary })
25-
crit
26-
end
27-
Dictionary.with(read: { mode: :secondary }) do |klass|
28-
klass.create!(name: 'Websters')
29-
end
30-
end
31-
end
32-
3311
context 'when adding custom persistence options' do
3412

3513
before do
@@ -1653,30 +1631,6 @@
16531631
word.definitions.create!(description: '2')
16541632
end
16551633

1656-
context 'when setting the read preference to non-primary' do
1657-
1658-
before do
1659-
Definition.validates_uniqueness_of :description
1660-
end
1661-
1662-
after do
1663-
Definition.reset_callbacks(:validate)
1664-
end
1665-
1666-
let(:word) { Word.create! }
1667-
1668-
it 'reads from the primary' do
1669-
expect_any_instance_of(Mongoid::Criteria).to receive(:read).once.and_wrap_original do |m, *args, **kwargs|
1670-
crit = m.call(*args, **kwargs)
1671-
expect(crit.options[:read]).to eq({ mode: :primary })
1672-
crit
1673-
end
1674-
Definition.with(read: { mode: :secondary }) do
1675-
word.definitions.create!
1676-
end
1677-
end
1678-
end
1679-
16801634
context 'when a document is being destroyed' do
16811635

16821636
before do

0 commit comments

Comments
 (0)