jferris-mocha 0.9.5.0.1240002286 → 0.9.5.0.1240351621

Sign up to get free protection for your applications and to get access to all the features.
data/lib/mocha/api.rb CHANGED
@@ -149,6 +149,13 @@ module Mocha # :nodoc:
149
149
  Mockery.instance.new_state_machine(name)
150
150
  end
151
151
 
152
+ # Asserts that the given mock received the given method.
153
+ #
154
+ # Examples:
155
+ #
156
+ # assert_received(mock, :to_s)
157
+ # assert_received(Radio, :new) {|expect| expect.with(1041) }
158
+ # assert_received(radio, :volume) {|expect| expect.with(11).twice }
152
159
  def assert_received(mock, expected_method_name)
153
160
  matcher = have_received(expected_method_name)
154
161
  yield(matcher) if block_given?
@@ -170,7 +177,7 @@ module Mocha # :nodoc:
170
177
  if mock.respond_to?(:mocha)
171
178
  @mock = mock.mocha
172
179
  else
173
- mock = @mock
180
+ @mock = mock
174
181
  end
175
182
 
176
183
  @expectation = Expectation.new(@mock, @expected_method_name)
@@ -204,6 +211,16 @@ module Mocha # :nodoc:
204
211
  end
205
212
  end
206
213
 
214
+ # :call-seq:
215
+ # should have_received(method).with(arguments).times(times)
216
+ #
217
+ # Ensures that the given mock received the given method.
218
+ #
219
+ # Examples:
220
+ #
221
+ # mock.should have_received(:to_s)
222
+ # Radio.should have_received(:new).with(1041)
223
+ # radio.should have_received(:volume).with(11).twice
207
224
  def have_received(expected_method_name)
208
225
  HaveReceived.new(expected_method_name)
209
226
  end
@@ -2,10 +2,8 @@ require File.join(File.dirname(__FILE__), "acceptance_test_helper")
2
2
  require 'mocha'
3
3
  require 'matcher_helpers'
4
4
 
5
- class SpyTest < Test::Unit::TestCase
6
-
7
- include AcceptanceTest
8
-
5
+ module SpyTestMethods
6
+
9
7
  def setup
10
8
  setup_acceptance_test
11
9
  end
@@ -15,7 +13,7 @@ class SpyTest < Test::Unit::TestCase
15
13
  end
16
14
 
17
15
  def test_should_accept_wildcard_stub_call_without_arguments
18
- instance = Object.new
16
+ instance = new_instance
19
17
  instance.stubs(:to_s)
20
18
  instance.to_s
21
19
  assert_received(instance, :to_s)
@@ -23,7 +21,7 @@ class SpyTest < Test::Unit::TestCase
23
21
  end
24
22
 
25
23
  def test_should_accept_wildcard_stub_call_with_arguments
26
- instance = Object.new
24
+ instance = new_instance
27
25
  instance.stubs(:to_s)
28
26
  instance.to_s(:argument)
29
27
  assert_received(instance, :to_s)
@@ -31,14 +29,14 @@ class SpyTest < Test::Unit::TestCase
31
29
  end
32
30
 
33
31
  def test_should_not_accept_wildcard_stub_without_call
34
- instance = Object.new
32
+ instance = new_instance
35
33
  instance.stubs(:to_s)
36
34
  assert_fails { assert_received(instance, :to_s) }
37
35
  assert_fails { assert_matcher_accepts have_received(:to_s), instance }
38
36
  end
39
37
 
40
38
  def test_should_not_accept_call_without_arguments
41
- instance = Object.new
39
+ instance = new_instance
42
40
  instance.stubs(:to_s)
43
41
  instance.to_s
44
42
  assert_fails { assert_received(instance, :to_s) {|expect| expect.with(1) } }
@@ -46,7 +44,7 @@ class SpyTest < Test::Unit::TestCase
46
44
  end
47
45
 
48
46
  def test_should_not_accept_call_with_different_arguments
49
- instance = Object.new
47
+ instance = new_instance
50
48
  instance.stubs(:to_s)
51
49
  instance.to_s(2)
52
50
  assert_fails { assert_received(instance, :to_s) {|expect| expect.with(1) } }
@@ -54,7 +52,7 @@ class SpyTest < Test::Unit::TestCase
54
52
  end
55
53
 
56
54
  def test_should_accept_call_with_correct_arguments
