mocha 0.9.0 → 0.9.1

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/RELEASE CHANGED
@@ -1,4 +1,17 @@
1
- = 0.9.0
1
+ = 0.9.1 (r349)
2
+
3
+ * Fixed bug #21465 - expects & stubs should support method names as strings (as well as symbols) or fail fast. Convert all expectation method names to a symbol in case they were supplied as a string.
4
+ * By removing Mock#unexpected_method_called we reduce the number of methods vulnerable to the problem that surfaced in bug #21563.
5
+ * Fix bug #21563 - stubbing 'verified?' method is unsafe. Instance method names on the Mock class should be more obscure.
6
+ * Performance improvement. StubbaExampleTest goes twice as fast on my local machine.
7
+ * Added primitive performance test to default rake task.
8
+ * Fix format of case statements which don't work in Ruby 1.9 and make others consistent.
9
+ * There is no point in running (potentially expensive) checks if configuration is set to allow such checks to fail. This is a relatively quick fix in response to Chris McGrath's performance problems.
10
+ * Fix for bug #21161 - 'uninitialized constant Deprecation in stubba.rb'.
11
+ * It's more readable to talk about 'once' and 'twice' rather than '1 time' and '2 times'.
12
+ * Fix bug #20883 - never should raise when called to prevent follow up errors. Fail fast when there are no matching invokable expectations and handle the stub_everything case sensibly. This might not be entirely backwards compatible, but I think the benefits outweigh the risks. The most likely change is that a test that was already failing will now fail faster, which doesn't seem so awful.
13
+
14
+ = 0.9.0 (r316)
2
15
 
3
16
  * Configurable warnings or errors
4
17
  * when a method on a non-public method is stubbed
data/Rakefile CHANGED
@@ -5,11 +5,11 @@ require 'rake/testtask'
5
5
  require 'rake/contrib/sshpublisher'
6
6
 
7
7
  module Mocha
8
- VERSION = "0.9.0"
8
+ VERSION = "0.9.1"
9
9
  end
10
10
 
11
11
  desc "Run all tests"
12
- task 'default' => ['test:units', 'test:acceptance']
12
+ task 'default' => ['test:units', 'test:acceptance', 'test:performance']
13
13
 
14
14
  namespace 'test' do
15
15
 
@@ -42,6 +42,23 @@ namespace 'test' do
42
42
  # t.rcov_opts << '--xref'
43
43
  # end
44
44
 
45
+ desc "Run performance tests"
46
+ task 'performance' do
47
+ require 'test/acceptance/stubba_example_test'
48
+ require 'test/acceptance/mocha_example_test'
49
+ iterations = 1000
50
+ puts "\nBenchmarking with #{iterations} iterations..."
51
+ [MochaExampleTest, StubbaExampleTest].each do |test_case|
52
+ puts "#{test_case}: #{benchmark_test_case(test_case, iterations)} seconds."
53
+ end
54
+ end
55
+
56
+ end
57
+
58
+ def benchmark_test_case(klass, iterations)
59
+ require 'test/unit/ui/console/testrunner'
60
+ require 'benchmark'
61
+ time = Benchmark.realtime { iterations.times { Test::Unit::UI::Console::TestRunner.run(klass, Test::Unit::UI::SILENT) } }
45
62
  end
46
63
 
47
64
  desc 'Generate RDoc'
@@ -43,11 +43,10 @@ module Mocha
43
43
  end
44
44
 
45
45
  def method_exists?(method)
46
- existing_methods = []
47
- existing_methods += stubbee.public_instance_methods(false)
48
- existing_methods += stubbee.protected_instance_methods(false)
49
- existing_methods += stubbee.private_instance_methods(false)
50
- existing_methods.any? { |m| m.to_s == method.to_s }
46
+ return true if stubbee.public_instance_methods(false).include?(method)
47
+ return true if stubbee.protected_instance_methods(false).include?(method)
48
+ return true if stubbee.private_instance_methods(false).include?(method)
49
+ return false
51
50
  end
52
51
 
53
52
  end
@@ -20,10 +20,8 @@ module Mocha
20
20
 
21
21
  def times(range_or_count)
22
22
  case range_or_count
23
- when Range
24
- new(range_or_count.first, range_or_count.last)
25
- else
26
- new(range_or_count, range_or_count)
23
+ when Range then new(range_or_count.first, range_or_count.last)
24
+ else new(range_or_count, range_or_count)
27
25
  end
28
26
  end
29
27
 
@@ -80,7 +78,12 @@ module Mocha
80
78
  attr_reader :required, :maximum
81
79
 
82
80
  def times(number)
83
- number == 1 ? "once" : "#{number} times"
81
+ case number
82
+ when 0 then "no times"
83
+ when 1 then "once"
84
+ when 2 then "twice"
85
+ else "#{number} times"
86
+ end
84
87
  end
85
88
 
86
89
  def infinite?(number)
@@ -7,7 +7,8 @@ module Mocha
7
7
  attr_reader :stubbee, :method
8
8
 
9
9
  def initialize(stubbee, method)
10
- @stubbee, @method = stubbee, method
10
+ @stubbee = stubbee
11
+ @method = RUBY_VERSION < '1.9' ? method.to_s : method.to_sym
11
12
  end
12
13
 
13
14
  def stub
@@ -59,7 +60,8 @@ module Mocha
59
60
  else
60
61
  method_name = method.to_s.gsub(/\W/) { |s| "_substituted_character_#{s.ord}_" }
61
62
  end
62
- "__stubba__#{method_name}__stubba__"
63
+ hidden_method = "__stubba__#{method_name}__stubba__"
64
+ RUBY_VERSION < '1.9' ? hidden_method.to_s : hidden_method.to_sym
63
65
  end
64
66
 
65
67
  def eql?(other)
@@ -74,13 +76,11 @@ module Mocha
74
76
  end
75
77
 
76
78
  def method_exists?(method)
