flexmock 0.8.11 → 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (51) hide show
  1. data/README.rdoc +2 -2
  2. data/Rakefile +23 -65
  3. data/TAGS +689 -691
  4. data/doc/releases/flexmock-0.9.0.rdoc +89 -0
  5. data/lib/flexmock.rb +1 -1
  6. data/lib/flexmock/argument_matchers.rb +5 -5
  7. data/lib/flexmock/argument_types.rb +1 -1
  8. data/lib/flexmock/base.rb +1 -1
  9. data/lib/flexmock/core.rb +5 -5
  10. data/lib/flexmock/core_class_methods.rb +6 -6
  11. data/lib/flexmock/default_framework_adapter.rb +2 -2
  12. data/lib/flexmock/deprecated_methods.rb +1 -1
  13. data/lib/flexmock/errors.rb +1 -1
  14. data/lib/flexmock/expectation.rb +12 -12
  15. data/lib/flexmock/expectation_director.rb +1 -1
  16. data/lib/flexmock/mock_container.rb +12 -2
  17. data/lib/flexmock/noop.rb +2 -2
  18. data/lib/flexmock/ordering.rb +1 -1
  19. data/lib/flexmock/partial_mock.rb +10 -3
  20. data/lib/flexmock/rails.rb +1 -1
  21. data/lib/flexmock/recorder.rb +1 -1
  22. data/lib/flexmock/rspec.rb +1 -1
  23. data/lib/flexmock/test_unit.rb +2 -2
  24. data/lib/flexmock/test_unit_integration.rb +5 -5
  25. data/lib/flexmock/undefined.rb +8 -4
  26. data/lib/flexmock/validators.rb +2 -2
  27. data/lib/flexmock/version.rb +11 -0
  28. data/test/{test_aliasing.rb → aliasing_test.rb} +7 -8
  29. data/test/{test_container_methods.rb → container_methods_test.rb} +21 -22
  30. data/test/{test_default_framework_adapter.rb → default_framework_adapter_test.rb} +8 -9
  31. data/test/{test_demeter_mocking.rb → demeter_mocking_test.rb} +1 -4
  32. data/test/{test_deprecated_methods.rb → deprecated_methods_test.rb} +11 -13
  33. data/test/{test_examples_from_readme.rb → examples_from_readme_test.rb} +5 -6
  34. data/test/{test_extended_should_receive.rb → extended_should_receive_test.rb} +11 -12
  35. data/test/{test_flexmodel.rb → flexmodel_test.rb} +1 -2
  36. data/test/{test_naming.rb → naming_test.rb} +5 -6
  37. data/test/{test_new_instances.rb → new_instances_test.rb} +36 -28
  38. data/test/{test_partial_mock.rb → partial_mock_test.rb} +20 -18
  39. data/test/{test_rails_view_stub.rb → rails_view_stub_test.rb} +3 -3
  40. data/test/{test_record_mode.rb → record_mode_test.rb} +2 -5
  41. data/test/rspec_integration/integration_spec.rb +14 -8
  42. data/test/{test_samples.rb → samples_test.rb} +13 -14
  43. data/test/{test_should_ignore_missing.rb → should_ignore_missing_test.rb} +4 -6
  44. data/test/{test_should_receive.rb → should_receive_test.rb} +39 -42
  45. data/test/test_setup.rb +30 -0
  46. data/test/test_unit_integration/{test_auto_test_unit.rb → auto_test_unit_test.rb} +4 -4
  47. data/test/tu_integration_test.rb +99 -0
  48. data/test/{test_undefined.rb → undefined_test.rb} +2 -3
  49. metadata +31 -39
  50. data/test/asserts.rb +0 -34
  51. data/test/test_tu_integration.rb +0 -94
@@ -0,0 +1,89 @@
1
+ = FlexMock 0.9.0 Released
2
+
3
+ FlexMock is a flexible mocking library for use in unit testing and
4
+ behavior specification in Ruby. Release 0.8.5 is a minor release with
5
+ a few bug fixes.
6
+
7
+ == Ruby 1.9.3 Compatibility
8
+
9
+ * Fixed a number of minor warnings reported by the 1.9.3 version of Ruby.
10
+
11
+ == What is FlexMock?
12
+
13
+ FlexMock is a flexible framework for creating mock object for testing. When
14
+ running unit tests, it is often desirable to use isolate the objects being
15
+ tested from the "real world" by having them interact with simplified test
16
+ objects. Sometimes these test objects simply return values when called, other
17
+ times they verify that certain methods were called with particular arguments
18
+ in a particular order.
19
+
20
+ FlexMock makes creating these test objects easy.
21
+
22
+ === Features
23
+
24
+ * Easy integration with both Test::Unit and RSpec. Mocks created with the
25
+ flexmock method are automatically verified at the end of the test or
26
+ example.
27
+
28
+ * A fluent interface that allows mock behavior to be specified very
29
+ easily.
30
+
31
+ * A "record mode" where an existing implementation can record its
32
+ interaction with a mock for later validation against a new
33
+ implementation.
34
+
35
+ * Easy mocking of individual methods in existing, non-mock objects.
36
+
37
+ * Easy mocking of chains of method calls.
38
+
39
+ * The ability to cause classes to instantiate test instances (instead of real
40
+ instances) for the duration of a test.
41
+
42
+ === Example
43
+
44
+ Suppose you had a Dog object that wagged a tail when it was happy.
45
+ Something like this:
46
+
47
+ class Dog
48
+ def initialize(a_tail)
49
+ @tail = a_tail
50
+ end
51
+ def happy
52
+ @tail.wag
53
+ end
54
+ end
55
+
56
+ To test the +Dog+ class without a real +Tail+ object (perhaps because
57
+ real +Tail+ objects activate servos in some robotic equipment), you
58
+ can do something like this:
59
+
60
+ require 'test/unit'
61
+ require 'flexmock/test_unit'
62
+
63
+ class TestDog < Test::Unit::TestCase
64
+ def test_dog_wags_tail_when_happy
65
+ tail = flexmock("tail")
66
+ tail.should_receive(:wag).once
67
+ dog = Dog.new(tail)
68
+ dog.happy
69
+ end
70
+ end
71
+
72
+ FlexMock will automatically verify that the mocked tail object received the
73
+ message +wag+ exactly one time. If it doesn't, the test will not pass.
74
+
75
+ See the FlexMock documentation at http://flexmock.rubyforge.org for details on
76
+ specifying arguments and return values on mocked methods, as well as a simple
77
+ technique for mocking tail objects when the Dog class creates the tail objects
78
+ directly.
79
+
80
+ == Availability
81
+
82
+ You can make sure you have the latest version with a quick RubyGems command:
83
+
84
+ gem install flexmock (you may need root/admin privileges)
85
+
86
+ You will find documentation at: http://flexmock.rubyforge.org.
87
+
88
+ -- Jim Weirich
89
+
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
3
  #---
4
- # Copyright 2003, 2004, 2005, 2006, 2007 by Jim Weirich (jim@weirichhouse.org).
4
+ # Copyright 2003-2011 by Jim Weirich (jim@weirichhouse.org).
5
5
  # All rights reserved.
6
6
 
7
7
  # Permission is granted for use, copying, modification, distribution,
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
3
  #---
4
- # Copyright 2003, 2004, 2005, 2006, 2007 by Jim Weirich (jim@weirichhouse.org).
4
+ # Copyright 2003-2011 by Jim Weirich (jim@weirichhouse.org).
5
5
  # All rights reserved.
6
6
  #
7
7
  # Permission is granted for use, copying, modification, distribution,
@@ -52,7 +52,7 @@ class FlexMock
52
52
  "on{...}"
53
53
  end
54
54
  end
55
-
55
+
56
56
  ####################################################################
57
57
  # Match only things where the block evaluates to true.
58
58
  class HashMatcher
@@ -66,7 +66,7 @@ class FlexMock
66
66
  "hsh(#{@hash.inspect})"
67
67
  end
68
68
  end
69
-
69
+
70
70
  ####################################################################
71
71
  # Match only things where the block evaluates to true.
72
72
  class DuckMatcher
@@ -80,6 +80,6 @@ class FlexMock
80
80
  "ducktype(#{@methods.map{|m| m.inspect}.join(',')})"
81
81
  end
82
82
  end
83
-
84
-
83
+
84
+
85
85
  end
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
3
  #---
4
- # Copyright 2003, 2004, 2005, 2006, 2007 by Jim Weirich (jim@weirichhouse.org).
4
+ # Copyright 2003-2011 by Jim Weirich (jim@weirichhouse.org).
5
5
  # All rights reserved.
6
6
 
7
7
  # Permission is granted for use, copying, modification, distribution,
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
3
  #---
4
- # Copyright 2003, 2004, 2005, 2006, 2007 by Jim Weirich (jim@weirichhouse.org).
4
+ # Copyright 2003-2011 by Jim Weirich (jim@weirichhouse.org).
5
5
  # All rights reserved.
6
6
 
7
7
  # Permission is granted for use, copying, modification, distribution,
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
3
  #---
4
- # Copyright 2003, 2004, 2005, 2006, 2007 by Jim Weirich (jim@weirichhouse.org).
4
+ # Copyright 2003-2011 by Jim Weirich (jim@weirichhouse.org).
5
5
  # All rights reserved.
6
6
  #
7
7
  # Permission is granted for use, copying, modification, distribution,
@@ -16,7 +16,7 @@ require 'flexmock/ordering'
16
16
  ######################################################################
17
17
  # FlexMock is a flexible mock object framework for supporting testing.
18
18
  #
19
- # FlexMock has a simple interface that's easy to remember, and leaves
19
+ # FlexMock has a simple interface that's easy to remember, and leaves
20
20
  # the hard stuff to all those other mock object implementations.
21
21
  #
22
22
  # Basic Usage:
@@ -166,7 +166,7 @@ class FlexMock
166
166
  end
167
167
  @last_expectation
168
168
  end
169
-
169
+
170
170
  # Declare that the mock object should expect methods by providing a
171
171
  # recorder for the methods and having the user invoke the expected
172
172
  # methods in a block. Further expectations may be applied the
@@ -194,8 +194,8 @@ class FlexMock
194
194
  "in mock '#{@flexmock_name}': #{ex.message}",
195
195
  ex.backtrace
196
196
  end
197
-
198
-
197
+
198
+
199
199
  # Override the existing definition of method +sym+ in the mock.
200
200
  # Most methods depend on the method_missing trick to be invoked.
201
201
  # However, if the method already exists, it will not call
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
3
  #---
4
- # Copyright 2003, 2004, 2005, 2006, 2007 by Jim Weirich (jim@weirichhouse.org).
4
+ # Copyright 2003-2011 by Jim Weirich (jim@weirichhouse.org).
5
5
  # All rights reserved.
6
6
 
7
7
  # Permission is granted for use, copying, modification, distribution,
@@ -41,7 +41,7 @@ class FlexMock
41
41
  container = UseContainer.new
42
42
  mocks = names.collect { |n| container.flexmock(n) }
43
43
  yield(*mocks)
44
- rescue Exception => ex
44
+ rescue Exception => _
45
45
  container.got_exception = true
46
46
  raise
47
47
  ensure
@@ -66,16 +66,16 @@ class FlexMock
66
66
 
67
67
  end
68
68
 
69
- # Container object to be used by the FlexMock.use method.
69
+ # Container object to be used by the FlexMock.use method.
70
70
  class UseContainer
71
71
  include MockContainer
72
-
72
+
73
73
  attr_accessor :got_exception
74
-
74
+
75
75
  def initialize
76
76
  @got_exception = false
77
77
  end
78
-
78
+
79
79
  def passed?
80
80
  ! got_exception
81
81
  end
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
3
  #---
4
- # Copyright 2003, 2004, 2005, 2006, 2007 by Jim Weirich (jim@weirichhouse.org).
4
+ # Copyright 2003-2011 by Jim Weirich (jim@weirichhouse.org).
5
5
  # All rights reserved.
6
6
 
7
7
  # Permission is granted for use, copying, modification, distribution,
@@ -28,4 +28,4 @@ class FlexMock
28
28
  AssertionFailedError
29
29
  end
30
30
  end
31
- end
31
+ end
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
3
  #---
4
- # Copyright 2003, 2004, 2005, 2006, 2007 by Jim Weirich (jim@weirichhouse.org).
4
+ # Copyright 2003-2011 by Jim Weirich (jim@weirichhouse.org).
5
5
  # All rights reserved.
6
6
  #
7
7
  # Permission is granted for use, copying, modification, distribution,
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
3
  #---
4
- # Copyright 2003, 2004, 2005, 2006, 2007 by Jim Weirich (jim@weirichhouse.org).
4
+ # Copyright 2003-2011 by Jim Weirich (jim@weirichhouse.org).
5
5
  # All rights reserved.
