rspec-fire 1.1.3 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -1,4 +1,4 @@
1
- source :rubygems
1
+ source 'https://rubygems.org'
2
2
 
3
3
  gemspec
4
4
 
data/HISTORY CHANGED
@@ -41,3 +41,8 @@
41
41
 
42
42
  1.1.3 - 3 September 2012
43
43
  * Use __send__ in case classes have overriden it. (#25)
44
+
45
+ 1.2.0 - 2 June 2013
46
+ * Support `expect` syntax.
47
+ * Better naming in the API: `instance_double` and `class_double`. Deprecate
48
+ `fire_double` and `fire_class_double`.
data/README.md CHANGED
@@ -87,7 +87,7 @@ Run your specs:
87
87
  rspec spec/user_spec.rb
88
88
 
89
89
  # Will fail if EmailNotifier#notify method is not defined
90
- rspec -Ilib/email_notifier.rb spec/user_spec.rb
90
+ rspec -Ilib -remail_notifier.rb spec/user_spec.rb
91
91
 
92
92
  Method presence/absence is checked, and if a `with` is provided then so is
93
93
  arity.
@@ -159,9 +159,13 @@ module RSpec
159
159
 
160
160
  protected
161
161
 
162
+ def expect(value)
163
+ ::RSpec::Expectations::ExpectationTarget.new(value)
164
+ end
165
+
162
166
  def ensure_arity(actual)
163
167
  @double.with_doubled_class do |klass|
164
- klass.__send__(@method_finder, @sym).should support_arity(actual)
168
+ expect(klass.__send__(@method_finder, @sym)).to support_arity(actual)
165
169
  end
166
170
  end
167
171
 
@@ -171,7 +175,6 @@ module RSpec
171
175
  end
172
176
 
173
177
  module FireDoublable
174
- extend RSpec::Matchers::DSL
175
178
  include RecursiveConstMethods
176
179
 
177
180
  def should_receive(method_name)
@@ -271,39 +274,48 @@ module RSpec
271
274
  end
272
275
  end
273
276
 
274
- class FireClassDoubleBuilder
275
- def self.build(doubled_class, stubs = {})
276
- Module.new do
277
- extend FireDoublable
277
+ class FireClassDouble < Module
278
+ include FireDoublable
278
279
 
279
- @__doubled_class_name = doubled_class
280
- @__checked_methods = :public_methods
281
- @__method_finder = :method
280
+ def initialize(doubled_class, stubs = {})
281
+ @__doubled_class_name = doubled_class
282
+ @__checked_methods = :public_methods
283
+ @__method_finder = :method
282
284
 
283
- verify_constant_name if RSpec::Fire.configuration.verify_constant_names?
285
+ verify_constant_name if RSpec::Fire.configuration.verify_constant_names?
284
286
 
285
- ::RSpec::Mocks::TestDouble.extend_onto self,
286
- doubled_class, stubs.merge(:__declared_as => "FireClassDouble")
287
+ ::RSpec::Mocks::TestDouble.extend_onto self,
288
+ doubled_class, stubs.merge(:__declared_as => "FireClassDouble")
287
289
 
288
- def self.as_replaced_constant(options = {})
289
- RSpec::Mocks::ConstantStubber.stub(@__doubled_class_name, self, options)
290
- @__original_class = RSpec::Mocks::Constant.original(@__doubled_class_name).original_value
290
+ # This needs to come after `::RSpec::Mocks::TestDouble.extend_onto`
291
+ # so that it gets precedence...
292
+ extend StringRepresentations
293
+ end
291
294
 
292
- extend AsReplacedConstant
293
- self
294
- end
295
+ def as_stubbed_const(options = {})
296
+ RSpec::Mocks::ConstantStubber.stub(@__doubled_class_name, self, options)
297
+ @__original_class = RSpec::Mocks::Constant.original(@__doubled_class_name).original_value
295
298
 
296
- def self.to_s
297
- @__doubled_class_name + " (fire double)"
298
- end
299
+ extend AsReplacedConstant
300
+ self
301
+ end
299
302
 
300
- def self.inspect
301
- to_s
302
- end
303
+ def as_replaced_constant(*args)
304
+ RSpec::Fire::DEPRECATED["as_replaced_constant is deprecated, use as_stubbed_const instead."]
305
+ as_stubbed_const(*args)
306
+ end
303
307
 
304
- def self.name
305
- @__doubled_class_name
306
- end
308
+ def name
309
+ @__doubled_class_name
310
+ end
311
+
312
+ module StringRepresentations
313
+ def to_s
314
+ @__doubled_class_name + " (fire double)"
315
+ end
316
+
317
+ def inspect
318
+ to_s
307
319
  end
308
320
  end
309
321
 
@@ -319,16 +331,32 @@ module RSpec
319
331
  yield const.original_value if const.stubbed?
320
332
  end
321
333
 
322
- def fire_double(*args)
334
+ def instance_double(*args)
323
335
  FireObjectDouble.new(*args)
324
336
  end
325
337
 
338
+ def class_double(*args)
339
+ FireClassDouble.new(*args)
340
+ end
341
+
342
+ def fire_double(*args)
343
+ DEPRECATED["fire_double is deprecated, use instance_double instead."]
344
+ instance_double(*args)
345
+ end
346
+
326
347
  def fire_class_double(*args)
327
- FireClassDoubleBuilder.build(*args)
348
+ DEPRECATED["fire_class_double is deprecated, use class_double instead."]
349
+ class_double(*args)
328
350
  end
329
351
 
330
352
  def fire_replaced_class_double(*args)
331
- fire_class_double(*args).as_replaced_constant
353
+ DEPRECATED["fire_replaced_class_double is deprecated, use class_double with as_stubbed_const instead."]
354
+ class_double(*args).as_stubbed_const
332
355
  end
356
+
357
+ DEPRECATED = lambda do |msg|
358
+ Kernel.warn caller[2] + ": " + msg
359
+ end
360
+
333
361
  end
334
362
  end
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'rspec-fire'
3
- s.version = '1.1.3'
3
+ s.version = '1.2.0'
4
4
  s.summary = 'More resilient test doubles for RSpec.'
5
5
  s.platform = Gem::Platform::RUBY
6
6
  s.authors = ["Xavier Shay"]
@@ -60,7 +60,7 @@ end
60
60
 
61
61
  shared_examples_for 'a fire-enhanced double method' do
62
62
  describe 'doubled class is not loaded' do
63
- let(:doubled_object) { fire_double("UnloadedObject") }
63
+ let(:doubled_object) { instance_double("UnloadedObject") }
64
64
  should_allow(:undefined_method)
65
65
  should_allow('undefined_method')
66
66
  end
@@ -174,7 +174,7 @@ shared_examples_for 'a fire-enhanced double' do
174
174
 
175
175
  context "RSpec's hash shortcut syntax" do
176
176
  context 'doubled class is not loaded' do
177
- let(:doubled_object) { fire_double("UnloadedObject") }
177
+ let(:doubled_object) { instance_double("UnloadedObject") }
178
178
  should_allow(:undefined_method => 123)
179
179
  end
180
180
 
@@ -187,29 +187,47 @@ shared_examples_for 'a fire-enhanced double' do
187
187
  end
188
188
  end
189
189
 
190
- describe '#fire_double' do
191
- let(:doubled_object) { fire_double("TestObject") }
190
+ describe '#instance_double' do
191
+ let(:doubled_object) { instance_double("TestObject") }
192
192
 
193
193
  it_should_behave_like 'a fire-enhanced double'
194
- it_should_behave_like "verifying named constants", :fire_double
194
+ it_should_behave_like "verifying named constants", :instance_double
195
195
 
196
196
  it 'allows stubs to be passed as a hash' do
197
- double = fire_double("TestObject", :defined_method => 17)
197
+ double = instance_double("TestObject", :defined_method => 17)
198
198
  double.defined_method.should eq(17)
199
199
  end
200
200
 
201
201
  it 'does not prevent stubbing methods on an unloaded nested constant with a name that matches a top-level constant' do
202
- double = fire_double("TestObject::Hash")
202
+ double = instance_double("TestObject::Hash")
203
203
  double.stub(:foo).and_return("bar")
204
204
  double.foo.should eq("bar")
205
205
  end
206
+
207
+ def with_isolated_syntax_change
208
+ original_syntax = ::RSpec::Matchers.configuration.syntax
209
+ yield
210
+ ensure
211
+ ::RSpec::Matchers.configuration.syntax = original_syntax
212
+ end
213
+
214
+ it 'works when the `should` syntax is disabled' do
215
+ with_isolated_syntax_change do
216
+ ::RSpec::Matchers.configuration.syntax = :expect
217
+ expect(5).not_to respond_to(:should, :should_not)
218
+
219
+ double = instance_double("TestObject")
220
+ double.should_receive(:defined_method_one_arg).with(8)
221
+ double.defined_method_one_arg(8)
222
+ end
223
+ end
206
224
  end
207
225
 
208
- describe '#fire_class_double' do
209
- let(:doubled_object) { fire_class_double("TestClass") }
226
+ describe '#class_double' do
227
+ let(:doubled_object) { class_double("TestClass") }
210
228
 
211
229
  it_should_behave_like 'a fire-enhanced double'
212
- it_should_behave_like "verifying named constants", :fire_class_double
230
+ it_should_behave_like "verifying named constants", :class_double
213
231
 
214
232
  it 'uses a module for the doubled object so that it supports nested constants like a real class' do
215
233
  doubled_object.should be_a(Module)
@@ -232,7 +250,7 @@ describe '#fire_class_double' do
232
250
  end
233
251
 
234
252
  it 'allows stubs to be specified as a hash' do
235
- double = fire_class_double("SomeClass", :a => 5, :b => 8)
253
+ double = class_double("SomeClass", :a => 5, :b => 8)
236
254
  double.a.should eq(5)
237
255
  double.b.should eq(8)
238
256
  end
@@ -242,7 +260,7 @@ describe '#fire_class_double' do
242
260
  mod.stub(:use)
243
261
  expect { mod.use }.to raise_error(/private method `use' called/)
244
262
 
245
- fire_double = fire_class_double("TestClass")
263
+ fire_double = class_double("TestClass")
246
264
  fire_double.stub(:use)
247
265
  fire_double.use # should not raise an error
248
266
  end if defined?(::RSpec::Mocks::TestDouble)
@@ -252,8 +270,8 @@ def reset_rspec_mocks
252
270
  ::RSpec::Mocks.space.reset_all
253
271
  end
254
272
 
255
- describe '#fire_replaced_class_double (for an existing class)' do
256
- let(:doubled_object) { fire_replaced_class_double("TestClass") }
273
+ describe '.as_stubbed_const (for an existing class)' do
274
+ let(:doubled_object) { class_double("TestClass").as_stubbed_const }
257
275
 
258
276
  it_should_behave_like 'a fire-enhanced double'
259
277
 
@@ -266,40 +284,34 @@ describe '#fire_replaced_class_double (for an existing class)' do
266
284
  end
267
285
 
268
286
  it 'supports transferring nested constants to the double' do
269
- fire_class_double("TestClass").as_replaced_constant(:transfer_nested_constants => true)
287
+ class_double("TestClass").as_stubbed_const(:transfer_nested_constants => true)
270
288
  TestClass::M.should eq(:m)
271
289
  TestClass::N.should eq(:n)
272
290
  end
273
291
 
274
- def use_doubles(class_double, instance_double)
275
- instance_double.should_receive(:defined_method).and_return(3)
276
- class_double.should_receive(:defined_method).and_return(4)
292
+ def use_doubles(class_double_obj, instance_double_obj)
293
+ instance_double_obj.should_receive(:defined_method).and_return(3)
294
+ class_double_obj.should_receive(:defined_method).and_return(4)
277
295
 
278
- instance_double.defined_method.should eq(3)
279
- class_double.defined_method.should eq(4)
296
+ instance_double_obj.defined_method.should eq(3)
297
+ class_double_obj.defined_method.should eq(4)
280
298
 
281
- expect { instance_double.should_receive(:undefined_method) }.to fail_matching("does not implement")
282
- expect { class_double.should_receive(:undefined_method) }.to fail_matching("does not implement")
299
+ expect { instance_double_obj.should_receive(:undefined_method) }.to fail_matching("does not implement")
300
+ expect { class_double_obj.should_receive(:undefined_method) }.to fail_matching("does not implement")
283
301
  end
284
302
 
285
303
  it 'can be used after a declared fire_double for the same class' do
286
- instance_double = fire_double("TestClass")
287
- class_double = fire_replaced_class_double("TestClass")
288
-
289
- use_doubles class_double, instance_double
304
+ use_doubles instance_double("TestClass"), class_double("TestClass")
290
305
  end
291
306
 
292
307
  it 'can be used before a declared fire_double for the same class' do
293
- class_double = fire_replaced_class_double("TestClass")
294
- instance_double = fire_double("TestClass")
295
-
296
- use_doubles class_double, instance_double
308
+ use_doubles class_double("TestClass"), instance_double("TestClass")
297
309
  end
298
310
  end
299
311
 
300
- describe '#fire_replaced_class_double (for a non-existant class)' do
312
+ describe '.as_stubbed_const (for a non-existant class)' do
301
313
  it 'allows any method to be mocked' do
302
- double = fire_replaced_class_double("A::B::C")
314
+ double = class_double("A::B::C").as_stubbed_const
303
315
  double.should_receive(:foo).with("a").and_return(:bar)
304
316
  A::B::C.foo("a").should eq(:bar)
305
317
  end
@@ -313,17 +325,13 @@ describe '#fire_replaced_class_double (for a non-existant class)' do
313
325
  end
314
326
 
315
327
  it 'can be used after a declared fire_double for the same class' do
316
- instance_double = fire_double("A::B::C")
317
- class_double = fire_replaced_class_double("A::B::C")
318
-
319
- use_doubles class_double, instance_double
328
+ use_doubles class_double("A::B::C").as_stubbed_const,
329
+ instance_double("A::B::C")
320
330
  end
321
331
 
322
332
  it 'can be used before a declared fire_double for the same class' do
323
- class_double = fire_replaced_class_double("A::B::C")
324
- instance_double = fire_double("A::B::C")
325
-
326
- use_doubles class_double, instance_double
333
+ use_doubles class_double("A::B::C").as_stubbed_const,
334
+ instance_double("A::B::C")
327
335
  end
328
336
  end
329
337
 
@@ -409,3 +417,25 @@ describe RSpec::Fire::SupportArityMatcher do
409
417
  end
410
418
  end
411
419
  end
420
+
421
+ describe 'deprecation' do
422
+ specify '#fire_double warns user of deprecated usage' do
423
+ Kernel.should_receive(:warn)
424
+ fire_double("TestClass")
425
+ end
426
+
427
+ specify '#fire_class_double warns user of deprecated usage' do
428
+ Kernel.should_receive(:warn)
429
+ fire_class_double("TestClass")
430
+ end
431
+
432
+ specify '#fire_replaced_class_double warns user of deprecated usage' do
433
+ Kernel.should_receive(:warn)
434
+ fire_replaced_class_double("TestClass")
435
+ end
436
+
437
+ specify '#fire_replaced_class_double warns user of deprecated usage' do
438
+ Kernel.should_receive(:warn)
439
+ class_double("TestClass").as_replaced_constant
440
+ end
441
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rspec-fire
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.3
4
+ version: 1.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-09-03 00:00:00.000000000 Z
12
+ date: 2013-06-03 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
@@ -79,7 +79,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
79
79
  version: '0'
80
80
  requirements: []
81
81
  rubyforge_project:
82
- rubygems_version: 1.8.24
82
+ rubygems_version: 1.8.23
83
83
  signing_key:
84
84
  specification_version: 3
85
85
  summary: More resilient test doubles for RSpec.