77
- existing_methods = []
78
- existing_methods += stubbee.public_methods(true) - stubbee.superclass.public_methods(true)
79
- existing_methods += stubbee.protected_methods(true) - stubbee.superclass.protected_methods(true)
80
- existing_methods += stubbee.private_methods(true) - stubbee.superclass.private_methods(true)
81
- existing_methods.any? { |m| m.to_s == method.to_s }
79
+ symbol = method.to_sym
80
+ metaclass = stubbee.__metaclass__
81
+ metaclass.public_method_defined?(symbol) || metaclass.protected_method_defined?(symbol) || metaclass.private_method_defined?(symbol)
82
82
  end
83
83
 
84
84
  end
85
85
 
86
- end
86
+ end
@@ -354,7 +354,7 @@ module Mocha # :nodoc:
354
354
 
355
355
  def initialize(mock, expected_method_name, backtrace = nil)
356
356
  @mock = mock
357
- @method_matcher = MethodMatcher.new(expected_method_name)
357
+ @method_matcher = MethodMatcher.new(expected_method_name.to_sym)
358
358
  @parameters_matcher = ParametersMatcher.new
359
359
  @ordering_constraints = []
360
360
  @side_effects = []
@@ -422,11 +422,11 @@ module Mocha # :nodoc:
422
422
 
423
423
  def mocha_inspect
424
424
  message = "#{@cardinality.mocha_inspect}, "
425
- if @invocation_count > 0
426
- message << "already invoked #{@invocation_count} time"
427
- message << "s" if @invocation_count > 1
428
- else
429
- message << "never invoked"
425
+ message << case @invocation_count
426
+ when 0 then "not yet invoked"
427
+ when 1 then "already invoked once"
428
+ when 2 then "already invoked twice"
429
+ else "already invoked #{@invocation_count} times"
430
430
  end
431
431
  message << ": "
432
432
  message << method_signature
@@ -7,7 +7,7 @@ module Mocha # :nodoc:
7
7
  end
8
8
 
9
9
  def add(expectation)
10
- @expectations << expectation
10
+ @expectations.unshift(expectation)
11
11
  expectation
12
12
  end
13
13
 
@@ -15,10 +15,12 @@ module Mocha # :nodoc:
15
15
  @expectations.any? { |expectation| expectation.matches_method?(method_name) }
16
16
  end
17
17
 
18
- def detect(method_name, *arguments)
19
- expectations = @expectations.reverse.select { |e| e.match?(method_name, *arguments) }
20
- expectation = expectations.detect { |e| e.invocations_allowed? }
21
- expectation || expectations.first
18
+ def match(method_name, *arguments)
19
+ matching_expectations(method_name, *arguments).first
20
+ end
21
+
22
+ def match_allowing_invocation(method_name, *arguments)
23
+ matching_expectations(method_name, *arguments).detect { |e| e.invocations_allowed? }
22
24
  end
23
25
 
24
26
  def verified?(assertion_counter = nil)
@@ -37,6 +39,12 @@ module Mocha # :nodoc:
37
39
  @expectations.length
38
40
  end
39
41
 
42
+ private
43
+
44
+ def matching_expectations(method_name, *arguments)
45
+ @expectations.select { |e| e.match?(method_name, *arguments) }
46
+ end
47
+
40
48
  end
41
49
 
42
50
  end
@@ -5,11 +5,10 @@ module Mocha
5
5
  class InstanceMethod < ClassMethod
6
6
 
7
7
  def method_exists?(method)
8
- existing_methods = []
9
- existing_methods += stubbee.public_methods(false)
10
- existing_methods += stubbee.protected_methods(false)
11
- existing_methods += stubbee.private_methods(false)
12
- existing_methods.any? { |m| m.to_s == method.to_s }
8
+ return true if stubbee.public_methods(false).include?(method)
9
+ return true if stubbee.protected_methods(false).include?(method)
10
+ return true if stubbee.private_methods(false).include?(method)
11
+ return false
13
12
  end
14
13
 
15
14
  end
@@ -5,6 +5,7 @@ require 'mocha/names'
5
5
  require 'mocha/mockery'
6
6
  require 'mocha/method_matcher'
7
7
  require 'mocha/parameters_matcher'
8
+ require 'mocha/unexpected_invocation'
8
9
 
9
10
  module Mocha # :nodoc:
10
11
 
@@ -145,10 +146,6 @@ module Mocha # :nodoc:
145
146
 
146
147
  alias_method :quacks_like, :responds_like
147
148
 
148
- def add_expectation(expectation)
149
- @expectations.add(expectation)
150
- end
151
-
152
149
  def stub_everything
153
150
  @everything_stubbed = true
154
151
  end
@@ -157,13 +154,14 @@ module Mocha # :nodoc:
157
154
  if @responder and not @responder.respond_to?(symbol)
158
155
  raise NoMethodError, "undefined method `#{symbol}' for #{self.mocha_inspect} which responds like #{@responder.mocha_inspect}"
159
156
  end
160
- matching_expectation = @expectations.detect(symbol, *arguments)
161
- if matching_expectation then
162
- matching_expectation.invoke(&block)
163
- elsif @everything_stubbed then
164
- return
157
+ if matching_expectation_allowing_invocation = @expectations.match_allowing_invocation(symbol, *arguments)
158
+ matching_expectation_allowing_invocation.invoke(&block)
165
159
  else
166
- unexpected_method_called(symbol, *arguments)
160
+ if (matching_expectation = @expectations.match(symbol, *arguments)) || (!matching_expectation && !@everything_stubbed)
161
+ message = UnexpectedInvocation.new(self, symbol, *arguments).to_s
162
+ message << Mockery.instance.mocha_inspect
163
+ raise ExpectationError.new(message, caller)
164
+ end
167
165
  end
168
166
  end
169
167
 
@@ -175,16 +173,7 @@ module Mocha # :nodoc:
175
173
  end
176
174
  end
177
175
 
