Skip to content

Commit 751309e

Browse files
author
jordanbreen28
committed
(CAT-1644) - Fix RSpec/StubbedMock
1 parent b23c031 commit 751309e

File tree

5 files changed

+51
-34
lines changed

5 files changed

+51
-34
lines changed

.rubocop_todo.yml

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ RSpec/InstanceVariable:
2323
- 'spec/acceptance/namevar_spec.rb'
2424
- 'spec/puppet/resource_api/data_type_handling_spec.rb'
2525

26-
# Offense count: 15
26+
# # Offense count: 15
2727
RSpec/LeakyConstantDeclaration:
2828
Exclude:
2929
- 'spec/puppet/resource_api/base_context_spec.rb'
@@ -45,14 +45,6 @@ RSpec/MultipleMemoizedHelpers:
4545
RSpec/NestedGroups:
4646
Max: 7
4747

48-
# Offense count: 25
49-
RSpec/StubbedMock:
50-
Exclude:
51-
- 'spec/puppet/resource_api/property_spec.rb'
52-
- 'spec/puppet/resource_api/transport/wrapper_spec.rb'
53-
- 'spec/puppet/resource_api/transport_spec.rb'
54-
- 'spec/puppet/resource_api_spec.rb'
55-
5648
# Offense count: 47
5749
RSpec/SubjectStub:
5850
Exclude:

spec/puppet/resource_api/property_spec.rb

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,8 @@
113113

114114
context 'when insync? is not defined in the provider' do
115115
it 'raises an error' do
116-
expect(referrable_type_custom_insync).to receive(:my_provider).and_return(test_provider_without_insync)
116+
allow(referrable_type_custom_insync).to receive(:my_provider).and_return(test_provider_without_insync)
117+
expect(referrable_type_custom_insync).to receive(:my_provider)
117118
expect { custom_insync_property.insync?('Foo') }.to raise_error Puppet::DevError, /No insync\? method defined in the provider/
118119
end
119120
end
@@ -159,33 +160,38 @@
159160

160161
context 'when custom insync from the provider returns a boolean for the result' do
161162
it 'returns true if the result was true' do
162-
expect(test_provider_with_insync).to receive(:insync?).and_return(true)
163+
allow(test_provider_with_insync).to receive(:insync?).and_return(true)
164+
expect(test_provider_with_insync).to receive(:insync?)
163165
expect(custom_insync_property.insync?('Foo')).to be true
164166
end
165167

166168
it 'returns false if result was false' do
167-
expect(test_provider_with_insync).to receive(:insync?).and_return(false)
169+
allow(test_provider_with_insync).to receive(:insync?).and_return(false)
170+
expect(test_provider_with_insync).to receive(:insync?)
168171
expect(custom_insync_property.insync?('Foo')).to be false
169172
end
170173
end
171174

172175
context 'when custom insync from the provider returns a string for the result' do
173176
it 'raises an explanatory DevError' do
174-
expect(test_provider_with_insync).to receive(:insync?).and_return('true')
177+
allow(test_provider_with_insync).to receive(:insync?).and_return('true')
178+
expect(test_provider_with_insync).to receive(:insync?)
175179
expect { custom_insync_property.insync?('foo') }.to raise_error(Puppet::DevError, %r{returned a String with a value of "true" instead of true/false})
176180
end
177181
end
178182

179183
context 'when custom insync from the provider returns a symbol for the result' do
180184
it 'raises an explanatory DevError' do
181-
expect(test_provider_with_insync).to receive(:insync?).and_return(:true) # rubocop:disable Lint/BooleanSymbol
185+
allow(test_provider_with_insync).to receive(:insync?).and_return(:true) # rubocop:disable Lint/BooleanSymbol
186+
expect(test_provider_with_insync).to receive(:insync?)
182187
expect { custom_insync_property.insync?('foo') }.to raise_error(Puppet::DevError, %r{returned a Symbol with a value of :true instead of true/false})
183188
end
184189
end
185190

186191
context 'when insync? returned an unexpected result class' do
187192
it 'raises an explanatory DevError' do
188-
expect(test_provider_with_insync).to receive(:insync?).and_return(foo: 1)
193+
allow(test_provider_with_insync).to receive(:insync?).and_return(foo: 1)
194+
expect(test_provider_with_insync).to receive(:insync?)
189195
expect { custom_insync_property.insync?('foo') }.to raise_error(Puppet::DevError, %r{returned a Hash with a value of \{:foo=>1\} instead of true/false})
190196
end
191197
end
@@ -208,7 +214,8 @@
208214