6
6
  #
7
7
  # Permission is granted for use, copying, modification, distribution,
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
3
  #---
4
- # Copyright 2003, 2004, 2005, 2006, 2007 by Jim Weirich (jim@weirichhouse.org).
4
+ # Copyright 2003-2011 by Jim Weirich (jim@weirichhouse.org).
5
5
  # All rights reserved.
6
6
 
7
7
  # Permission is granted for use, copying, modification, distribution,
@@ -12,7 +12,7 @@
12
12
  require 'flexmock/noop'
13
13
 
14
14
  class FlexMock
15
-
15
+
16
16
  ####################################################################
17
17
  # An Expectation is returned from each +should_receive+ message sent
18
18
  # to mock object. Each expectation records how a message matching
@@ -69,7 +69,7 @@ class FlexMock
69
69
  def return_value(args)
70
70
  case @return_queue.size
71
71
  when 0
72
- block = lambda { |*args| @return_value }
72
+ block = lambda { |*a| @return_value }
73
73
  when 1
74
74
  block = @return_queue.first
75
75
  else
@@ -85,7 +85,7 @@ class FlexMock
85
85
  unless @yield_queue.empty?
86
86
  block = args.last
87
87
  values = (@yield_queue.size == 1) ? @yield_queue.first : @yield_queue.shift
88
- if block && block.respond_to?(:call)
88
+ if block && block.respond_to?(:call)
89
89
  @return_value = block.call(*values)
90
90
  else
91
91
  fail MockError, "No Block given to mock with 'and_yield' expectation"
@@ -103,7 +103,7 @@ class FlexMock
103
103
  # Is this expectation constrained by any call counts?
104
104
  def call_count_constrained?
105
105
  ! @count_validators.empty?
106
- end
106
+ end
107
107
 
108
108
  # Validate that the order
109
109
  def validate_order
@@ -186,7 +186,7 @@ class FlexMock
186
186
  # +returns+ is an alias for +and_return+.
187
187
  #
188
188
  def and_return(*args, &block)
189
- if block_given?
189
+ if block_given?
190
190
  @return_queue << block
191
191
  else
192
192
  args.each do |arg|
@@ -230,7 +230,7 @@ class FlexMock
230
230
  end
231
231
  alias :yields :and_yield
232
232
 
233
-
233
+
234
234
  # :call-seq:
235
235
  # and_raise(an_exception)
236
236
  # and_raise(SomeException)
@@ -241,12 +241,12 @@ class FlexMock
241
241
  #
242
242
  # * If an exception instance is given, then that instance will be
243
243
  # raised.
244
- #
244
+ #
245
245
  # * If an exception class is given, the exception raised with be
246
246
  # an instance of that class constructed with +new+. Any
247
247
  # additional arguments in the argument list will be passed to
248
248
  # the +new+ constructor when it is invoked.
249
- #
249
+ #
250
250
  # +raises+ is an alias for +and_raise+.
251
251
  #
252
252
  def and_raise(exception, *args)
@@ -418,7 +418,7 @@ class FlexMock
418
418
 
419
419
  # The following methods return a value, so we make an arbitrary choice
420
420
  # and return the value for the first expectation in the composite.
421
-
421
+
422
422
  # Return the order number of the first expectation in the list.
423
423
  def order_number
424
424
  @expectations.first.order_number
@@ -428,7 +428,7 @@ class FlexMock
428
428
  def mock
429
429
  @expectations.first.mock
430
430
  end
431
-
431
+
432
432
  # Start a new method expectation. The following constraints will be
433
433
  # applied to the new expectation.
434
434
  def should_receive(*args, &block)
@@ -462,7 +462,7 @@ class FlexMock
462
462
  @expectations << [sym, args, block]
463
463
  self
464
464
  end
465
-
465
+
466
466
  # Apply the recorded messages to the given object in a chaining fashion
467
467
  # (i.e. the result of the previous call is used as the target of the next
468
468
  # call).
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
3
  #---
4
- # Copyright 2003, 2004, 2005, 2006, 2007 by Jim Weirich (jim@weirichhouse.org).
4
+ # Copyright 2003-2011 by Jim Weirich (jim@weirichhouse.org).
5
5
  # All rights reserved.
6
6
 
7
7
  # Permission is granted for use, copying, modification, distribution,
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
3
  #---
4
- # Copyright 2003, 2004, 2005, 2006, 2007 by Jim Weirich (jim@weirichhouse.org).
4
+ # Copyright 2003-2011 by Jim Weirich (jim@weirichhouse.org).
5
5
  # All rights reserved.
6
6
 
7
7
  # Permission is granted for use, copying, modification, distribution,
@@ -29,7 +29,7 @@ class FlexMock
29
29
  # Do the flexmock specific teardown stuff. If you need finer control,
30
30
  # you can use either +flexmock_verify+ or +flexmock_close+.
31
31
  def flexmock_teardown
32
- flexmock_verify if passed?
32
+ flexmock_verify unless flexmock_test_has_failed?
33
33
  ensure
34
34
  flexmock_close
35
35
  end
@@ -164,6 +164,16 @@ class FlexMock
164
164
  mocking_object.flexmock_container = self
165
165
  mocking_object
166
166
  end
167
+
168
+ private
169
+
170
+ # In frameworks (e.g. MiniTest) passed? will return nil to
171
+ # indicate the test isn't over yet. From our point of view we are
172
+ # only interested if the test has actually failed, so we wrap the
173
+ # raw call to passed? and handle accordingly.
174
+ def flexmock_test_has_failed?
175
+ passed? == false
176
+ end
167
177
  end
168
178
 
169
179
  # #################################################################
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
3
  #---
4
- # Copyright 2003, 2004, 2005, 2006, 2007 by Jim Weirich (jim@weirichhouse.org).
4
+ # Copyright 2003-2011 by Jim Weirich (jim@weirichhouse.org).
5
5
  # All rights reserved.
6
6
 
7
7
  # Permission is granted for use, copying, modification, distribution,
@@ -10,4 +10,4 @@
10
10
  #+++
11
11
 
12
12
  # No-op include file. Used as a kludge so that only the comments in the
13
- # core.rb file are applied to the FlexMock class.
13
+ # core.rb file are applied to the FlexMock class.
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
3
  #---
4
- # Copyright 2003, 2004, 2005, 2006, 2007 by Jim Weirich (jim@weirichhouse.org).
4
+ # Copyright 2003-2011 by Jim Weirich (jim@weirichhouse.org).
5
5
  # All rights reserved.
6
6
 
7
7
  # Permission is granted for use, copying, modification, distribution,
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
3
  #---
4
- # Copyright 2003, 2004, 2005, 2006, 2007 by Jim Weirich (jim@weirichhouse.org).
4
+ # Copyright 2003-2011 by Jim Weirich (jim@weirichhouse.org).
5
5
  # All rights reserved.
6
6
 
7
7
  # Permission is granted for use, copying, modification, distribution,
@@ -125,6 +125,7 @@ class FlexMock
125
125
  allocators = [:new] if allocators.empty?
126
126
  result = ExpectationRecorder.new
127
127
  allocators.each do |allocate_method|
128
+ check_allocate_method(allocate_method)
128
129
  # HACK: Without the following lambda, Ruby 1.9 will not bind
129
130
  # the allocate_method parameter correctly.
130
131
  lambda { }
@@ -182,6 +183,12 @@ class FlexMock
182
183
 
183
184
  private
184
185
 
186
+ def check_allocate_method(allocate_method)
187
+ if allocate_method == :allocate && RUBY_VERSION >= "1.9"
188
+ fail UsageError, "Cannot mock the allocation method using new_instances in Ruby 1.9"
189
+ end
190
+ end
191
+
185
192
  # The singleton class of the object.
186
193
  def sclass
187
194
  class << @obj; self; end
@@ -235,7 +242,7 @@ class FlexMock
235
242
  end
236
243
  end
237
244
  new_alias
238
- rescue NameError => ex
245
+ rescue NameError => _
239
246
  # Alias attempt failed
240
247
  nil
241
248
  end
@@ -259,7 +266,7 @@ class FlexMock
259
266
  instance_variable_get('@flexmock_proxy').mock.#{method_name}(*args, &block)
260
267
  end
261
268
  }, __FILE__, eval_line
262
- make_rcov_recognize_the_above_eval_is_covered = true
269
+ _ = true # make rcov recognize the above eval is covered
263
270
  end
264
271
  end
265
272