178
- def unexpected_method_called(symbol, *arguments)
179
- method_matcher = MethodMatcher.new(symbol)
180
- parameters_matcher = ParametersMatcher.new(arguments)
181
- method_signature = "#{mocha_inspect}.#{method_matcher.mocha_inspect}#{parameters_matcher.mocha_inspect}"
182
- message = "unexpected invocation: #{method_signature}\n"
183
- message << Mockery.instance.mocha_inspect
184
- raise ExpectationError.new(message, caller)
185
- end
186
-
187
- def verified?(assertion_counter = nil)
176
+ def __verified__?(assertion_counter = nil)
188
177
  @expectations.verified?(assertion_counter)
189
178
  end
190
179
 
@@ -43,7 +43,7 @@ module Mocha
43
43
  end
44
44
 
45
45
  def verify(assertion_counter = nil)
46
- unless mocks.all? { |mock| mock.verified?(assertion_counter) }
46
+ unless mocks.all? { |mock| mock.__verified__?(assertion_counter) }
47
47
  message = "not all expectations were satisfied\n#{mocha_inspect}"
48
48
  if unsatisfied_expectations.empty?
49
49
  backtrace = caller
@@ -53,7 +53,11 @@ module Mocha
53
53
  raise ExpectationError.new(message, backtrace)
54
54
  end
55
55
  expectations.each do |e|
56
- on_stubbing_method_unnecessarily(e) unless e.used?
56
+ unless Mocha::Configuration.allow?(:stubbing_method_unnecessarily)
57
+ unless e.used?
58
+ on_stubbing_method_unnecessarily(e)
59
+ end
60
+ end
57
61
  end
58
62
  end
59
63
 
@@ -82,36 +86,47 @@ module Mocha
82
86
  message
83
87
  end
84
88
 
85
- def on_stubbing(object, symbol)
86
- on_stubbing_non_existent_method(object, symbol) unless object.method_exists?(symbol, include_public_methods = true)
87
- on_stubbing_non_public_method(object, symbol) if object.method_exists?(symbol, include_public_methods = false)
88
- on_stubbing_method_on_non_mock_object(object, symbol)
89
+ def on_stubbing(object, method)
90
+ method = RUBY_VERSION < '1.9' ? method.to_s : method.to_sym
91
+ unless Mocha::Configuration.allow?(:stubbing_non_existent_method)
92
+ unless object.method_exists?(method, include_public_methods = true)
93
+ on_stubbing_non_existent_method(object, method)
94
+ end
95
+ end
96
+ unless Mocha::Configuration.allow?(:stubbing_non_public_method)
97
+ if object.method_exists?(method, include_public_methods = false)
98
+ on_stubbing_non_public_method(object, method)
99
+ end
100
+ end
101
+ unless Mocha::Configuration.allow?(:stubbing_method_on_non_mock_object)
102
+ on_stubbing_method_on_non_mock_object(object, method)
103
+ end
89
104
  end
90
105
 
91
- def on_stubbing_non_existent_method(object, symbol)
106
+ def on_stubbing_non_existent_method(object, method)
92
107
  if Mocha::Configuration.prevent?(:stubbing_non_existent_method)
93
- raise StubbingError.new("stubbing non-existent method: #{object.mocha_inspect}.#{symbol}", caller)
108
+ raise StubbingError.new("stubbing non-existent method: #{object.mocha_inspect}.#{method}", caller)
94
109
  end
95
110
  if Mocha::Configuration.warn_when?(:stubbing_non_existent_method)
96
- logger.warn "stubbing non-existent method: #{object.mocha_inspect}.#{symbol}"
111
+ logger.warn "stubbing non-existent method: #{object.mocha_inspect}.#{method}"
97
112
  end
98
113
  end
99
114
 
100
- def on_stubbing_non_public_method(object, symbol)
115
+ def on_stubbing_non_public_method(object, method)
101
116
  if Mocha::Configuration.prevent?(:stubbing_non_public_method)
102
- raise StubbingError.new("stubbing non-public method: #{object.mocha_inspect}.#{symbol}", caller)
117
+ raise StubbingError.new("stubbing non-public method: #{object.mocha_inspect}.#{method}", caller)
103
118
  end
104
119
  if Mocha::Configuration.warn_when?(:stubbing_non_public_method)
105
- logger.warn "stubbing non-public method: #{object.mocha_inspect}.#{symbol}"
120
+ logger.warn "stubbing non-public method: #{object.mocha_inspect}.#{method}"
106
121
  end
107
122
  end
108
123
 
109
- def on_stubbing_method_on_non_mock_object(object, symbol)
124
+ def on_stubbing_method_on_non_mock_object(object, method)
110
125
  if Mocha::Configuration.prevent?(:stubbing_method_on_non_mock_object)
111
- raise StubbingError.new("stubbing method on non-mock object: #{object.mocha_inspect}.#{symbol}", caller)
126
+ raise StubbingError.new("stubbing method on non-mock object: #{object.mocha_inspect}.#{method}", caller)
112
127
  end
113
128
  if Mocha::Configuration.warn_when?(:stubbing_method_on_non_mock_object)
114
- logger.warn "stubbing method on non-mock object: #{object.mocha_inspect}.#{symbol}"
129
+ logger.warn "stubbing method on non-mock object: #{object.mocha_inspect}.#{method}"
115
130
  end
116
131
  end
117
132
 
@@ -5,11 +5,10 @@ module Mocha
5
5
  class ModuleMethod < ClassMethod
6
6
 
7
7
  def method_exists?(method)
8
- existing_methods = []
9
- existing_methods += stubbee.public_methods(false)
10
- existing_methods += stubbee.protected_methods(false)
11
- existing_methods += stubbee.private_methods(false)
12
- existing_methods.any? { |m| m.to_s == method.to_s }
8
+ return true if stubbee.public_methods(false).include?(method)
9
+ return true if stubbee.protected_methods(false).include?(method)
10
+ return true if stubbee.private_methods(false).include?(method)
11
+ return false
13
12
  end