57
- instance = Object.new
55
+ instance = new_instance
58
56
  instance.stubs(:to_s)
59
57
  instance.to_s(1)
60
58
  assert_received(instance, :to_s) {|expect| expect.with(1) }
@@ -62,7 +60,7 @@ class SpyTest < Test::Unit::TestCase
62
60
  end
63
61
 
64
62
  def test_should_accept_call_with_wildcard_arguments
65
- instance = Object.new
63
+ instance = new_instance
66
64
  instance.stubs(:to_s)
67
65
  instance.to_s('hello')
68
66
  assert_received(instance, :to_s) {|expect| expect.with(is_a(String)) }
@@ -70,8 +68,8 @@ class SpyTest < Test::Unit::TestCase
70
68
  end
71
69
 
72
70
  def test_should_reject_call_on_different_mock
73
- instance = Object.new
74
- other = Object.new
71
+ instance = new_instance
72
+ other = new_instance
75
73
  instance.stubs(:to_s)
76
74
  other.stubs(:to_s)
77
75
  other.to_s('hello')
@@ -80,7 +78,7 @@ class SpyTest < Test::Unit::TestCase
80
78
  end
81
79
 
82
80
  def test_should_accept_correct_number_of_calls
83
- instance = Object.new
81
+ instance = new_instance
84
82
  instance.stubs(:to_s)
85
83
  2.times { instance.to_s }
86
84
  assert_received(instance, :to_s) {|expect| expect.twice }
@@ -88,7 +86,7 @@ class SpyTest < Test::Unit::TestCase
88
86
  end
89
87
 
90
88
  def test_should_reject_incorrect_number_of_calls
91
- instance = Object.new
89
+ instance = new_instance
92
90
  instance.stubs(:to_s)
93
91
  instance.to_s
94
92
  message = /expected exactly twice/
@@ -107,3 +105,21 @@ class SpyTest < Test::Unit::TestCase
107
105
  end
108
106
 
109
107
  end
108
+
109
+ class PartialSpyTest < Test::Unit::TestCase
110
+ include AcceptanceTest
111
+ include SpyTestMethods
112
+
113
+ def new_instance
114
+ Object.new
115
+ end
116
+ end
117
+
118
+ class PureSpyTest < Test::Unit::TestCase
119
+ include AcceptanceTest
120
+ include SpyTestMethods
121
+
122
+ def new_instance
123
+ stub
124
+ end
125
+ end
@@ -5,33 +5,17 @@ require 'mocha/mockery'
5
5
  require 'mocha/object'
6
6
  require 'matcher_helpers'
7
7
 
8
- class HaveReceivedTest < Test::Unit::TestCase
8
+ module HaveReceivedTestMethods
9
9
 
10
10
  include Mocha
11
- include TestRunner
12
- include Mocha::API
13
11
 
14
12
  def teardown
15
13
  Mockery.reset_instance
16
14
  end
17
15
 
18
- class FakeMock
19
- def initialize(name)
20
- @name = name
21
- end
22
-
23
- def inspect
24
- @name
25
- end
26
-
27
- def mocha
28
- self
29
- end
30
- end
31
-
32
16
  def test_passes_if_invocation_exists
33
17
  method = :a_method
34
- mock = FakeMock.new('a mock')
18
+ mock = new_mock('a mock')
35
19
  Mockery.instance.invocation(mock, method, [])
36
20
  assert_passes do
37
21
  assert_matcher_accepts have_received(method), mock
@@ -40,7 +24,7 @@ class HaveReceivedTest < Test::Unit::TestCase
40
24
 
41
25
  def test_fails_if_invocation_doesnt_exist
42
26
  method = :a_method
43
- mock = FakeMock.new('a mock')
27
+ mock = new_mock('a mock')
44
28
  assert_fails do
45
29
  assert_matcher_accepts have_received(method), mock
46
30
  end
@@ -48,7 +32,7 @@ class HaveReceivedTest < Test::Unit::TestCase
48
32
 
49
33
  def test_fails_if_invocation_exists_with_different_arguments
50
34
  method = :a_method
51
- mock = FakeMock.new('a mock')
35
+ mock = new_mock('a mock')
52
36
  Mockery.instance.invocation(mock, method, [2, 1])
53
37
  assert_fails do
54
38
  assert_matcher_accepts have_received(method).with(1, 2), mock
