rspec-fire 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/HISTORY CHANGED
@@ -28,3 +28,6 @@
28
28
  1.0.0 - 9 July 2012
29
29
  * RSpec 2.11 compatibility, no longer compatible with earlier versions.
30
30
  * Removed `stub_const`, it is now in `rspec-mocks`.
31
+
32
+ 1.1.0 - 10 July 2012
33
+ * Add `verify_constant_names` option.
data/README.md CHANGED
@@ -173,6 +173,22 @@ Particularly handy for `ActiveRecord` finders. Use `fire_class_double`. If you
173
173
  dig into the code, you'll find you can create subclasses of `FireDouble` to
174
174
  check for *any* set of methods.
175
175
 
176
+ ### Preventing Typo'd Constant Names
177
+
178
+ `fire_double("MyClas")` will not verify any mocked methods, even when
179
+ `MyClass` is loaded, because of the typo in the constant name. There's
180
+ an option to help prevent these sorts of fat-finger errors:
181
+
182
+ RSpec::Fire.configure do |config|
183
+ config.verify_constant_names = true
184
+ end
185
+
186
+ When this is set to true, rspec-fire will raise an error when given
187
+ the name of an undefined constant. You probably only want to set this
188
+ when running your entire test suite, with all production code loaded.
189
+ Setting this for an isolated unit test will prevent you from being
190
+ able to isolate it!
191
+
176
192
  ### Mocking Done Right (tm)
177
193
 
178
194
  * Only mock methods on collaborators, _not_ the class under test.
data/lib/rspec/fire.rb CHANGED
@@ -4,6 +4,26 @@ require 'delegate'
4
4
 
5
5
  module RSpec
6
6
  module Fire
7
+ class Configuration
8
+ attr_accessor :verify_constant_names
9
+ alias verify_constant_names? verify_constant_names
10
+
11
+ def initialize
12
+ self.verify_constant_names = false
13
+ end
14
+ end
15
+
16
+ def self.configuration
17
+ @configuration ||= Configuration.new
18
+ end
19
+
20
+ def self.configure
21
+ yield configuration
22
+ end
23
+
24
+ Error = Class.new(StandardError)
25
+ UndefinedConstantError = Class.new(Error)
26
+
7
27
  class SupportArityMatcher
8
28
  def initialize(arity)
9
29
  @arity = arity
@@ -179,11 +199,18 @@ module RSpec
179
199
  methods.sort.map {|x|
180
200
  " #{x}"
181
201
  }.join("\n")
202
+
182
203
  ]
183
204
  raise RSpec::Expectations::ExpectationNotMetError, msg
184
205
  end
185
206
  end
186
207
  end
208
+
209
+ def verify_constant_name
210
+ return if recursive_const_defined?(@__doubled_class_name)
211
+
212
+ raise UndefinedConstantError, "#{@__doubled_class_name} is not a defined constant."
213
+ end
187
214
  end
188
215
 
189
216
  class FireObjectDouble < RSpec::Mocks::Mock
@@ -193,6 +220,7 @@ module RSpec
193
220
  args << {} unless Hash === args.last
194
221
 
195
222
  @__doubled_class_name = doubled_class
223
+ verify_constant_name if RSpec::Fire.configuration.verify_constant_names?
196
224
 
197
225
  # __declared_as copied from rspec/mocks definition of `double`
198
226
  args.last[:__declared_as] = 'FireDouble'
@@ -213,6 +241,8 @@ module RSpec
213
241
  @__checked_methods = :public_methods
214
242
  @__method_finder = :method
215
243
 
244
+ verify_constant_name if RSpec::Fire.configuration.verify_constant_names?
245
+
216
246
  # TestDouble was added after rspec 2.9.0, and allows proper mocking
217
247
  # of public methods that have clashing private methods. See spec for
218
248
  # details.
data/rspec-fire.gemspec CHANGED
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'rspec-fire'
3
- s.version = '1.0.0'
3
+ s.version = '1.1.0'
4
4
  s.summary = 'More resilient test doubles for RSpec.'
5
5
  s.platform = Gem::Platform::RUBY
6
6
  s.authors = ["Xavier Shay"]
@@ -29,6 +29,31 @@ class TestClass
29
29
  end
30
30
  end
31
31
 
32
+ shared_examples_for "verifying named constants" do |double_method|
33
+ def clear_config
34
+ RSpec::Fire.instance_variable_set(:@configuration, nil)
35
+ end
36
+
37
+ before(:each) { clear_config }
38
+ after(:all) { clear_config }
39
+
40
+ it "allows mispelled constants by default" do
41
+ double = send(double_method, "TestClas")
42
+ double.should_receive(:undefined_method)
43
+ double.undefined_method
44
+ end
45
+
46
+ it "raises an error when constants are mispelled and the appropriate config option is set" do
47
+ RSpec::Fire.configure do |c|
48
+ c.verify_constant_names = true
49
+ end
50
+
51
+ expect {
52
+ send(double_method, "TestClas")
53
+ }.to raise_error(/TestClas is not a defined constant/)
54
+ end
55
+ end
56
+
32
57
  shared_examples_for 'a fire-enhanced double method' do
33
58
  describe 'doubled class is not loaded' do
34
59
  let(:doubled_object) { fire_double("UnloadedObject") }
@@ -159,6 +184,7 @@ describe '#fire_double' do
159
184
  let(:doubled_object) { fire_double("TestObject") }
160
185
 
161
186
  it_should_behave_like 'a fire-enhanced double'
187
+ it_should_behave_like "verifying named constants", :fire_double
162
188
 
163
189
  it 'allows stubs to be passed as a hash' do
164
190
  double = fire_double("TestObject", :defined_method => 17)
@@ -170,6 +196,7 @@ describe '#fire_class_double' do
170
196
  let(:doubled_object) { fire_class_double("TestClass") }
171
197
 
172
198
  it_should_behave_like 'a fire-enhanced double'
199
+ it_should_behave_like "verifying named constants", :fire_class_double
173
200
 
174
201
  it 'uses a module for the doubled object so that it supports nested constants like a real class' do
175
202
  doubled_object.should be_a(Module)
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.0.0
4
+ version: 1.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-07-09 00:00:00.000000000 Z
12
+ date: 2012-07-11 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
16
- requirement: &2156163500 !ruby/object:Gem::Requirement
16
+ requirement: &2152390460 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '2.11'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *2156163500
24
+ version_requirements: *2152390460
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rake
27
- requirement: &2156162900 !ruby/object:Gem::Requirement
27
+ requirement: &2152389940 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,7 +32,7 @@ dependencies:
32
32
  version: '0'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *2156162900
35
+ version_requirements: *2152389940
36
36
  description:
37
37
  email:
38
38
  - hello@xaviershay.com