14
13
 
15
14
  end
@@ -63,10 +63,14 @@ class Object
63
63
  mocha.stubs(symbol, caller)
64
64
  end
65
65
 
66
- def method_exists?(symbol, include_public_methods = true)
67
- existing_methods = private_methods(include_superclass_methods = true) + protected_methods(include_superclass_methods = true)
68
- existing_methods += public_methods(include_superclass_methods = true) if include_public_methods
69
- existing_methods.any? { |m| m.to_s == symbol.to_s } || (respond_to?(symbol) && include_public_methods)
66
+ def method_exists?(method, include_public_methods = true)
67
+ if include_public_methods
68
+ return true if public_methods(include_superclass_methods = true).include?(method)
69
+ return true if respond_to?(method)
70
+ end
71
+ return true if protected_methods(include_superclass_methods = true).include?(method)
72
+ return true if private_methods(include_superclass_methods = true).include?(method)
73
+ return false
70
74
  end
71
75
 
72
76
  end
@@ -103,10 +107,13 @@ class Class
103
107
  @stubba_object
104
108
  end
105
109
 
106
- def method_exists?(symbol, include_public_methods = true)
107
- existing_methods = @stubba_object.private_instance_methods(include_superclass_methods = true) + @stubba_object.protected_instance_methods(include_superclass_methods = true)
108
- existing_methods += @stubba_object.public_instance_methods(include_superclass_methods = true) if include_public_methods
109
- existing_methods.any? { |m| m.to_s == symbol.to_s }
110
+ def method_exists?(method, include_public_methods = true)
111
+ if include_public_methods
112
+ return true if @stubba_object.public_instance_methods(include_superclass_methods = true).include?(method)
113
+ end
114
+ return true if @stubba_object.protected_instance_methods(include_superclass_methods = true).include?(method)
115
+ return true if @stubba_object.private_instance_methods(include_superclass_methods = true).include?(method)
116
+ return false
110
117
  end
111
118
 
112
119
  end
@@ -16,12 +16,9 @@ module Mocha # :nodoc:
16
16
 
17
17
  def next
18
18
  case @values.length
19
- when 0
20
- nil
21
- when 1
22
- @values.first.evaluate
23
- else
24
- @values.shift.evaluate
19
+ when 0 then nil
20
+ when 1 then @values.first.evaluate
21
+ else @values.shift.evaluate
25
22
  end
26
23
  end
27
24
 
@@ -0,0 +1,18 @@
1
+ module Mocha # :nodoc:
2
+
3
+ class UnexpectedInvocation
4
+
5
+ def initialize(mock, symbol, *arguments)
6
+ @mock = mock
7
+ @method_matcher = MethodMatcher.new(symbol)
8
+ @parameters_matcher = ParametersMatcher.new(arguments)
9
+ end
10
+
11
+ def to_s
12
+ method_signature = "#{@mock.mocha_inspect}.#{@method_matcher.mocha_inspect}#{@parameters_matcher.mocha_inspect}"
13
+ "unexpected invocation: #{method_signature}\n"
14
+ end
15
+
16
+ end
17
+
18
+ end
@@ -12,9 +12,9 @@ module Mocha # :nodoc:
12
12
 
13
13
  def next_invocation
14
14
  case @parameter_groups.length
15
- when 0; NoYields.new
16
- when 1; @parameter_groups.first
17
- else @parameter_groups.shift
15
+ when 0 then NoYields.new
16
+ when 1 then @parameter_groups.first
17
+ else @parameter_groups.shift
18
18
  end
19
19
  end
20
20
 
@@ -1,3 +1,4 @@
1
1
  # for backwards compatibility
2
2
  require 'mocha'
3
- Deprecation.warning "require 'stubba' is no longer needed and stubba.rb will soon be removed"
3
+ require 'mocha/deprecation'
4
+ Mocha::Deprecation.warning "require 'stubba' is no longer needed and stubba.rb will soon be removed"
@@ -0,0 +1,34 @@
1
+ require File.join(File.dirname(__FILE__), "acceptance_test_helper")
2
+ require 'mocha'
3
+
4
+ class Bug21563Test < Test::Unit::TestCase
5
+
6
+ include AcceptanceTest
7
+
8
+ def setup
9
+ setup_acceptance_test
10
+ end
11
+
12
+ def teardown
13
+ teardown_acceptance_test
14
+ end
15
+
16
+ def test_should_allow_expected_method_name_to_be_a_string
17
+ test_result = run_test do
18
+ mock = mock()
19
+ mock.expects('wibble')
20
+ mock.wibble
21
+ end
22
+ assert_passed(test_result)
23
+ end
24
+
25
+ def test_should_allow_stubbed_method_name_to_be_a_string
26
+ test_result = run_test do
27
+ mock = mock()
28
+ mock.stubs('wibble')
29
+ mock.wibble
30
+ end
31
+ assert_passed(test_result)
32
+ end
33
+
34
+ end
@@ -0,0 +1,25 @@
1
+ require File.join(File.dirname(__FILE__), "acceptance_test_helper")
2
+ require 'mocha'
3
+
4
+ class Bug21563Test < Test::Unit::TestCase
5
+
6
+ include AcceptanceTest
7
+
8
+ def setup
9
+ setup_acceptance_test
10
+ end
11
+
12
+ def teardown
13
+ teardown_acceptance_test
14
+ end
15
+
16
+ def test_should_allow_stubbing_of_verified_method
17
+ test_result = run_test do
18
+ object = Object.new
19
+ object.stubs(:verified?).returns(false)
20
+ assert !object.verified?
21
+ end
22
+ assert_passed(test_result)
23
+ end
24
+
25
+ end
@@ -22,14 +22,14 @@ class ExpectedInvocationCountTest < Test::Unit::TestCase
22
22
  assert_passed(test_result)
23
23
  end
24
24
 
25
- def test_should_fail_if_method_is_never_expected_but_is_called_once
25
+ def test_should_fail_fast_if_method_is_never_expected_but_is_called_once
26
26
  test_result = run_test do
27
27
  mock = mock('mock')
28
28
  mock.expects(:method).never
29
29
  1.times { mock.method }
30
30
  end
31
31
  assert_failed(test_result)
32
- assert_equal ["not all expectations were satisfied\nunsatisfied expectations:\n- expected never, already invoked 1 time: #<Mock:mock>.method(any_parameters)\n"], test_result.failure_messages
32
+ assert_equal ["unexpected invocation: #<Mock:mock>.method()\nsatisfied expectations:\n- expected never, not yet invoked: #<Mock:mock>.method(any_parameters)\n"], test_result.failure_messages
33
33
  end
34
34
 
35
35
  def test_should_pass_if_method_is_expected_twice_and_is_called_twice
@@ -48,17 +48,17 @@ class ExpectedInvocationCountTest < Test::Unit::TestCase
48
48
  1.times { mock.method }
49
49
  end
50
50
  assert_failed(test_result)
51
- assert_equal ["not all expectations were satisfied\nunsatisfied expectations:\n- expected exactly 2 times, already invoked 1 time: #<Mock:mock>.method(any_parameters)\n"], test_result.failure_messages
51
+ assert_equal ["not all expectations were satisfied\nunsatisfied expectations:\n- expected exactly twice, already invoked once: #<Mock:mock>.method(any_parameters)\n"], test_result.failure_messages
52
52
  end
53
53
 
54
- def test_should_fail_if_method_is_expected_twice_but_is_called_three_times
54
+ def test_should_fail_fast_if_method_is_expected_twice_but_is_called_three_times
55
55
  test_result = run_test do
56
56
  mock = mock('mock')
57
57
  mock.expects(:method).times(2)
58
58
  3.times { mock.method }
59
59
  end
60
60
  assert_failed(test_result)
61
- assert_equal ["not all expectations were satisfied\nunsatisfied expectations:\n- expected exactly 2 times, already invoked 3 times: #<Mock:mock>.method(any_parameters)\n"], test_result.failure_messages
61
+ assert_equal ["unexpected invocation: #<Mock:mock>.method()\nsatisfied expectations:\n- expected exactly twice, already invoked twice: #<Mock:mock>.method(any_parameters)\n"], test_result.failure_messages
62
62
  end
63
63
 
64
64
  def test_should_pass_if_method_is_expected_between_two_and_four_times_and_is_called_twice
@@ -95,17 +95,17 @@ class ExpectedInvocationCountTest < Test::Unit::TestCase
95
95
  1.times { mock.method }
96
96
  end
97
97
  assert_failed(test_result)
98
- assert_equal ["not all expectations were satisfied\nunsatisfied expectations:\n- expected between 2 and 4 times, already invoked 1 time: #<Mock:mock>.method(any_parameters)\n"], test_result.failure_messages
98
+ assert_equal ["not all expectations were satisfied\nunsatisfied expectations:\n- expected between 2 and 4 times, already invoked once: #<Mock:mock>.method(any_parameters)\n"], test_result.failure_messages
99
99
  end
100
100
 
101
- def test_should_fail_if_method_is_expected_between_two_and_four_times_and_is_called_five_times
101
+ def test_should_fail_fast_if_method_is_expected_between_two_and_four_times_and_is_called_five_times
102
102
  test_result = run_test do
103
103
  mock = mock('mock')
104
104
  mock.expects(:method).times(2..4)
105
105
  5.times { mock.method }
106
106
  end
107
107
  assert_failed(test_result)
108
- assert_equal ["not all expectations were satisfied\nunsatisfied expectations:\n- expected between 2 and 4 times, already invoked 5 times: #<Mock:mock>.method(any_parameters)\n"], test_result.failure_messages
108
+ assert_equal ["unexpected invocation: #<Mock:mock>.method()\nsatisfied expectations:\n- expected between 2 and 4 times, already invoked 4 times: #<Mock:mock>.method(any_parameters)\n"], test_result.failure_messages
109
109
  end
110
110
 
111
111
  def test_should_pass_if_method_is_expected_at_least_once_and_is_called_once
@@ -133,7 +133,7 @@ class ExpectedInvocationCountTest < Test::Unit::TestCase
133
133
  0.times { mock.method }
134
134
  end
135
135
  assert_failed(test_result)
136
- assert_equal ["not all expectations were satisfied\nunsatisfied expectations:\n- expected at least once, never invoked: #<Mock:mock>.method(any_parameters)\n"], test_result.failure_messages
136
+ assert_equal ["not all expectations were satisfied\nunsatisfied expectations:\n- expected at least once, not yet invoked: #<Mock:mock>.method(any_parameters)\n"], test_result.failure_messages
137
137
  end
138
138
 
139
139
  def test_should_pass_if_method_is_expected_at_most_once_and_is_never_called
@@ -154,14 +154,14 @@ class ExpectedInvocationCountTest < Test::Unit::TestCase
154
154
  assert_passed(test_result)
155
155
  end
156
156
 
157
- def test_should_fail_if_method_is_expected_at_most_once_but_is_called_twice
157
+ def test_should_fail_fast_if_method_is_expected_at_most_once_but_is_called_twice
158
158
  test_result = run_test do
159
159
  mock = mock('mock')
160
160
  mock.expects(:method).at_most_once
161
161
  2.times { mock.method }
162
162
  end
163
163
  assert_failed(test_result)
164
- assert_equal ["not all expectations were satisfied\nunsatisfied expectations:\n- expected at most once, already invoked 2 times: #<Mock:mock>.method(any_parameters)\n"], test_result.failure_messages
164
+ assert_equal ["unexpected invocation: #<Mock:mock>.method()\nsatisfied expectations:\n- expected at most once, already invoked once: #<Mock:mock>.method(any_parameters)\n"], test_result.failure_messages
165
165
  end