@@ -57,7 +41,7 @@ class HaveReceivedTest < Test::Unit::TestCase
57
41
 
58
42
  def test_passes_if_invocation_exists_with_wildcard_arguments
59
43
  method = :a_method
60
- mock = FakeMock.new('a mock')
44
+ mock = new_mock('a mock')
61
45
  Mockery.instance.invocation(mock, method, ['hello'])
62
46
  assert_passes do
63
47
  assert_matcher_accepts have_received(method).with(is_a(String)), mock
@@ -66,7 +50,7 @@ class HaveReceivedTest < Test::Unit::TestCase
66
50
 
67
51
  def test_passes_if_invocation_exists_with_exact_arguments
68
52
  method = :a_method
69
- mock = FakeMock.new('a mock')
53
+ mock = new_mock('a mock')
70
54
  Mockery.instance.invocation(mock, method, ['hello'])
71
55
  assert_passes do
72
56
  assert_matcher_accepts have_received(method).with('hello'), mock
@@ -75,7 +59,7 @@ class HaveReceivedTest < Test::Unit::TestCase
75
59
 
76
60
  def test_fails_if_invocation_exists_only_on_other_mock
77
61
  method = :a_method
78
- mock = FakeMock.new('a mock')
62
+ mock = new_mock('a mock')
79
63
  other = 'another mock'
80
64
  Mockery.instance.invocation(other, method, ['hello'])
81
65
  assert_fails do
@@ -86,7 +70,7 @@ class HaveReceivedTest < Test::Unit::TestCase
86
70
  def test_passes_if_invocation_exists_for_impersonating_mock
87
71
  method = :a_method
88
72
  object = Object.new
89
- mock = FakeMock.new('a mock')
73
+ mock = new_mock('a mock')
90
74
 
91
75
  class << object
92
76
  attr_accessor :mocha
@@ -101,7 +85,7 @@ class HaveReceivedTest < Test::Unit::TestCase
101
85
 
102
86
  def test_passes_if_invocation_count_correct
103
87
  method = :a_method
104
- mock = FakeMock.new('a mock')
88
+ mock = new_mock('a mock')
105
89
  2.times { Mockery.instance.invocation(mock, method, []) }
106
90
  assert_passes do
107
91
  assert_matcher_accepts have_received(method).twice, mock
@@ -110,7 +94,7 @@ class HaveReceivedTest < Test::Unit::TestCase
110
94
 
111
95
  def test_fails_if_invocation_count_incorrect
112
96
  method = :a_method
113
- mock = FakeMock.new('a mock')
97
+ mock = new_mock('a mock')
114
98
  Mockery.instance.invocation(mock, method, [])
115
99
  assert_fails do
116
100
  assert_matcher_accepts have_received(method).twice, mock
@@ -135,3 +119,56 @@ class HaveReceivedTest < Test::Unit::TestCase
135
119
  end
136
120
 
137
121
  end
122
+
123
+ class PartialHaveReceivedTest < Test::Unit::TestCase
124
+
125
+ include TestRunner
126
+ include Mocha::API
127
+ include HaveReceivedTestMethods
128
+
129
+ class FakeMock
130
+ def initialize(name)
131
+ @name = name
132
+ end
133
+
134
+ def inspect
135
+ @name
136
+ end
137
+
138
+ def mocha
139
+ self
140
+ end
141
+ end
142
+
143
+ def new_mock(*args)
144
+ FakeMock.new(*args)
145
+ end
146
+
147
+ end
148
+
149
+
150
+ class PureHaveReceivedTest < Test::Unit::TestCase
151
+
152
+ include TestRunner
153
+ include Mocha::API
154
+ include HaveReceivedTestMethods
155
+
156
+ class FakeMock
157
+ def initialize(name)
158
+ @name = name
159
+ end
160
+
161
+ def inspect
162
+ @name
163
+ end
164
+
165
+ def mocha
166
+ self
167
+ end
168
+ end
169
+
170
+ def new_mock(*args)
171
+ Mocha::Mock.new(*args)
172
+ end
173
+
174
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jferris-mocha
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.5.0.1240002286
4
+ version: 0.9.5.0.1240351621
5
5
  platform: ruby
6
6
  authors:
7
7
  - James Mead
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-04-17 00:00:00 -07:00
12
+ date: 2009-04-20 21:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency