hardmock 1.3.3 → 1.3.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/CHANGES CHANGED
@@ -1,3 +1,7 @@
1
+ Hardmock 1.3.4
2
+
3
+ * Prevents accidental stubbing and mocking on NilClasses
4
+
1
5
  Hardmock 1.3.3
2
6
 
3
7
  * stubs! and expects! no longer require that their target methods exist in reality (this used to prevent you from stubbing methods that "exist" by virtue of "method_missing"
data/Rakefile CHANGED
@@ -1,7 +1,7 @@
1
1
  require 'rake'
2
2
  require 'rubygems'
3
3
 
4
- HARDMOCK_VERSION = "1.3.3"
4
+ HARDMOCK_VERSION = "1.3.4"
5
5
 
6
6
  Dir["rake_tasks/*.rake"].each { |f| load f }
7
7
 
@@ -2,16 +2,19 @@ module Hardmock
2
2
  # Raised when:
3
3
  # * Unexpected method is called on a mock object
4
4
  # * Bad arguments passed to an expected call
5
- class ExpectationError < StandardError; end
5
+ class ExpectationError < StandardError #:nodoc:#
6
+ end
6
7
 
7
8
  # Raised for methods that should no longer be called. Hopefully, the exception message contains helpful alternatives.
8
- class DeprecationError < StandardError; end
9
+ class DeprecationError < StandardError #:nodoc:#
10
+ end
9
11
 
10
12
  # Raised when stubbing fails
11
- class StubbingError < StandardError; end
13
+ class StubbingError < StandardError #:nodoc:#
14
+ end
12
15
 
13
16
  # Raised when it is discovered that an expected method call was never made.
14
- class VerifyError < StandardError
17
+ class VerifyError < StandardError #:nodoc:#
15
18
  def initialize(msg,unmet_expectations)
16
19
  super("#{msg}:" + unmet_expectations.map { |ex| "\n * #{ex.to_s}" }.join)
17
20
  end
@@ -52,7 +52,7 @@ module Hardmock
52
52
  # Exists only for documentation
53
53
  end
54
54
 
55
- class ReplacedMethod
55
+ class ReplacedMethod #:nodoc:#
56
56
  attr_reader :target, :method_name
57
57
 
58
58
  def initialize(target, method_name)
@@ -151,6 +151,24 @@ module Hardmock
151
151
 
152
152
  end
153
153
 
154
+ class ::NilClass
155
+ # Use this only if you really mean it
156
+ alias_method :intentionally_stubs!, :stubs!
157
+
158
+ # Use this only if you really mean it
159
+ alias_method :intentionally_expects!, :expects!
160
+
161
+ # Overridden to protect against accidental nil reference self delusion
162
+ def stubs!(mname)
163
+ raise StubbingError, "Cannot stub #{mname} method on nil. (If you really mean to, try 'intentionally_stubs!')"
164
+ end
165
+
166
+ # Overridden to protect against accidental nil reference self delusion
167
+ def expects!(mname, *args)
168
+ raise StubbingError, "Cannot mock #{mname} method on nil. (If you really mean to, try 'intentionally_expects!')"
169
+ end
170
+ end
171
+
154
172
  class << self
155
173
  def track_replaced_method(replaced_method)
156
174
  all_replaced_methods << replaced_method
@@ -320,6 +320,34 @@ class StubbingTest < Test::Unit::TestCase
320
320
  end
321
321
  end
322
322
 
323
+ it "does not allow stubbing on nil objects" do
324
+ [ nil, @this_is_nil ].each do |nil_obj|
325
+ assert_error Hardmock::StubbingError, /cannot/i, /nil/i, /intentionally/ do
326
+ nil_obj.stubs!(:wont_work)
327
+ end
328
+ end
329
+ end
330
+
331
+ it "does not allow concrete method mocking on nil objects" do
332
+ [ nil, @this_is_nil ].each do |nil_obj|
333
+ assert_error Hardmock::StubbingError, /cannot/i, /nil/i, /intentionally/ do
334
+ nil_obj.expects!(:wont_work)
335
+ end
336
+ end
337
+ end
338
+
339
+ it "provides an alternate method for stubbing on nil objects" do
340
+ @this_is_nil.intentionally_stubs!(:bogus).returns('output')
341
+ assert_equal 'output', @this_is_nil.bogus
342
+ end
343
+
344
+ it "provides an alternate method for mocking concreate methods on nil objects" do
345
+ @this_is_nil.intentionally_expects!(:bogus).returns('output')
346
+ assert_error Hardmock::VerifyError, /unmet expectations/i, /NilClass.bogus/ do
347
+ verify_mocks
348
+ end
349
+ end
350
+
323
351
  #
324
352
  # HELPERS
325
353
  #
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hardmock
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.3
4
+ version: 1.3.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - David Crosby
@@ -9,7 +9,7 @@ autorequire: hardmock
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2007-12-05 00:00:00 -05:00
12
+ date: 2007-12-06 00:00:00 -05:00
13
13
  default_executable:
14
14
  dependencies: []
15
15