166
166
 
167
167
  def test_should_pass_if_method_is_never_expected_and_is_never_called_even_if_everything_is_stubbed
@@ -173,24 +173,24 @@ class ExpectedInvocationCountTest < Test::Unit::TestCase
173
173
  assert_passed(test_result)
174
174
  end
175
175
 
176
- def test_should_fail_if_method_is_never_expected_but_is_called_once_even_if_everything_is_stubbed
176
+ def test_should_fail_fast_if_method_is_never_expected_but_is_called_once_even_if_everything_is_stubbed
177
177
  test_result = run_test do
178
178
  stub = stub_everything('stub')
179
179
  stub.expects(:method).never
180
180
  1.times { stub.method }
181
181
  end
182
182
  assert_failed(test_result)
183
- assert_equal ["not all expectations were satisfied\nunsatisfied expectations:\n- expected never, already invoked 1 time: #<Mock:stub>.method(any_parameters)\n"], test_result.failure_messages
183
+ assert_equal ["unexpected invocation: #<Mock:stub>.method()\nsatisfied expectations:\n- expected never, not yet invoked: #<Mock:stub>.method(any_parameters)\n"], test_result.failure_messages
184
184
  end
185
185
 
186
- def test_should_fail_if_there_is_no_matching_expectation
186
+ def test_should_fail_fast_if_there_is_no_matching_expectation
187
187
  test_result = run_test do
188
188
  mock = mock('mock')
189
189
  mock.expects(:method).with(1)
190
190
  1.times { mock.method }
191
191
  end
192
192
  assert_failed(test_result)
193
- assert_equal ["unexpected invocation: #<Mock:mock>.method()\nunsatisfied expectations:\n- expected exactly once, never invoked: #<Mock:mock>.method(1)\n"], test_result.failure_messages
193
+ assert_equal ["unexpected invocation: #<Mock:mock>.method()\nunsatisfied expectations:\n- expected exactly once, not yet invoked: #<Mock:mock>.method(1)\n"], test_result.failure_messages
194
194
  end
195
195
 
196
196
  end
@@ -0,0 +1,15 @@
1
+ require File.join(File.dirname(__FILE__), "..", "test_helper")
2
+ require 'deprecation_disabler'
3
+
4
+ class StubbaTest < Test::Unit::TestCase
5
+
6
+ include DeprecationDisabler
7
+
8
+ def test_should_report_deprecation_of_stubba_which_will_be_removed_in_a_future_release
9
+ disable_deprecations do
10
+ load 'stubba.rb'
11
+ end
12
+ assert_equal ["require 'stubba' is no longer needed and stubba.rb will soon be removed"], Mocha::Deprecation.messages
13
+ end
14
+
15
+ end
@@ -43,7 +43,7 @@ class AnyInstanceMethodTest < Test::Unit::TestCase
43
43
  result = instance.method_x(:param1, :param2)
44
44
 
45
45
  assert_equal :result, result
46
- assert mocha.verified?
46
+ assert mocha.__verified__?
47
47
  end
48
48
 
49
49
  def test_should_restore_original_method
@@ -25,16 +25,16 @@ class CardinalityTest < Test::Unit::TestCase
25
25
  assert_equal 'allowed any number of times', Cardinality.at_least(0).mocha_inspect
26
26
 
27
27
  assert_equal 'expected at most once', Cardinality.at_most(1).mocha_inspect
28
- assert_equal 'expected at most 2 times', Cardinality.at_most(2).mocha_inspect
28
+ assert_equal 'expected at most twice', Cardinality.at_most(2).mocha_inspect
29
29
  assert_equal 'expected at most 3 times', Cardinality.at_most(3).mocha_inspect
30
30
 
31
31
  assert_equal 'expected at least once', Cardinality.at_least(1).mocha_inspect
32
- assert_equal 'expected at least 2 times', Cardinality.at_least(2).mocha_inspect
32
+ assert_equal 'expected at least twice', Cardinality.at_least(2).mocha_inspect
33
33
  assert_equal 'expected at least 3 times', Cardinality.at_least(3).mocha_inspect
34
34
 
35
35
  assert_equal 'expected never', Cardinality.exactly(0).mocha_inspect
36
36
  assert_equal 'expected exactly once', Cardinality.exactly(1).mocha_inspect
37
- assert_equal 'expected exactly 2 times', Cardinality.exactly(2).mocha_inspect
37
+ assert_equal 'expected exactly twice', Cardinality.exactly(2).mocha_inspect
38
38
  assert_equal 'expected exactly 3 times', Cardinality.times(3).mocha_inspect
39
39
 
40
40
  assert_equal 'expected between 2 and 4 times', Cardinality.times(2..4).mocha_inspect
@@ -21,7 +21,7 @@ class CentralTest < Test::Unit::TestCase
21
21
 
22
22
  stubba.stub(method)
23
23
 
24
- assert method.verified?
24
+ assert method.__verified__?
25
25
  end
26
26
 
27
27
  def test_should_not_stub_method_if_already_stubbed
@@ -34,7 +34,7 @@ class CentralTest < Test::Unit::TestCase
34
34
 
35
35
  stubba.stub(method)
36
36
 
37
- assert method.verified?
37
+ assert method.__verified__?
38
38
  end
39
39
 
40
40
  def test_should_record_method
@@ -58,8 +58,8 @@ class CentralTest < Test::Unit::TestCase
58
58
  stubba.unstub_all
59
59
 
60
60
  assert_equal [], stubba.stubba_methods
61
- assert method_1.verified?
62
- assert method_2.verified?
61
+ assert method_1.__verified__?
62
+ assert method_2.__verified__?
63
63
  end
64
64
 
65
65
  end
@@ -10,47 +10,47 @@ class ClassMethodTest < Test::Unit::TestCase
10
10
 
11
11
  def test_should_provide_hidden_version_of_method_name_starting_with_prefix
12
12
  method = ClassMethod.new(nil, :original_method_name)
13
- assert_match(/^__stubba__/, method.hidden_method)
13
+ assert_match(/^__stubba__/, method.hidden_method.to_s)
14
14
  end
15
15
 
16
16
  def test_should_provide_hidden_version_of_method_name_ending_with_suffix
17
17
  method = ClassMethod.new(nil, :original_method_name)
18
- assert_match(/__stubba__$/, method.hidden_method)
18
+ assert_match(/__stubba__$/, method.hidden_method.to_s)
19
19
  end
20
20
 
21
21
  def test_should_provide_hidden_version_of_method_name_including_original_method_name
22
22
  method = ClassMethod.new(nil, :original_method_name)
23
- assert_match(/original_method_name/, method.hidden_method)
23
+ assert_match(/original_method_name/, method.hidden_method.to_s)
24
24
  end
25
25
 
26
26
  def test_should_provide_hidden_version_of_method_name_substituting_question_mark
27
27
  method = ClassMethod.new(nil, :question_mark?)
28
- assert_no_match(/\?/, method.hidden_method)
29
- assert_match(/question_mark_substituted_character_63/, method.hidden_method)
28
+ assert_no_match(/\?/, method.hidden_method.to_s)
29
+ assert_match(/question_mark_substituted_character_63/, method.hidden_method.to_s)
30
30
  end
31
31
 
32
32
  def test_should_provide_hidden_version_of_method_name_substituting_exclamation_mark
33
33
  method = ClassMethod.new(nil, :exclamation_mark!)
34
- assert_no_match(/!/, method.hidden_method)
35
- assert_match(/exclamation_mark_substituted_character_33/, method.hidden_method)
34
+ assert_no_match(/!/, method.hidden_method.to_s)
35
+ assert_match(/exclamation_mark_substituted_character_33/, method.hidden_method.to_s)
36
36
  end
37
37
 
38
38
  def test_should_provide_hidden_version_of_method_name_substituting_equals_sign
39
39
  method = ClassMethod.new(nil, :equals_sign=)
40
- assert_no_match(/\=/, method.hidden_method)
41
- assert_match(/equals_sign_substituted_character_61/, method.hidden_method)
40
+ assert_no_match(/\=/, method.hidden_method.to_s)
41
+ assert_match(/equals_sign_substituted_character_61/, method.hidden_method.to_s)
42
42
  end
43
43
 
44
44
  def test_should_provide_hidden_version_of_method_name_substituting_brackets
45
45
  method = ClassMethod.new(nil, :[])
46
- assert_no_match(/\[\]/, method.hidden_method)
47
- assert_match(/substituted_character_91__substituted_character_93/, method.hidden_method)
46
+ assert_no_match(/\[\]/, method.hidden_method.to_s)
47
+ assert_match(/substituted_character_91__substituted_character_93/, method.hidden_method.to_s)
48
48
  end
49
49
 
50
50
  def test_should_provide_hidden_version_of_method_name_substituting_plus_sign
51
51
  method = ClassMethod.new(nil, :+)
52
- assert_no_match(/\+/, method.hidden_method)
53
- assert_match(/substituted_character_43/, method.hidden_method)
52
+ assert_no_match(/\+/, method.hidden_method.to_s)
53
+ assert_match(/substituted_character_43/, method.hidden_method.to_s)
54
54
  end
55
55
 
56
56
  def test_should_hide_original_method
@@ -95,7 +95,7 @@ class ClassMethodTest < Test::Unit::TestCase
95
95
  result = klass.method_x(:param1, :param2)
96
96
 
97
97
  assert_equal :result, result
98
- assert mocha.verified?
98
+ assert mocha.__verified__?
99
99
  end
100
100
 
101
101
  def test_should_remove_new_method
@@ -20,7 +20,7 @@ class ExpectationListTest < Test::Unit::TestCase
20
20
  expectation2 = Expectation.new(nil, :my_method).with(:argument3, :argument4)
21
21
  expectation_list.add(expectation1)
22
22
  expectation_list.add(expectation2)
23
- assert_same expectation2, expectation_list.detect(:my_method, :argument3, :argument4)
23
+ assert_same expectation1, expectation_list.match(:my_method, :argument1, :argument2)
24
24
  end
25
25
 
26
26
  def test_should_find_most_recent_matching_expectation
@@ -29,29 +29,29 @@ class ExpectationListTest < Test::Unit::TestCase
29
29
  expectation2 = Expectation.new(nil, :my_method).with(:argument1, :argument2)
30
30
  expectation_list.add(expectation1)
31
31
  expectation_list.add(expectation2)
32
- assert_same expectation2, expectation_list.detect(:my_method, :argument1, :argument2)
32
+ assert_same expectation2, expectation_list.match(:my_method, :argument1, :argument2)
33
33
  end
34
34
 
35
- def test_should_find_most_recent_matching_expectation_but_give_preference_to_those_allowing_invocations
35
+ def test_should_find_matching_expectation_allowing_invocation
36
36
  expectation_list = ExpectationList.new
37
- expectation1 = Expectation.new(nil, :my_method)
38
- expectation2 = Expectation.new(nil, :my_method)
37
+ expectation1 = Expectation.new(nil, :my_method).with(:argument1, :argument2)
38
+ expectation2 = Expectation.new(nil, :my_method).with(:argument3, :argument4)
39
39
  expectation1.define_instance_method(:invocations_allowed?) { true }
40
- expectation2.define_instance_method(:invocations_allowed?) { false }
40
+ expectation2.define_instance_method(:invocations_allowed?) { true }
41
41
  expectation_list.add(expectation1)
42
42
  expectation_list.add(expectation2)
43
- assert_same expectation1, expectation_list.detect(:my_method)
43
+ assert_same expectation1, expectation_list.match_allowing_invocation(:my_method, :argument1, :argument2)
44
44
  end
45
45
 
