mocha 0.5.1 → 0.5.2

Sign up to get free protection for your applications and to get access to all the features.
data/COPYING CHANGED
@@ -1,3 +1,3 @@
1
1
  Copyright Revieworld Ltd. 2006
2
-
2
+
3
3
  You may use, copy and redistribute this library under the same terms as Ruby itself (see http://www.ruby-lang.org/en/LICENSE.txt) or under the MIT license (see MIT-LICENSE file).
data/README CHANGED
@@ -31,5 +31,5 @@ Or download Mocha from here - http://rubyforge.org/projects/mocha
31
31
  == License
32
32
 
33
33
  Copyright Revieworld Ltd. 2006
34
-
34
+
35
35
  You may use, copy and redistribute this library under the same terms as {Ruby itself}[http://www.ruby-lang.org/en/LICENSE.txt] or under the {MIT license}[http://mocha.rubyforge.org/files/MIT-LICENSE.html].
data/RELEASE CHANGED
@@ -1,4 +1,77 @@
1
- = 0.4.0
1
+ = 0.5.2 (r159)
2
+
3
+ - Fix bug 11885 - "never doesn't work with stub_everything" submitted by Alexander Lang. In fixing this bug, also fixed undiscoverd bug where expected & actual invocation counts were being incorrectly reported which seems to have been introduced when fixes were added for invocation dispatch (see MockedMethodDispatchAcceptanceTest).
4
+ - Previously when an expectation did not allow more invocations, it was treated as not matching. Now we prefer matching expectations which allow more invocations, but still match expectations which cannot allow more invocations. I think this may be overcomplicating things, but let's see how it goes.
5
+
6
+ = 0.5.1 (r149)
7
+
8
+ - Fixed bug #11583 "Mocha 0.5.0 throwing unexpected warnings". Also switched on ruby warning for all rake test tasks. Fixed majority of warnings, but some left to fix.
9
+
10
+ = 0.5.0 (r147)
11
+
12
+ - Parameter Matchers - I’ve added a few Hamcrest-style parameter matchers which are designed to be used inside Expectation#with. The following matchers are currently available: anything(), includes(), has_key(), has_value(), has_entry(), all_of() & any_of(). More to follow soon. The idea is eventually to get rid of the nasty parameter_block option on Expectation#with.
13
+
14
+ object = mock()
15
+ object.expects(:method).with(has_key('key_1'))
16
+ object.method('key_1' => 1, 'key_2' => 2)
17
+ # no verification error raised
18
+
19
+ object = mock()
20
+ object.expects(:method).with(has_key('key_1'))
21
+ object.method('key_2' => 2)
22
+ # verification error raised, because method was not called with Hash containing key: 'key_1'
23
+
24
+ - Values Returned and Exceptions Raised on Consecutive Invocations - Allow multiple calls to Expectation#returns and Expectation#raises to build up a sequence of responses to invocations on the mock. Added syntactic sugar method Expectation#then to allow more readable expectations.
25
+
26
+ object = mock()
27
+ object.stubs(:method).returns(1, 2).then.raises(Exception).then.returns(4)
28
+ object.method # => 1
29
+ object.method # => 2
30
+ object.method # => raises exception of class Exception
31
+ object.method # => 4
32
+
33
+ - Yields on Consecutive Invocations - Allow multiple calls to yields on single expectation to allow yield parameters to be specified for consecutive invocations.
34
+
35
+ object = mock()
36
+ object.stubs(:method).yields(1, 2).then.yields(3)
37
+ object.method { |*values| p values } # => [1, 2]
38
+ object.method { |*values| p values } # => [3]
39
+
40
+ - Multiple Yields on Single Invocation - Added Expectation#multiple_yields to allow a mocked or stubbed method to yield multiple times for a single invocation.
41
+
42
+ object = mock()
43
+ object.stubs(:method).multiple_yields([1, 2], [3])
44
+ object.method { |*values| p values } # => [1, 2] # => [3]
45
+
46
+ - Invocation Dispatch - Expectations were already being matched in reverse order i.e. the most recently defined one was being found first. This is still the case, but we now stop matching an expectation when its maximum number of expected invocations is reached. c.f. JMock v1. A stub will never stop matching by default. Hopefully this means we can soon get rid of the need to pass a Proc to Expectation#returns.
47
+
48
+ object = mock()
49
+ object.stubs(:method).returns(2)
50
+ object.expects(:method).once.returns(1)
51
+ object.method # => 1
52
+ object.method # => 2
53
+ object.method # => 2
54
+ # no verification error raised
55
+
56
+ # The following should still work...
57
+
58
+ Time.stubs(:now).returns(Time.parse('Mon Jan 01 00:00:00 UTC 2007'))
59
+ Time.now # => Mon Jan 01 00:00:00 UTC 2007
60
+ Time.stubs(:now).returns(Time.parse('Thu Feb 01 00:00:00 UTC 2007'))
61
+ Time.now # => Thu Feb 01 00:00:00 UTC 2007
62
+
63
+ - Deprecate passing an instance of Proc to Expectation#returns.
64
+ - Explicitly include all Rakefile dependencies in project.
65
+ - Fixed old Stubba example.
66
+ - Fix so that it is possible for a stubbed method to raise an Interrupt exception without a message in Ruby 1.8.6
67
+ - Added responds_like and quacks_like.
68
+ - Capture standard object methods before Mocha adds any.
69
+ - Added Expectation#once method to make interface less surprising.
70
+ - Use Rake::TestTask to run tests. Created three separate tasks to run unit, integration & acceptance tests. Split inspect_test into one file per TestCase. Deleted superfluous all_tests file.
71
+ - Fiddled with mocha_inspect and tests to give more sensible results on x86 platform.
72
+ - Fixed bug #7834 "infinite_range.rb makes incorrect assumption about to_f" logged by James Moore.
73
+
74
+ = 0.4.0 (r92)
2
75
 
3
76
  - Allow naming of mocks (patch from Chris Roos).
4
77
  - Specify multiple return values for consecutive calls.
data/Rakefile CHANGED
@@ -5,7 +5,7 @@ require 'rake/testtask'
5
5
  require 'rake/contrib/sshpublisher'
6
6
 
7
7
  module Mocha
8
- VERSION = "0.5.1"
8
+ VERSION = "0.5.2"
9
9
  end
10
10
 
11
11
  desc "Run all tests"
@@ -93,14 +93,14 @@ end
93
93
  Gem::manage_gems
94
94
 
95
95
  specification = Gem::Specification.new do |s|
96
- s.name = "mocha"
96
+ s.name = "mocha"
97
97
  s.summary = "Mocking and stubbing library"
98
- s.version = Mocha::VERSION
99
- s.author = 'James Mead'
100
- s.description = <<-EOF
98
+ s.version = Mocha::VERSION
99
+ s.author = 'James Mead'
100
+ s.description = <<-EOF
101
101
  Mocking and stubbing library with JMock/SchMock syntax, which allows mocking and stubbing of methods on real (non-mock) classes.
102
102
  EOF
103
- s.email = 'mocha-developer@rubyforge.org'
103
+ s.email = 'mocha-developer@rubyforge.org'
104
104
  s.homepage = 'http://mocha.rubyforge.org'
105
105
  s.rubyforge_project = 'mocha'
106
106
 
@@ -113,8 +113,8 @@ specification = Gem::Specification.new do |s|
113
113
  end
114
114
 
115
115
  Rake::GemPackageTask.new(specification) do |package|
116
- package.need_zip = true
117
- package.need_tar = true
116
+ package.need_zip = true
117
+ package.need_tar = true
118
118
  end
119
119
 
120
120
  task :verify_user do
@@ -36,7 +36,10 @@ module Mocha # :nodoc:
36
36
  # end
37
37
  def mock(*args)
38
38
  name, expectations = name_and_expectations_from_args(args)
39
- build_mock_with_expectations(:expects, expectations, name)
39
+ mock = Mock.new(false, name)
40
+ mock.expects(expectations)
41
+ mocks << mock
42
+ mock
40
43
  end
41
44
 
42
45
  # :call-seq: stub(name) -> mock object
@@ -58,7 +61,10 @@ module Mocha # :nodoc:
58
61
  # end
59
62
  def stub(*args)
60
63
  name, expectations = name_and_expectations_from_args(args)
61
- build_mock_with_expectations(:stubs, expectations, name)
64
+ mock = Mock.new(false, name)
65
+ mock.stubs(expectations)
66
+ mocks << mock
67
+ mock
62
68
  end
63
69
 
64
70
  # :call-seq: stub_everything(name) -> mock object
@@ -78,7 +84,10 @@ module Mocha # :nodoc:
78
84
  # end
79
85
  def stub_everything(*args)
80
86
  name, expectations = name_and_expectations_from_args(args)
81
- build_mock_with_expectations(:stub_everything, expectations, name)
87
+ mock = Mock.new(true, name)
88
+ mock.stubs(expectations)
89
+ mocks << mock
90
+ mock
82
91
  end
83
92
 
84
93
  def verify_mocks # :nodoc:
@@ -89,17 +98,6 @@ module Mocha # :nodoc:
89
98
  reset_mocks
90
99
  end
91
100
 
92
- def build_mock_with_expectations(expectation_type = :expects, expectations = {}, name = nil) # :nodoc:
93
- stub_everything = (expectation_type == :stub_everything)
94
- expectation_type = :stubs if expectation_type == :stub_everything
95
- mock = Mocha::Mock.new(stub_everything, name)
96
- expectations.each do |method, result|
97
- mock.__send__(expectation_type, method).returns(result)
98
- end
99
- mocks << mock
100
- mock
101
- end
102
-
103
101
  private
104
102
 
105
103
  def name_and_expectations_from_args(args) # :nodoc:
@@ -28,6 +28,7 @@ module Mocha # :nodoc:
28
28
  @invoked_count, @return_values = 0, ReturnValues.new
29
29
  @backtrace = backtrace || caller
30
30
  @yield_parameters = YieldParameters.new
31
+ @final_expectation = false
31
32
  end
32
33
 
33
34
  def match?(method_name, *arguments)
@@ -37,12 +38,15 @@ module Mocha # :nodoc:
37
38
  else
38
39
  return false unless (@parameters == arguments)
39
40
  end
41
+ return true
42
+ end
43
+
44
+ def invocations_allowed?
40
45
  if @expected_count.is_a?(Range) then
41
- return false unless @invoked_count < @expected_count.last
46
+ @invoked_count < @expected_count.last
42
47
  else
43
- return false unless @invoked_count < @expected_count
48
+ @invoked_count < @expected_count
44
49
  end
45
- return true
46
50
  end
47
51
 
48
52
  # :startdoc:
@@ -0,0 +1,45 @@
1
+ module Mocha # :nodoc:
2
+
3
+ class ExpectationList
4
+
5
+ def initialize
6
+ @expectations = []
7
+ end
8
+
9
+ def add(expectation)
10
+ @expectations << expectation
11
+ end
12
+
13
+ def respond_to?(method_name)
14
+ @expectations.any? { |expectation| expectation.method_name == method_name }
15
+ end
16
+
17
+ def detect(method_name, *arguments)
18
+ expectations = @expectations.reverse.select { |expectation| expectation.match?(method_name, *arguments) }
19
+ expectation = expectations.detect { |expectation| expectation.invocations_allowed? }
20
+ expectation || expectations.first
21
+ end
22
+
23
+ def similar(method_name)
24
+ @expectations.select { |expectation| expectation.method_name == method_name }
25
+ end
26
+
27
+ def verify(&block)
28
+ @expectations.each { |expectation| expectation.verify(&block) }
29
+ end
30
+
31
+ def to_a
32
+ @expectations
33
+ end
34
+
35
+ def to_set
36
+ @expectations.to_set
37
+ end
38
+
39
+ def size
40
+ @expectations.size
41
+ end
42
+
43
+ end
44
+
45
+ end
@@ -11,15 +11,15 @@ module Mocha # :nodoc:
11
11
 
12
12
  def verify
13
13
  msg = error_message(0, 1)
14
- similar_expectations_list = similar_expectations.collect { |expectation| expectation.method_signature }.join("\n")
15
- msg << "\nSimilar expectations:\n#{similar_expectations_list}" unless similar_expectations.empty?
14
+ similar_expectations_list = @mock.similar_expectations(@method_name).collect { |expectation| expectation.method_signature }.join("\n")
15
+ msg << "\nSimilar expectations:\n#{similar_expectations_list}" unless similar_expectations_list.empty?
16
16
  error = ExpectationError.new(msg)
17
17
  error.set_backtrace(filtered_backtrace)
18
18
  raise error if @invoked_count
19
19
  end
20
20
 
21
21
  def similar_expectations
22
- @mock.expectations.select { |expectation| expectation.method_name == self.method_name }
22
+ @mock.similar_expectations(method_name)
23
23
  end
24
24
 
25
25
  end
@@ -1,9 +1,12 @@
1
1
  require 'mocha/expectation'
2
+ require 'mocha/expectation_list'
2
3
  require 'mocha/stub'
3
4
  require 'mocha/missing_expectation'
4
5
  require 'mocha/metaclass'
5
6
 
6
7
  module Mocha # :nodoc:
8
+
9
+ class ExpectationSequenceError < RuntimeError; end
7
10
 
8
11
  # Traditional mock object.
9
12
  #
@@ -15,7 +18,7 @@ module Mocha # :nodoc:
15
18
  def initialize(stub_everything = false, name = nil)
16
19
  @stub_everything = stub_everything
17
20
  @mock_name = name
18
- @expectations = []
21
+ @expectations = ExpectationList.new
19
22
  @responder = nil
20
23
  end
21
24
 
@@ -142,7 +145,7 @@ module Mocha # :nodoc:
142
145
  alias_method :quacks_like, :responds_like
143
146
 
144
147
  def add_expectation(expectation)
145
- @expectations << expectation
148
+ @expectations.add(expectation)
146
149
  method_name = expectation.method_name
147
150
  self.__metaclass__.send(:undef_method, method_name) if self.__metaclass__.method_defined?(method_name)
148
151
  expectation
@@ -152,42 +155,30 @@ module Mocha # :nodoc:
152
155
  if @responder and not @responder.respond_to?(symbol)
153
156
  raise NoMethodError, "undefined method `#{symbol}' for #{self.mocha_inspect} which responds like #{@responder.mocha_inspect}"
154
157
  end
155
- matching_expectation = matching_expectation(symbol, *arguments)
158
+ matching_expectation = @expectations.detect(symbol, *arguments)
156
159
  if matching_expectation then
157
160
  matching_expectation.invoke(&block)
158
161
  elsif stub_everything then
159
162
  return
160
163
  else
161
- begin
162
- super_method_missing(symbol, *arguments, &block)
163
- rescue NoMethodError
164
- unexpected_method_called(symbol, *arguments)
165
- end
166
- end
167
- end
168
-
169
- def respond_to?(symbol)
170
- if @responder then
171
- @responder.respond_to?(symbol)
164
+ unexpected_method_called(symbol, *arguments)
165
+ end
166
+ end
167
+
168
+ def respond_to?(symbol)
169
+ if @responder then
170
+ @responder.respond_to?(symbol)
172
171
  else
173
- @expectations.any? { |expectation| expectation.method_name == symbol }
174
- end
175
- end
176
-
177
- def super_method_missing(symbol, *arguments, &block)
178
- raise NoMethodError
172
+ @expectations.respond_to?(symbol)
173
+ end
179
174
  end
180
-
181
- def unexpected_method_called(symbol, *arguments)
175
+
176
+ def unexpected_method_called(symbol, *arguments)
182
177
  MissingExpectation.new(self, symbol).with(*arguments).verify
183
178
  end
184
-
185
- def matching_expectation(symbol, *arguments)
186
- @expectations.reverse.detect { |expectation| expectation.match?(symbol, *arguments) }
187
- end
188
179
 
189
180
  def verify(&block)
190
- @expectations.each { |expectation| expectation.verify(&block) }
181
+ @expectations.verify(&block)
191
182
  end
192
183
 
193
184
  def mocha_inspect
@@ -200,8 +191,12 @@ module Mocha # :nodoc:
200
191
  mocha_inspect
201
192
  end
202
193
 
194
+ def similar_expectations(method_name)
195
+ @expectations.similar(method_name)
196
+ end
197
+
203
198
  # :startdoc:
204
199
 
205
200
  end
206
201
 
207
- end
202
+ end
@@ -0,0 +1,187 @@
1
+ require File.join(File.dirname(__FILE__), "..", "test_helper")
2
+ require 'mocha'
3
+ require 'test_runner'
4
+
5
+ class ExpectedInvocationCountAcceptanceTest < Test::Unit::TestCase
6
+
7
+ include TestRunner
8
+
9
+ def test_should_pass_if_method_is_never_expected_and_is_never_called
10
+ test_result = run_test do
11
+ mock = mock('mock')
12
+ mock.expects(:method).never
13
+ 0.times { mock.method }
14
+ end
15
+ assert_passed(test_result)
16
+ end
17
+
18
+ def test_should_fail_if_method_is_never_expected_but_is_called_once
19
+ test_result = run_test do
20
+ mock = mock('mock')
21
+ mock.expects(:method).never
22
+ 1.times { mock.method }
23
+ end
24
+ assert_failed(test_result)
25
+ failure_messages = test_result.failures.map { |failure| failure.message }
26
+ assert_equal ['#<Mock:mock>.method - expected calls: 0, actual calls: 1'], failure_messages
27
+ end
28
+
29
+ def test_should_pass_if_method_is_expected_twice_and_is_called_twice
30
+ test_result = run_test do
31
+ mock = mock('mock')
32
+ mock.expects(:method).times(2)
33
+ 2.times { mock.method }
34
+ end
35
+ assert_passed(test_result)
36
+ end
37
+
38
+ def test_should_fail_if_method_is_expected_twice_but_is_called_once
39
+ test_result = run_test do
40
+ mock = mock('mock')
41
+ mock.expects(:method).times(2)
42
+ 1.times { mock.method }
43
+ end
44
+ assert_failed(test_result)
45
+ failure_messages = test_result.failures.map { |failure| failure.message }
46
+ assert_equal ['#<Mock:mock>.method - expected calls: 2, actual calls: 1'], failure_messages
47
+ end
48
+
49
+ def test_should_fail_if_method_is_expected_twice_but_is_called_three_times
50
+ test_result = run_test do
51
+ mock = mock('mock')
52
+ mock.expects(:method).times(2)
53
+ 3.times { mock.method }
54
+ end
55
+ assert_failed(test_result)
56
+ failure_messages = test_result.failures.map { |failure| failure.message }
57
+ assert_equal ['#<Mock:mock>.method - expected calls: 2, actual calls: 3'], failure_messages
58
+ end
59
+
60
+ def test_should_pass_if_method_is_expected_between_two_and_four_times_and_is_called_twice
61
+ test_result = run_test do
62
+ mock = mock('mock')
63
+ mock.expects(:method).times(2..4)
64
+ 2.times { mock.method }
65
+ end
66
+ assert_passed(test_result)
67
+ end
68
+
69
+ def test_should_pass_if_method_is_expected_between_two_and_four_times_and_is_called_three_times
70
+ test_result = run_test do
71
+ mock = mock('mock')
72
+ mock.expects(:method).times(2..4)
73
+ 3.times { mock.method }
74
+ end
75
+ assert_passed(test_result)
76
+ end
77
+
78
+ def test_should_pass_if_method_is_expected_between_two_and_four_times_and_is_called_four_times
79
+ test_result = run_test do
80
+ mock = mock('mock')
81
+ mock.expects(:method).times(2..4)
82
+ 4.times { mock.method }
83
+ end
84
+ assert_passed(test_result)
85
+ end
86
+
87
+ def test_should_fail_if_method_is_expected_between_two_and_four_times_and_is_called_once
88
+ test_result = run_test do
89
+ mock = mock('mock')
90
+ mock.expects(:method).times(2..4)
91
+ 1.times { mock.method }
92
+ end
93
+ assert_failed(test_result)
94
+ failure_messages = test_result.failures.map { |failure| failure.message }
95
+ assert_equal ['#<Mock:mock>.method - expected calls: 2..4, actual calls: 1'], failure_messages
96
+ end
97
+
98
+ def test_should_fail_if_method_is_expected_between_two_and_four_times_and_is_called_five_times
99
+ test_result = run_test do
100
+ mock = mock('mock')
101
+ mock.expects(:method).times(2..4)
102
+ 5.times { mock.method }
103
+ end
104
+ assert_failed(test_result)
105
+ failure_messages = test_result.failures.map { |failure| failure.message }
106
+ assert_equal ['#<Mock:mock>.method - expected calls: 2..4, actual calls: 5'], failure_messages
107
+ end
108
+
109
+ def test_should_pass_if_method_is_expected_at_least_once_and_is_called_once
110
+ test_result = run_test do
111
+ mock = mock('mock')
112
+ mock.expects(:method).at_least_once
113
+ 1.times { mock.method }
114
+ end
115
+ assert_passed(test_result)
116
+ end
117
+
118
+ def test_should_pass_if_method_is_expected_at_least_once_and_is_called_twice
119
+ test_result = run_test do
120
+ mock = mock('mock')
121
+ mock.expects(:method).at_least_once
122
+ 2.times { mock.method }
123
+ end
124
+ assert_passed(test_result)
125
+ end
126
+
127
+ def test_should_fail_if_method_is_expected_at_least_once_but_is_never_called
128
+ test_result = run_test do
129
+ mock = mock('mock')
130
+ mock.expects(:method).at_least_once
131
+ 0.times { mock.method }
132
+ end
133
+ assert_failed(test_result)
134
+ failure_messages = test_result.failures.map { |failure| failure.message }
135
+ assert_equal ['#<Mock:mock>.method - expected calls: at least 1, actual calls: 0'], failure_messages
136
+ end
137
+
138
+ def test_should_pass_if_method_is_expected_at_most_once_and_is_never_called
139
+ test_result = run_test do
140
+ mock = mock('mock')
141
+ mock.expects(:method).at_most_once
142
+ 0.times { mock.method }
143
+ end
144
+ assert_passed(test_result)
145
+ end
146
+
147
+ def test_should_pass_if_method_is_expected_at_most_once_and_called_once
148
+ test_result = run_test do
149
+ mock = mock('mock')
150
+ mock.expects(:method).at_most_once
151
+ 1.times { mock.method }
152
+ end
153
+ assert_passed(test_result)
154
+ end
155
+
156
+ def test_should_fail_if_method_is_expected_at_most_once_but_is_called_twice
157
+ test_result = run_test do
158
+ mock = mock('mock')
159
+ mock.expects(:method).at_most_once
160
+ 2.times { mock.method }
161
+ end
162
+ assert_failed(test_result)
163
+ failure_messages = test_result.failures.map { |failure| failure.message }
164
+ assert_equal ['#<Mock:mock>.method - expected calls: at most 1, actual calls: 2'], failure_messages
165
+ end
166
+
167
+ def test_should_pass_if_method_is_never_expected_and_is_never_called_even_if_everything_is_stubbed
168
+ test_result = run_test do
169
+ stub = stub_everything('stub')
170
+ stub.expects(:method).never
171
+ 0.times { stub.method }
172
+ end
173
+ assert_passed(test_result)
174
+ end
175
+
176
+ def test_should_fail_if_method_is_never_expected_but_is_called_once_even_if_everything_is_stubbed
177
+ test_result = run_test do
178
+ stub = stub_everything('stub')
179
+ stub.expects(:method).never
180
+ 1.times { stub.method }
181
+ end
182
+ assert_failed(test_result)
183
+ failure_messages = test_result.failures.map { |failure| failure.message }
184
+ assert_equal ['#<Mock:stub>.method - expected calls: 0, actual calls: 1'], failure_messages
185
+ end
186
+
187
+ end
@@ -54,8 +54,7 @@ class MockedMethodDispatchAcceptanceTest < Test::Unit::TestCase
54
54
  assert_passed(test_result)
55
55
  end
56
56
 
57
- def test_should
58
- # flunk
57
+ def test_should_find_latest_expectation_with_range_of_expected_invocation_count_which_has_not_stopped_matching
59
58
  test_result = run_test do
60
59
  mock = mock()
61
60
  mock.stubs(:method).returns(1)
@@ -14,84 +14,63 @@ class AutoVerifyTest < Test::Unit::TestCase
14
14
  end
15
15
  end
16
16
 
17
- def test_should_add_mock_type_expectations
18
- test_case.define_instance_accessor(:expectation_type, :expectations)
19
- test_case.define_instance_method(:build_mock_with_expectations) do |expectation_type, expectations|
20
- self.expectation_type = expectation_type
21
- self.expectations = expectations
22
- end
23
- expectations = { :method1 => :value1, :method2 => :value2 }
24
-
25
- test_case.mock(expectations)
26
-
27
- assert_equal :expects, test_case.expectation_type
28
- assert_equal expectations, test_case.expectations
29
- end
30
-
31
- def test_should_add_stub_type_expectations
32
- test_case.define_instance_accessor(:expectation_type, :expectations)
33
- test_case.define_instance_method(:build_mock_with_expectations) do |expectation_type, expectations|
34
- self.expectation_type = expectation_type
35
- self.expectations = expectations
36
- end
37
- expectations = { :method1 => :value1, :method2 => :value2 }
38
-
39
- test_case.stub(expectations)
40
-
41
- assert_equal :stubs, test_case.expectation_type
42
- assert_equal expectations, test_case.expectations
43
- end
44
-
45
- def test_should_add_greedy_stub_type_expectations
46
- test_case.define_instance_accessor(:expectation_type, :expectations)
47
- test_case.define_instance_method(:build_mock_with_expectations) do |expectation_type, expectations|
48
- self.expectation_type = expectation_type
49
- self.expectations = expectations
50
- end
51
- expectations = { :method1 => :value1, :method2 => :value2 }
52
-
53
- test_case.stub_everything(expectations)
54
-
55
- assert_equal :stub_everything, test_case.expectation_type
56
- assert_equal expectations, test_case.expectations
57
- end
58
-
59
17
  def test_should_build_mock
60
- mock = test_case.build_mock_with_expectations
18
+ mock = test_case.mock
61
19
  assert mock.is_a?(Mocha::Mock)
62
20
  end
63
21
 
64
- def test_should_add_expectation_to_mock
65
- mock = test_case.build_mock_with_expectations(:expects, :expected_method => 'return_value')
66
- assert_equal 'return_value', mock.expected_method
22
+ def test_should_add_expectations_to_mock
23
+ mock = test_case.mock(:method_1 => 'result_1', :method_2 => 'result_2')
24
+ assert_equal 'result_1', mock.method_1
25
+ assert_equal 'result_2', mock.method_2
67
26
  end
68
27
 
69
28
  def test_should_build_stub
70
- stub = test_case.build_mock_with_expectations(:stubs)
29
+ stub = test_case.stub
71
30
  assert stub.is_a?(Mocha::Mock)
72
31
  end
73
32
 
74
33
  def test_should_add_expectation_to_stub
75
- stub = test_case.build_mock_with_expectations(:stubs, :stubbed_method => 'return_value')
76
- assert_equal 'return_value', stub.stubbed_method
34
+ stub = test_case.stub(:method_1 => 'result_1', :method_2 => 'result_2')
35
+ assert_equal 'result_1', stub.method_1
36
+ assert_equal 'result_2', stub.method_2
77
37
  end
78
38
 
79
- def test_should_build_greedy_stub
80
- greedy_stub = test_case.build_mock_with_expectations(:stub_everything)
81
- assert greedy_stub.stub_everything
39
+ def test_should_build_stub_that_stubs_all_methods
40
+ stub = test_case.stub_everything
41
+ assert stub.stub_everything
82
42
  end
83
43
 
84
- def test_should_add_expectations_to_greedy_stub
85
- greedy_mock = test_case.build_mock_with_expectations(:stub_everything, :stubbed_method => 'return_value')
86
- assert_equal 'return_value', greedy_mock.stubbed_method
44
+ def test_should_add_expectations_to_stub_that_stubs_all_methods
45
+ stub = test_case.stub_everything(:method_1 => 'result_1', :method_2 => 'result_2')
46
+ assert_equal 'result_1', stub.method_1
47
+ assert_equal 'result_2', stub.method_2
87
48
  end
88
49
 
89
- def test_should_build_new_mock_each_time
90
- assert_not_equal test_case.build_mock_with_expectations, test_case.build_mock_with_expectations
50
+ def test_should_always_new_mock
51
+ assert_not_equal test_case.mock, test_case.mock
52
+ end
53
+
54
+ def test_should_always_build_new_stub
55
+ assert_not_equal test_case.stub, test_case.stub
56
+ end
57
+
58
+ def test_should_always_build_new_stub_that_stubs_all_methods
59
+ assert_not_equal test_case.stub, test_case.stub
91
60
  end
92
61
 
93
62
  def test_should_store_each_new_mock
94
- expected = Array.new(3) { test_case.build_mock_with_expectations }
63
+ expected = Array.new(3) { test_case.mock }
64
+ assert_equal expected, test_case.mocks
65
+ end
66
+
67
+ def test_should_store_each_new_stub
68
+ expected = Array.new(3) { test_case.stub }
69
+ assert_equal expected, test_case.mocks
70
+ end
71
+
72
+ def test_should_store_each_new_stub_that_stubs_all_methods
73
+ expected = Array.new(3) { test_case.stub_everything }
95
74
  assert_equal expected, test_case.mocks
96
75
  end
97
76
 
@@ -125,39 +104,19 @@ class AutoVerifyTest < Test::Unit::TestCase
125
104
  assert test_case.mocks.empty?
126
105
  end
127
106
 
128
- def test_should_stub_everything
129
- mock = test_case.stub_everything
130
- assert_equal true, mock.stub_everything
131
- end
132
-
133
- def test_should_add_mock_to_mocks
134
- mock = test_case.mock
135
- assert_equal [mock], test_case.mocks
136
- end
137
-
138
- def test_should_add_stub_to_mocks
139
- stub = test_case.stub
140
- assert_equal [stub], test_case.mocks
141
- end
142
-
143
- def test_should_add_greedy_stub_to_mocks
144
- greedy_stub = test_case.stub_everything
145
- assert_equal [greedy_stub], test_case.mocks
146
- end
147
-
148
- def test_should_create_mock_with_name
107
+ def test_should_create_named_mock
149
108
  mock = test_case.mock('named_mock')
150
109
  assert_equal '#<Mock:named_mock>', mock.mocha_inspect
151
110
  end
152
111
 
153
- def test_should_create_stub_with_name
112
+ def test_should_create_named_stub
154
113
  stub = test_case.stub('named_stub')
155
114
  assert_equal '#<Mock:named_stub>', stub.mocha_inspect
156
115
  end
157
116
 
158
- def test_should_create_greedy_stub_with_name
159
- greedy_stub = test_case.stub_everything('named_greedy_stub')
160
- assert_equal '#<Mock:named_greedy_stub>', greedy_stub.mocha_inspect
117
+ def test_should_create_named_stub_that_stubs_all_methods
118
+ stub = test_case.stub_everything('named_stub')
119
+ assert_equal '#<Mock:named_stub>', stub.mocha_inspect
161
120
  end
162
121
 
163
122
  end
@@ -0,0 +1,69 @@
1
+ require File.join(File.dirname(__FILE__), "..", "test_helper")
2
+ require 'mocha/expectation_list'
3
+ require 'mocha/expectation'
4
+ require 'set'
5
+ require 'method_definer'
6
+
7
+ class ExpectationListTest < Test::Unit::TestCase
8
+
9
+ include Mocha
10
+
11
+ def test_should_find_matching_expectation
12
+ expectation_list = ExpectationList.new
13
+ expectation1 = Expectation.new(nil, :my_method).with(:argument1, :argument2)
14
+ expectation2 = Expectation.new(nil, :my_method).with(:argument3, :argument4)
15
+ expectation_list.add(expectation1)
16
+ expectation_list.add(expectation2)
17
+ assert_same expectation2, expectation_list.detect(:my_method, :argument3, :argument4)
18
+ end
19
+
20
+ def test_should_find_most_recent_matching_expectation
21
+ expectation_list = ExpectationList.new
22
+ expectation1 = Expectation.new(nil, :my_method).with(:argument1, :argument2)
23
+ expectation2 = Expectation.new(nil, :my_method).with(:argument1, :argument2)
24
+ expectation_list.add(expectation1)
25
+ expectation_list.add(expectation2)
26
+ assert_same expectation2, expectation_list.detect(:my_method, :argument1, :argument2)
27
+ end
28
+
29
+ def test_should_find_most_recent_matching_expectation_but_give_preference_to_those_allowing_invocations
30
+ expectation_list = ExpectationList.new
31
+ expectation1 = Expectation.new(nil, :my_method)
32
+ expectation2 = Expectation.new(nil, :my_method)
33
+ expectation1.define_instance_method(:invocations_allowed?) { true }
34
+ expectation2.define_instance_method(:invocations_allowed?) { false }
35
+ expectation_list.add(expectation1)
36
+ expectation_list.add(expectation2)
37
+ assert_same expectation1, expectation_list.detect(:my_method)
38
+ end
39
+
40
+ def test_should_find_most_recent_matching_expectation_if_no_matching_expectations_allow_invocations
41
+ expectation_list = ExpectationList.new
42
+ expectation1 = Expectation.new(nil, :my_method)
43
+ expectation2 = Expectation.new(nil, :my_method)
44
+ expectation1.define_instance_method(:invocations_allowed?) { false }
45
+ expectation2.define_instance_method(:invocations_allowed?) { false }
46
+ expectation_list.add(expectation1)
47
+ expectation_list.add(expectation2)
48
+ assert_same expectation2, expectation_list.detect(:my_method)
49
+ end
50
+
51
+ def test_should_find_expectations_for_the_same_method_no_matter_what_the_arguments
52
+ expectation_list = ExpectationList.new
53
+ expectation1 = Expectation.new(nil, :my_method).with(:argument1, :argument2)
54
+ expectation_list.add(expectation1)
55
+ expectation2 = Expectation.new(nil, :my_method).with(:argument3, :argument4)
56
+ expectation_list.add(expectation2)
57
+ assert_equal [expectation1, expectation2].to_set, expectation_list.similar(:my_method).to_set
58
+ end
59
+
60
+ def test_should_ignore_expectations_for_different_methods
61
+ expectation_list = ExpectationList.new
62
+ expectation1 = Expectation.new(nil, :method1).with(:argument1, :argument2)
63
+ expectation_list.add(expectation1)
64
+ expectation2 = Expectation.new(nil, :method2).with(:argument1, :argument2)
65
+ expectation_list.add(expectation2)
66
+ assert_equal [expectation2], expectation_list.similar(:method2)
67
+ end
68
+
69
+ end
@@ -66,31 +66,31 @@ class ExpectationTest < Test::Unit::TestCase
66
66
  assert !expectation.match?(:expected_method, 1, 0, 3)
67
67
  end
68
68
 
69
- def test_should_match_until_expected_invocation_count_is_one_and_actual_invocation_count_would_be_two
69
+ def test_should_allow_invocations_until_expected_invocation_count_is_one_and_actual_invocation_count_would_be_two
70
70
  expectation = new_expectation.times(1)
71
- assert expectation.match?(:expected_method)
71
+ assert expectation.invocations_allowed?
72
72
  expectation.invoke
73
- assert !expectation.match?(:expected_method)
73
+ assert !expectation.invocations_allowed?
74
74
  end
75
75
 
76
- def test_should_match_until_expected_invocation_count_is_two_and_actual_invocation_count_would_be_three
76
+ def test_should_allow_invocations_until_expected_invocation_count_is_two_and_actual_invocation_count_would_be_three
77
77
  expectation = new_expectation.times(2)
78
- assert expectation.match?(:expected_method)
78
+ assert expectation.invocations_allowed?
79
79
  expectation.invoke
80
- assert expectation.match?(:expected_method)
80
+ assert expectation.invocations_allowed?
81
81
  expectation.invoke
82
- assert !expectation.match?(:expected_method)
82
+ assert !expectation.invocations_allowed?
83
83
  end
84
-
85
- def test_should_match_until_expected_invocation_count_is_a_range_from_two_to_three_and_actual_invocation_count_would_be_four
84
+
85
+ def test_should_allow_invocations_until_expected_invocation_count_is_a_range_from_two_to_three_and_actual_invocation_count_would_be_four
86
86
  expectation = new_expectation.times(2..3)
87
- assert expectation.match?(:expected_method)
87
+ assert expectation.invocations_allowed?
88
88
  expectation.invoke
89
- assert expectation.match?(:expected_method)
89
+ assert expectation.invocations_allowed?
90
90
  expectation.invoke
91
- assert expectation.match?(:expected_method)
91
+ assert expectation.invocations_allowed?
92
92
  expectation.invoke
93
- assert !expectation.match?(:expected_method)
93
+ assert !expectation.invocations_allowed?
94
94
  end
95
95
 
96
96
  def test_should_store_provided_backtrace
@@ -395,5 +395,5 @@ class ExpectationTest < Test::Unit::TestCase
395
395
  expectation = new_expectation()
396
396
  assert_match Regexp.new("/lib/$"), expectation.mocha_lib_directory
397
397
  end
398
-
398
+
399
399
  end
@@ -7,43 +7,37 @@ class MissingExpectationsTest < Test::Unit::TestCase
7
7
 
8
8
  include Mocha
9
9
 
10
- def new_expectation
11
- Expectation.new(nil, :expected_method)
12
- end
13
-
14
- def test_should_find_expectations_to_the_same_method
15
- expectation = new_expectation.with(1)
10
+ def test_should_find_similar_expectations_on_mock
16
11
  mock = Object.new
17
- mock.define_instance_method(:expectations) { [expectation] }
18
- failed_expectation = MissingExpectation.new(mock, :expected_method).with(2)
19
- assert_equal [expectation], failed_expectation.similar_expectations
12
+ missing_expectation = MissingExpectation.new(mock, :expected_method)
13
+ method_names = []
14
+ similar_expectations = [Expectation.new(mock, :expected_method)]
15
+ mock.define_instance_method(:similar_expectations) { |method_name| method_names << method_name; similar_expectations }
16
+ assert_equal similar_expectations, missing_expectation.similar_expectations
17
+ assert_equal [:expected_method], method_names
20
18
  end
21
19
 
22
20
  def test_should_report_similar_expectations
21
+ expectation_1 = Expectation.new(nil, :expected_method).with(1)
22
+ expectation_2 = Expectation.new(nil, :expected_method).with(2)
23
+
23
24
  mock = Object.new
24
- expectation_1 = new_expectation.with(1)
25
- expectation_2 = new_expectation.with(2)
26
- mock = Object.new
27
- mock.define_instance_method(:expectations) { [expectation_1, expectation_2] }
25
+ mock.define_instance_method(:similar_expectations) { [expectation_1, expectation_2] }
26
+ mock.define_instance_method(:mocha_inspect) { 'mocha_inspect' }
27
+
28
28
  missing_expectation = MissingExpectation.new(mock, :expected_method).with(3)
29
+
29
30
  exception = assert_raise(ExpectationError) { missing_expectation.verify }
30
- assert_equal "#{mock.mocha_inspect}.expected_method(3) - expected calls: 0, actual calls: 1\nSimilar expectations:\nexpected_method(1)\nexpected_method(2)", exception.message
31
- end
32
-
33
- def test_should_ignore_expectations_to_different_methods
34
- expectation = new_expectation.with(1)
35
- mock = Object.new
36
- mock.define_instance_method(:expectations) { [expectation] }
37
- failed_expectation = MissingExpectation.new(mock, :other_method).with(1)
38
- assert failed_expectation.similar_expectations.empty?
31
+ assert_equal "mocha_inspect.expected_method(3) - expected calls: 0, actual calls: 1\nSimilar expectations:\nexpected_method(1)\nexpected_method(2)", exception.message
39
32
  end
40
33
 
41
- def test_should_not_report_similar_expectations
42
- expectation = new_expectation.with(1)
34
+ def test_should_not_report_similar_expectations_if_there_are_none
43
35
  mock = Object.new
44
- mock.define_instance_method(:expectations) { [expectation] }
36
+ mock.define_instance_method(:similar_expectations) { [] }
45
37
  mock.define_instance_method(:mocha_inspect) { 'mocha_inspect' }
38
+
46
39
  missing_expectation = MissingExpectation.new(mock, :unexpected_method).with(1)
40
+
47
41
  exception = assert_raise(ExpectationError) { missing_expectation.verify }
48
42
  assert_equal "mocha_inspect.unexpected_method(1) - expected calls: 0, actual calls: 1", exception.message
49
43
  end
@@ -19,7 +19,7 @@ class MockTest < Test::Unit::TestCase
19
19
  mock = Mock.new
20
20
  expectation = mock.expects(:method1)
21
21
  assert_not_nil expectation
22
- assert_equal [expectation], mock.expectations
22
+ assert_equal [expectation], mock.expectations.to_a
23
23
  end
24
24
 
25
25
  def test_should_not_stub_everything_by_default
@@ -105,20 +105,6 @@ class MockTest < Test::Unit::TestCase
105
105
  assert_equal [stub1, stub2].to_set, mock.expectations.to_set
106
106
  end
107
107
 
108
- def test_should_find_matching_expectation
109
- mock = Mock.new
110
- expectation1 = mock.expects(:my_method).with(:argument1, :argument2)
111
- expectation2 = mock.expects(:my_method).with(:argument3, :argument4)
112
- assert_same expectation2, mock.matching_expectation(:my_method, :argument3, :argument4)
113
- end
114
-
115
- def test_should_find_most_recent_matching_expectation
116
- mock = Mock.new
117
- expectation1 = mock.expects(:my_method).with(:argument1, :argument2)
118
- expectation2 = mock.expects(:my_method).with(:argument1, :argument2)
119
- assert_same expectation2, mock.matching_expectation(:my_method, :argument1, :argument2)
120
- end
121
-
122
108
  def test_should_invoke_expectation_and_return_result
123
109
  mock = Mock.new
124
110
  mock.expects(:my_method).returns(:result)
@@ -135,13 +121,6 @@ class MockTest < Test::Unit::TestCase
135
121
  assert_nil result
136
122
  end
137
123
 
138
- def test_should_raise_no_method_error
139
- mock = Mock.new
140
- assert_raise(NoMethodError) do
141
- mock.super_method_missing(nil)
142
- end
143
- end
144
-
145
124
  def test_should_raise_assertion_error_for_unexpected_method_call
146
125
  mock = Mock.new
147
126
  error = assert_raise(ExpectationError) do
@@ -165,19 +144,6 @@ class MockTest < Test::Unit::TestCase
165
144
  assert_equal [:argument1, :argument2], mock.arguments
166
145
  end
167
146
 
168
- def test_should_call_method_missing_for_parent
169
- mock = Mock.new
170
- class << mock
171
- attr_accessor :symbol, :arguments
172
- def super_method_missing(symbol, *arguments, &block)
173
- self.symbol, self.arguments = symbol, arguments
174
- end
175
- end
176
- mock.my_method(:argument1, :argument2)
177
- assert_equal :my_method, mock.symbol
178
- assert_equal [:argument1, :argument2], mock.arguments
179
- end
180
-
181
147
  def test_should_verify_that_all_expectations_have_been_fulfilled
182
148
  mock = Mock.new
183
149
  mock.expects(:method1)
@@ -347,5 +313,4 @@ class MockTest < Test::Unit::TestCase
347
313
  assert_match(/which responds like mocha_inspect/, e.message)
348
314
  end
349
315
  end
350
-
351
316
  end
@@ -40,7 +40,7 @@ class ObjectTest < Test::Unit::TestCase
40
40
  $stubba = Mock.new
41
41
  $stubba.stubs(:stub)
42
42
  expectation = instance.expects(:method1)
43
- assert_equal [expectation], instance.mocha.expectations
43
+ assert_equal [expectation], instance.mocha.expectations.to_a
44
44
  end
45
45
 
46
46
  def test_should_verify_expectations
@@ -106,7 +106,7 @@ class ObjectTest < Test::Unit::TestCase
106
106
  $stubba = Mock.new
107
107
  $stubba.stubs(:stub)
108
108
  expectation = klass.expects(:method1)
109
- assert_equal [expectation], klass.mocha.expectations
109
+ assert_equal [expectation], klass.mocha.expectations.to_a
110
110
  end
111
111
 
112
112
  def test_should_stub_module_method
@@ -122,7 +122,7 @@ class ObjectTest < Test::Unit::TestCase
122
122
  $stubba = Mock.new
123
123
  $stubba.stubs(:stub)
124
124
  expectation = mod.expects(:method1)
125
- assert_equal [expectation], mod.mocha.expectations
125
+ assert_equal [expectation], mod.mocha.expectations.to_a
126
126
  end
127
127
 
128
128
  def test_should_use_stubba_instance_method_for_object
metadata CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.9.2
3
3
  specification_version: 1
4
4
  name: mocha
5
5
  version: !ruby/object:Gem::Version
6
- version: 0.5.1
7
- date: 2007-06-15 00:00:00 +01:00
6
+ version: 0.5.2
7
+ date: 2007-07-05 00:00:00 +01:00
8
8
  summary: Mocking and stubbing library
9
9
  require_paths:
10
10
  - lib
@@ -37,6 +37,7 @@ files:
37
37
  - lib/mocha/exception_raiser.rb
38
38
  - lib/mocha/expectation.rb
39
39
  - lib/mocha/expectation_error.rb
40
+ - lib/mocha/expectation_list.rb
40
41
  - lib/mocha/infinite_range.rb
41
42
  - lib/mocha/inspect.rb
42
43
  - lib/mocha/instance_method.rb
@@ -67,6 +68,7 @@ files:
67
68
  - lib/mocha.rb
68
69
  - lib/mocha_standalone.rb
69
70
  - lib/stubba.rb
71
+ - test/acceptance/expected_invocation_count_acceptance_test.rb
70
72
  - test/acceptance/mocha_acceptance_test.rb
71
73
  - test/acceptance/mocked_methods_dispatch_acceptance_test.rb
72
74
  - test/acceptance/parameter_matcher_acceptance_test.rb
@@ -87,6 +89,7 @@ files:
87
89
  - test/unit/central_test.rb
88
90
  - test/unit/class_method_test.rb
89
91
  - test/unit/date_time_inspect_test.rb
92
+ - test/unit/expectation_list_test.rb
90
93
  - test/unit/expectation_raiser_test.rb
91
94
  - test/unit/expectation_test.rb
92
95
  - test/unit/hash_inspect_test.rb