209215
context 'when insync? returns nil for the result' do
210216
it 'relies on Puppet::Property.change_to_s for change reporting' do
211-
expect(test_provider_with_insync).to receive(:insync?).and_return([nil, 'custom change message'])
217+
allow(test_provider_with_insync).to receive(:insync?).and_return([nil, 'custom change message'])
218+
expect(test_provider_with_insync).to receive(:insync?)
212219
expect(custom_insync_property.insync?('Foo')).to be(false)
213220
expect(custom_insync_property.change_to_s('Foo', 'foo')).to match(/changed 'Foo' to 'foo'/)
214221
end
@@ -217,23 +224,26 @@
217224
context 'when insync? returns a change message' do
218225
context 'when the message is empty' do
219226
it 'relies on Puppet::Property.change_to_s for change reporting' do
220-
expect(test_provider_with_insync).to receive(:insync?).and_return([false, ''])
227+
allow(test_provider_with_insync).to receive(:insync?).and_return([false, ''])
228+
expect(test_provider_with_insync).to receive(:insync?)
221229
expect(custom_insync_property.insync?('Foo')).to be(false)
222230
expect(custom_insync_property.change_to_s('Foo', 'foo')).to match(/changed 'Foo' to 'foo'/)
223231
end
224232
end
225233

226234
context 'when the result is nil' do
227235
it 'relies on Puppet::Property.change_to_s for change reporting' do
228-
expect(test_provider_with_insync).to receive(:insync?).and_return(nil)
236+
allow(test_provider_with_insync).to receive(:insync?).and_return(nil)
237+
expect(test_provider_with_insync).to receive(:insync?)
229238
expect(custom_insync_property.insync?('Foo')).to be(false)
230239
expect(custom_insync_property.change_to_s('Foo', 'foo')).to match(/changed 'Foo' to 'foo'/)
231240
end
232241
end
233242

234243
context 'when the result is not nil and the message is not empty' do
235244
it 'passes the message for change_to_s' do
236-
expect(test_provider_with_insync).to receive(:insync?).and_return([false, 'custom change log'])
245+
allow(test_provider_with_insync).to receive(:insync?).and_return([false, 'custom change log'])
246+
expect(test_provider_with_insync).to receive(:insync?)
237247
expect(custom_insync_property.insync?('Foo')).to be(false)
238248
expect(custom_insync_property.change_to_s('Foo', 'foo')).to match(/custom change log/)
239249
end
@@ -251,13 +261,15 @@
251261
end
252262

253263
it 'passes the default message for change reporting if insync? did not return a string' do
254-
expect(test_provider_with_insync).to receive(:insync?).and_return(false)
264+
allow(test_provider_with_insync).to receive(:insync?).and_return(false)
265+
expect(test_provider_with_insync).to receive(:insync?)
255266
custom_insync_property.insync?('Foo')
256267
expect(custom_insync_property.change_to_s(false, true)).to match(/Custom insync logic determined that this resource is out of sync/)
257268
end
258269

259270
it 'passes the string returned by insync? for change reporting' do
260-
expect(test_provider_with_insync).to receive(:insync?).and_return(insync_result)
271+
allow(test_provider_with_insync).to receive(:insync?).and_return(insync_result)
272+
expect(test_provider_with_insync).to receive(:insync?)
261273
custom_insync_property.insync?('Foo')
262274
expect(custom_insync_property.change_to_s(false, true)).to be insync_result[1]
263275
end

spec/puppet/resource_api/transport/wrapper_spec.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ class SomethingSomethingDarkside; end
2222
allow(File).to receive(:exist?).and_return(true)
2323
allow(Hocon).to receive(:load).and_call_original
2424
expect(Puppet::ResourceApi::Transport).to receive(:connect)
25-
expect(Hocon).to receive(:load).with('/etc/credentials', any_args).and_return('foo' => %w[a b], 'bar' => 2)
25+
allow(Hocon).to receive(:load).with('/etc/credentials', any_args).and_return('foo' => %w[a b], 'bar' => 2)
26+
expect(Hocon).to receive(:load).with('/etc/credentials', any_args)
2627
expect { described_class.new('wibble', url) }.not_to raise_error
2728
end
2829
end

spec/puppet/resource_api/transport_spec.rb

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,8 @@ def change_environment(name = nil)
222222
allow(described_class).to receive(:require).with('puppet/resource_api/puppet_context').and_call_original
223223
expect(described_class).to receive(:require).with('puppet/transport/test_target')
224224
expect(described_class).to receive(:validate).with(name, { host: 'example.com' })
225-
expect(Puppet::ResourceApi::PuppetContext).to receive(:new).with(kind_of(Puppet::ResourceApi::TransportSchemaDef)).and_return(context)
225+
allow(Puppet::ResourceApi::PuppetContext).to receive(:new).with(kind_of(Puppet::ResourceApi::TransportSchemaDef)).and_return(context)
226+
expect(Puppet::ResourceApi::PuppetContext).to receive(:new).with(kind_of(Puppet::ResourceApi::TransportSchemaDef))
226227

227228
stub_const('Puppet::Transport::TestTarget', test_target)
228229
expect(test_target).to receive(:new).with(context, { host: 'example.com' })
@@ -250,7 +251,8 @@ class Wibble; end
250251

251252
context 'when puppet has set_device' do
252253
it 'wraps the transport and calls set_device within NetworkDevice' do
253-
expect(Puppet::ResourceApi::Transport::Wrapper).to receive(:new).with(device_name, transport).and_return(wrapper)
254+
allow(Puppet::ResourceApi::Transport::Wrapper).to receive(:new).with(device_name, transport).and_return(wrapper)
255+
expect(Puppet::ResourceApi::Transport::Wrapper).to receive(:new).with(device_name, transport)
254256
allow(Puppet::Util::NetworkDevice).to receive(:respond_to?).with(:set_device).and_return(true)
255257
expect(Puppet::Util::NetworkDevice).to receive(:set_device).with(device_name, wrapper)
256258

@@ -260,8 +262,10 @@ class Wibble; end
260262

261263
context 'when puppet does not have set_device' do
262264
it 'wraps the transport and sets it as current in NetworkDevice' do
263-
expect(Puppet::ResourceApi::Transport::Wrapper).to receive(:new).with(device_name, transport).and_return(wrapper)
264-
expect(Puppet::Util::NetworkDevice).to receive(:respond_to?).with(:set_device).and_return(false)
265+
allow(Puppet::ResourceApi::Transport::Wrapper).to receive(:new).with(device_name, transport).and_return(wrapper)
266+
expect(Puppet::ResourceApi::Transport::Wrapper).to receive(:new).with(device_name, transport)
267+
allow(Puppet::Util::NetworkDevice).to receive(:respond_to?).with(:set_device).and_return(false)
268+
expect(Puppet::Util::NetworkDevice).to receive(:respond_to?).with(:set_device)
265269

266270
described_class.inject_device(device_name, transport)
267271

@@ -299,8 +303,10 @@ class Wibble; end
299303

300304
it 'validates the connection_info' do
301305
expect(described_class).not_to receive(:require).with('puppet/transport/schema/validate')
302-
expect(schema_def).to receive(:check_schema).with({}, kind_of(String)).and_return(nil)
303-
expect(schema_def).to receive(:validate).with({}).and_return(nil)
306+
allow(schema_def).to receive(:check_schema).with({}, kind_of(String)).and_return(nil)
307+
expect(schema_def).to receive(:check_schema).with({}, kind_of(String))
308+
allow(schema_def).to receive(:validate).with({}).and_return(nil)
309+
expect(schema_def).to receive(:validate).with({})
304310

305311
described_class.send :validate, 'validate', {}
306312
end
@@ -309,8 +315,10 @@ class Wibble; end
309315
let(:attributes) { { host: {}, foo: {} } }
310316

311317
it 'cleans the connection_info' do
312-
expect(schema_def).to receive(:check_schema).with({ host: 'host value', foo: 'foo value' }, kind_of(String)).and_return(nil)
313-
expect(schema_def).to receive(:validate).with({ host: 'host value', foo: 'foo value' }).and_return(nil)
318+
allow(schema_def).to receive(:check_schema).with({ host: 'host value', foo: 'foo value' }, kind_of(String)).and_return(nil)
319+
expect(schema_def).to receive(:check_schema).with({ host: 'host value', foo: 'foo value' }, kind_of(String))
320+
allow(schema_def).to receive(:validate).with({ host: 'host value', foo: 'foo value' }).and_return(nil)
321+
expect(schema_def).to receive(:validate).with({ host: 'host value', foo: 'foo value' })
314322

315323
expect(context).to receive(:debug).with('Discarding bolt metaparameter: query')
316324
expect(context).to receive(:debug).with('Discarding bolt metaparameter: remote-transport')
@@ -331,8 +339,10 @@ class Wibble; end
331339
let(:attributes) { { host: { default: 'example.com' }, port: { default: 443 } } }
332340

333341
it 'sets defaults in the connection info' do
334-
expect(schema_def).to receive(:check_schema).with({ host: 'host value', port: 443 }, kind_of(String)).and_return(nil)
335-
expect(schema_def).to receive(:validate).with({ host: 'host value', port: 443 }).and_return(nil)
342+
allow(schema_def).to receive(:check_schema).with({ host: 'host value', port: 443 }, kind_of(String)).and_return(nil)
343+
expect(schema_def).to receive(:check_schema).with({ host: 'host value', port: 443 }, kind_of(String))
344+
allow(schema_def).to receive(:validate).with({ host: 'host value', port: 443 }).and_return(nil)
345+
expect(schema_def).to receive(:validate).with({ host: 'host value', port: 443 })
336346

337347
expect(context).to receive(:debug).with('Using default value for attribute: port, value: 443')
338348
described_class.send :validate, 'validate', host: 'host value'

spec/puppet/resource_api_spec.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2229,13 +2229,15 @@ def set(_context, changes) end
22292229
end
22302230

22312231
it 'passes through the an empty array to `get`' do
2232-
expect(provider).to receive(:get).with(anything, nil).and_return([])
2232+
allow(provider).to receive(:get).with(anything, nil).and_return([])
2233+
expect(provider).to receive(:get).with(anything, nil)
22332234
type.instances
22342235
end
22352236

22362237
it 'passes through the resource title to `get`' do
22372238
instance = type.new(name: 'bar', ensure: 'present')
2238-
expect(provider).to receive(:get).with(anything, ['bar']).and_return([])
2239+
allow(provider).to receive(:get).with(anything, ['bar']).and_return([])
2240+
expect(provider).to receive(:get).with(anything, ['bar'])
22392241
instance.retrieve
22402242
end
22412243
end

0 commit comments

Comments
 (0)