46
- def test_should_find_most_recent_matching_expectation_if_no_matching_expectations_allow_invocations
46
+ def test_should_find_most_recent_matching_expectation_allowing_invocation
47
47
  expectation_list = ExpectationList.new
48
48
  expectation1 = Expectation.new(nil, :my_method)
49
49
  expectation2 = Expectation.new(nil, :my_method)
50
- expectation1.define_instance_method(:invocations_allowed?) { false }
50
+ expectation1.define_instance_method(:invocations_allowed?) { true }
51
51
  expectation2.define_instance_method(:invocations_allowed?) { false }
52
52
  expectation_list.add(expectation1)
53
53
  expectation_list.add(expectation2)
54
- assert_same expectation2, expectation_list.detect(:my_method)
54
+ assert_same expectation1, expectation_list.match_allowing_invocation(:my_method)
55
55
  end
56
-
56
+
57
57
  end
@@ -262,7 +262,7 @@ class ExpectationTest < Test::Unit::TestCase
262
262
  def test_should_not_verify_successfully_if_expected_call_was_not_made_at_least_once
263
263
  expectation = new_expectation.with(1, 2, 3).at_least_once
264
264
  assert !expectation.verified?
265
- assert_match(/expected at least once, never invoked/i, expectation.mocha_inspect)
265
+ assert_match(/expected at least once, not yet invoked/i, expectation.mocha_inspect)
266
266
  end
267
267
 
268
268
  def test_should_verify_successfully_if_expected_call_was_made_expected_number_of_times
@@ -275,7 +275,7 @@ class ExpectationTest < Test::Unit::TestCase
275
275
  expectation = new_expectation.times(2)
276
276
  1.times {expectation.invoke}
277
277
  assert !expectation.verified?
278
- assert_match(/expected exactly 2 times, already invoked 1 time/i, expectation.mocha_inspect)
278
+ assert_match(/expected exactly twice, already invoked once/i, expectation.mocha_inspect)
279
279
  end
280
280
 
281
281
  def test_should_not_verify_successfully_if_expected_call_was_made_too_many_times
@@ -54,7 +54,7 @@ class MockTest < Test::Unit::TestCase
54
54
  mock = Mock.new
55
55
  OBJECT_METHODS.each { |method| mock.__expects__(method.to_sym).returns(method) }
56
56
  OBJECT_METHODS.each { |method| assert_equal method, mock.__send__(method.to_sym) }
57
- assert mock.verified?
57
+ assert mock.__verified__?
58
58
  end
59
59
 
60
60
  def test_should_be_able_to_stub_standard_object_methods
@@ -113,30 +113,18 @@ class MockTest < Test::Unit::TestCase
113
113
  error = assert_raise(ExpectationError) do
114
114
  mock.unexpected_method_called(:my_method, :argument1, :argument2)
115
115
  end
116
+ assert_match(/unexpected invocation/, error.message)
116
117
  assert_match(/my_method/, error.message)
117
118
  assert_match(/argument1/, error.message)
118
119
  assert_match(/argument2/, error.message)
119
120
  end
120
121
 
121
- def test_should_indicate_unexpected_method_called
122
- mock = Mock.new
123
- class << mock
124
- attr_accessor :symbol, :arguments
125
- def unexpected_method_called(symbol, *arguments)
126
- self.symbol, self.arguments = symbol, arguments
127
- end
128
- end
129
- mock.my_method(:argument1, :argument2)
130
- assert_equal :my_method, mock.symbol
131
- assert_equal [:argument1, :argument2], mock.arguments
132
- end
133
-
134
122
  def test_should_not_verify_successfully_because_not_all_expectations_have_been_satisfied
135
123
  mock = Mock.new
136
124
  mock.expects(:method1)
137
125
  mock.expects(:method2)
138
126
  mock.method1
139
- assert !mock.verified?
127
+ assert !mock.__verified__?
140
128
  end
141
129
 
142
130
  def test_should_increment_assertion_counter_for_every_verified_expectation
@@ -150,7 +138,7 @@ class MockTest < Test::Unit::TestCase
150
138
 
151
139
  assertion_counter = SimpleCounter.new
152
140
 
153
- mock.verified?(assertion_counter)
141
+ mock.__verified__?(assertion_counter)
154
142
 
155
143
  assert_equal 2, assertion_counter.count
156
144
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mocha
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.0
4
+ version: 0.9.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - James Mead
@@ -9,7 +9,7 @@ autorequire: mocha
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2008-06-24 00:00:00 +01:00
12
+ date: 2008-08-30 00:00:00 +01:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -88,12 +88,15 @@ files:
88
88
  - lib/mocha/state_machine.rb
89
89
  - lib/mocha/stubbing_error.rb
90
90
  - lib/mocha/test_case_adapter.rb
91
+ - lib/mocha/unexpected_invocation.rb
91
92
  - lib/mocha/yield_parameters.rb
92
93
  - lib/mocha.rb
93
94
  - lib/mocha_standalone.rb
94
95
  - lib/stubba.rb
95
96
  - test/acceptance/acceptance_test_helper.rb
96
97
  - test/acceptance/bug_18914_test.rb
98
+ - test/acceptance/bug_21465_test.rb
99
+ - test/acceptance/bug_21563_test.rb
97
100
  - test/acceptance/expected_invocation_count_test.rb
98
101
  - test/acceptance/failure_messages_test.rb
99
102
  - test/acceptance/mocha_example_test.rb
@@ -115,6 +118,7 @@ files:
115
118
  - test/acceptance/stub_module_method_test.rb
116
119
  - test/acceptance/stub_test.rb
117
120
  - test/acceptance/stubba_example_test.rb
121
+ - test/acceptance/stubba_test.rb
118
122
  - test/acceptance/stubba_test_result_test.rb
119
123
  - test/acceptance/stubbing_error_backtrace_test.rb
120
124
  - test/acceptance/stubbing_method_unnecessarily_test.rb