rr 0.2.4 → 0.2.5

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGES CHANGED
@@ -1,3 +1,12 @@
1
+ * 0.2.5
2
+ - mock takes method_name argument
3
+ - stub takes method_name argument
4
+ - mock_probe takes method_name argument
5
+ - stub_probe takes method_name argument
6
+ - probe takes method_name argument
7
+ - dont_allow takes method_name argument
8
+ - do_not_allow takes method_name argument
9
+
1
10
  * 0.2.4
2
11
  - Space#doubles key is now the object id
3
12
  - Fixed [#12402] Stubbing return value of probes fails after calling the stubbed method two times
data/Rakefile CHANGED
@@ -21,7 +21,7 @@ def run_suite
21
21
  end
22
22
 
23
23
  PKG_NAME = "rr"
24
- PKG_VERSION = "0.2.4"
24
+ PKG_VERSION = "0.2.5"
25
25
  PKG_FILES = FileList[
26
26
  '[A-Z]*',
27
27
  '*.rb',
@@ -7,23 +7,46 @@ module Extensions
7
7
 
8
8
  before do
9
9
  @subject = Object.new
10
+ class << @subject
11
+ def foobar(*args)
12
+ :original_value
13
+ end
14
+ end
10
15
  end
11
16
 
12
17
  it "sets up the RR mock call chain" do
13
- should_create_mock_call_chain mock(@subject)
18
+ should create_mock_call_chain(mock(@subject))
14
19
  end
15
20
 
16
- it "sets up the RR mock call chain with rr_mock" do
17
- should_create_mock_call_chain rr_mock(@subject)
21
+ it "#rr_mock sets up the RR mock call chain" do
22
+ should create_mock_call_chain(rr_mock(@subject))
18
23
  end
19
24
 
20
- def should_create_mock_call_chain(creator)
21
- class << @subject
22
- def foobar(*args)
23
- :original_value
24
- end
25
- end
25
+ it "creates a mock Scenario for method when passed a second argument" do
26
+ should create_scenario_with_method_name(mock(@subject, :foobar))
27
+ end
26
28
 
29
+ it "creates a mock Scenario for method when passed a second argument with rr_mock" do
30
+ should create_scenario_with_method_name(rr_mock(@subject, :foobar))
31
+ end
32
+
33
+ it "raises error if passed a method name and a block" do
34
+ proc do
35
+ mock(@object, :foobar) {}
36
+ end.should raise_error(ArgumentError, "Cannot pass in a method name and a block")
37
+ end
38
+
39
+ def create_scenario_with_method_name(scenario)
40
+ method_name = scenario.method_name
41
+ scenario.with(1, 2) {:baz}
42
+ scenario.times_matcher.should == TimesCalledMatchers::IntegerMatcher.new(1)
43
+ scenario.argument_expectation.class.should == RR::Expectations::ArgumentEqualityExpectation
44
+ scenario.argument_expectation.expected_arguments.should == [1, 2]
45
+
46
+ @subject.__send__(method_name, 1, 2).should == :baz
47
+ end
48
+
49
+ def create_mock_call_chain(creator)
27
50
  scenario = creator.foobar(1, 2) {:baz}
28
51
  scenario.times_matcher.should == TimesCalledMatchers::IntegerMatcher.new(1)
29
52
  scenario.argument_expectation.class.should == RR::Expectations::ArgumentEqualityExpectation
@@ -38,23 +61,44 @@ module Extensions
38
61
 
39
62
  before do
40
63
  @subject = Object.new
64
+ class << @subject
65
+ def foobar(*args)
66
+ :original_value
67
+ end
68
+ end
41
69
  end
42
70
 
43
71
  it "sets up the RR stub call chain" do
44
- should_create_stub_call_chain stub(@subject)
72
+ should create_stub_call_chain(stub(@subject))
45
73
  end
46
74
 
47
- it "sets up the RR stub call chain with rr_stub" do
48
- should_create_stub_call_chain rr_stub(@subject)
75
+ it "#rr_stub sets up the RR stub call chain" do
76
+ should create_stub_call_chain(rr_stub(@subject))
49
77
  end
50
78
 
51
- def should_create_stub_call_chain(creator)
52
- class << @subject
53
- def foobar(*args)
54
- :original_value
55
- end
56
- end
79
+ it "creates a stub Scenario for method when passed a second argument" do
80
+ should create_scenario_with_method_name(stub(@subject, :foobar))
81
+ end
82
+
83
+ it "#rr_stub creates a stub Scenario for method when passed a second argument" do
84
+ should create_scenario_with_method_name(rr_stub(@subject, :foobar))
85
+ end
86
+
87
+ it "raises error if passed a method name and a block" do
88
+ proc do
89
+ stub(@object, :foobar) {}
90
+ end.should raise_error(ArgumentError, "Cannot pass in a method name and a block")
91
+ end
57
92
 
93
+ def create_scenario_with_method_name(scenario)
94
+ method_name = scenario.method_name
95
+ scenario.with(1, 2) {:baz}
96
+ scenario.times_matcher.should == TimesCalledMatchers::AnyTimesMatcher.new
97
+ scenario.argument_expectation.class.should == RR::Expectations::ArgumentEqualityExpectation
98
+ @subject.__send__(method_name, 1, 2).should == :baz
99
+ end
100
+
101
+ def create_stub_call_chain(creator)
58
102
  scenario = creator.foobar(1, 2) {:baz}
59
103
  scenario.times_matcher.should == TimesCalledMatchers::AnyTimesMatcher.new
60
104
  scenario.argument_expectation.class.should == RR::Expectations::ArgumentEqualityExpectation
@@ -67,27 +111,62 @@ module Extensions
67
111
 
68
112
  before do
69
113
  @subject = Object.new
114
+ class << @subject
115
+ def foobar(*args)
116
+ :original_value
117
+ end
118
+ end
70
119
  end
71
120
 
72
- it "sets up the RR probe call chain" do
73
- should_create_mock_probe_call_chain probe(@subject)
121
+ it "#probe sets up the RR probe call chain" do
122
+ should create_mock_probe_call_chain(probe(@subject))
74
123
  end
75
124
 
76
- it "sets up the RR probe call chain" do
77
- should_create_mock_probe_call_chain mock_probe(@subject)
125
+ it "#rr_probe sets up the RR probe call chain" do
126
+ should create_mock_probe_call_chain(rr_probe(@subject))
78
127
  end
79
128
 
80
- it "sets up the RR probe call chain with rr_probe" do
81
- should_create_mock_probe_call_chain rr_mock_probe(@subject)
129
+ it "#mock_probe sets up the RR probe call chain" do
130
+ should create_mock_probe_call_chain(mock_probe(@subject))
82
131
  end
83
132
 
84
- def should_create_mock_probe_call_chain(creator)
85
- class << @subject
86
- def foobar(*args)
87
- :original_value
88
- end
89
- end
133
+ it "#rr_mock_probe sets up the RR probe call chain with rr_probe" do
134
+ should create_mock_probe_call_chain(rr_mock_probe(@subject))
135
+ end
136
+
137
+ it "#probe creates a mock Scenario for method when passed a second argument" do
138
+ should create_scenario_with_method_name(probe(@subject, :foobar))
139
+ end
140
+
141
+ it "#rr_probe creates a mock Scenario for method when passed a second argument with rr_mock" do
142
+ should create_scenario_with_method_name(rr_probe(@subject, :foobar))
143
+ end
144
+
145
+ it "#mock_probe creates a mock Scenario for method when passed a second argument" do
146
+ should create_scenario_with_method_name(mock_probe(@subject, :foobar))
147
+ end
90
148
 
149
+ it "#rr_mock_probe creates a mock Scenario for method when passed a second argument with rr_mock" do
150
+ should create_scenario_with_method_name(rr_mock_probe(@subject, :foobar))
151
+ end
152
+
153
+ it "raises error if passed a method name and a block" do
154
+ proc do
155
+ mock_probe(@object, :foobar) {}
156
+ end.should raise_error(ArgumentError, "Cannot pass in a method name and a block")
157
+ end
158
+
159
+ def create_scenario_with_method_name(scenario)
160
+ method_name = scenario.method_name
161
+ scenario.with(1, 2)
162
+ scenario.times_matcher.should == TimesCalledMatchers::IntegerMatcher.new(1)
163
+ scenario.argument_expectation.class.should == RR::Expectations::ArgumentEqualityExpectation
164
+ scenario.argument_expectation.expected_arguments.should == [1, 2]
165
+
166
+ @subject.__send__(method_name, 1, 2).should == :original_value
167
+ end
168
+
169
+ def create_mock_probe_call_chain(creator)
91
170
  scenario = creator.foobar(1, 2)
92
171
  scenario.times_matcher.should == TimesCalledMatchers::IntegerMatcher.new(1)
93
172
  scenario.argument_expectation.class.should == RR::Expectations::ArgumentEqualityExpectation
@@ -102,23 +181,44 @@ module Extensions
102
181
 
103
182
  before do
104
183
  @subject = Object.new
184
+ class << @subject
185
+ def foobar(*args)
186
+ :original_value
187
+ end
188
+ end
105
189
  end
106
190
 
107
191
  it "sets up the RR probe call chain" do
108
- should_create_stub_probe_call_chain stub_probe(@subject)
192
+ should create_stub_probe_call_chain(stub_probe(@subject))
109
193
  end
110
194
 
111
195
  it "sets up the RR probe call chain" do
112
- should_create_stub_probe_call_chain rr_stub_probe(@subject)
196
+ should create_stub_probe_call_chain(rr_stub_probe(@subject))
113
197
  end
114
198
 
115
- def should_create_stub_probe_call_chain(creator)
116
- class << @subject
117
- def foobar(*args)
118
- :original_value
119
- end
120
- end
199
+ it "#stub_probe creates a stub Scenario for method when passed a second argument" do
200
+ should create_scenario_with_method_name(stub_probe(@subject, :foobar))
201
+ end
202
+
203
+ it "#rr_stub_probe creates a stub Scenario for method when passed a second argument with rr_stub" do
204
+ should create_scenario_with_method_name(rr_stub_probe(@subject, :foobar))
205
+ end
206
+
207
+ it "raises error if passed a method name and a block" do
208
+ proc do
209
+ stub_probe(@object, :foobar) {}
210
+ end.should raise_error(ArgumentError, "Cannot pass in a method name and a block")
211
+ end
121
212
 
213
+ def create_scenario_with_method_name(scenario)
214
+ method_name = scenario.method_name
215
+ scenario.times_matcher.should == TimesCalledMatchers::AnyTimesMatcher.new
216
+ scenario.argument_expectation.class.should == RR::Expectations::AnyArgumentExpectation
217
+
218
+ @subject.foobar(:something).should == :original_value
219
+ end
220
+
221
+ def create_stub_probe_call_chain(creator)
122
222
  scenario = creator.foobar
123
223
  scenario.times_matcher.should == TimesCalledMatchers::AnyTimesMatcher.new
124
224
  scenario.argument_expectation.class.should == RR::Expectations::AnyArgumentExpectation
@@ -132,35 +232,76 @@ module Extensions
132
232
 
133
233
  before do
134
234
  @subject = Object.new
235
+ class << @subject
236
+ def foobar(*args)
237
+ :original_value
238
+ end
239
+ end
135
240
  end
136
241
 
137
242
  it "sets up the RR do_not_allow call chain" do
138
- should_create_do_not_allow_call_chain do_not_allow(@subject)
243
+ should create_do_not_allow_call_chain(do_not_allow(@subject))
139
244
  end
140
245
 
141
246
  it "sets up the RR do_not_allow call chain with rr_do_not_allow" do
142
- should_create_do_not_allow_call_chain rr_do_not_allow(@subject)
247
+ should create_do_not_allow_call_chain(rr_do_not_allow(@subject))
143
248
  end
144
249
 
145
250
  it "sets up the RR do_not_allow call chain" do
146
- should_create_do_not_allow_call_chain dont_allow(@subject)
251
+ should create_do_not_allow_call_chain(dont_allow(@subject))
147
252
  end
148
253
 
149
254
  it "sets up the RR do_not_allow call chain with rr_do_not_allow" do
150
- should_create_do_not_allow_call_chain rr_dont_allow(@subject)
255
+ should create_do_not_allow_call_chain(rr_dont_allow(@subject))
151
256
  end
152
257
 
153
- def should_create_do_not_allow_call_chain(creator)
154
- class << @subject
155
- def foobar(*args)
156
- :original_value
157
- end
158
- end
258
+ it "#do_not_allow creates a mock Scenario for method when passed a second argument" do
259
+ should create_scenario_with_method_name(do_not_allow(@subject, :foobar))
260
+ end
261
+
262
+ it "#rr_do_not_allow creates a mock Scenario for method when passed a second argument with rr_mock" do
263
+ should create_scenario_with_method_name(rr_do_not_allow(@subject, :foobar))
264
+ end
265
+
266
+ it "#dont_allow creates a mock Scenario for method when passed a second argument" do
267
+ should create_scenario_with_method_name(dont_allow(@subject, :foobar))
268
+ end
269
+
270
+ it "#rr_dont_allow creates a mock Scenario for method when passed a second argument with rr_mock" do
271
+ should create_scenario_with_method_name(rr_dont_allow(@subject, :foobar))
272
+ end
273
+
274
+ it "raises error if passed a method name and a block" do
275
+ proc do
276
+ do_not_allow(@object, :foobar) {}
277
+ end.should raise_error(ArgumentError, "Cannot pass in a method name and a block")
278
+ end
159
279
 
280
+ def create_scenario_with_method_name(scenario)
281
+ method_name = scenario.method_name
282
+ scenario.with(1, 2)
283
+ scenario.times_matcher.should == TimesCalledMatchers::IntegerMatcher.new(0)
284
+ scenario.argument_expectation.class.should == RR::Expectations::ArgumentEqualityExpectation
285
+ scenario.argument_expectation.expected_arguments.should == [1, 2]
286
+
287
+ proc do
288
+ @subject.__send__(method_name, 1, 2)
289
+ end.should raise_error(Errors::TimesCalledError)
290
+ reset
291
+ nil
292
+ end
293
+
294
+ def create_do_not_allow_call_chain(creator)
160
295
  scenario = creator.foobar(1, 2)
161
296
  scenario.times_matcher.should == TimesCalledMatchers::IntegerMatcher.new(0)
162
297
  scenario.argument_expectation.class.should == RR::Expectations::ArgumentEqualityExpectation
163
298
  scenario.argument_expectation.expected_arguments.should == [1, 2]
299
+
300
+ proc do
301
+ @subject.foobar(1, 2)
302
+ end.should raise_error(Errors::TimesCalledError)
303
+ reset
304
+ nil
164
305
  end
165
306
  end
166
307
  end
@@ -1,7 +1,7 @@
1
1
  require "examples/example_helper"
2
2
 
3
3
  module RR
4
- describe Space, "#create_mock_creator" do
4
+ describe Space, "#mock_creator" do
5
5
  it_should_behave_like "RR::Space"
6
6
 
7
7
  before do
@@ -10,14 +10,26 @@ describe Space, "#create_mock_creator" do
10
10
  end
11
11
 
12
12
  it "creates a MockCreator" do
13
- creator = @space.create_mock_creator(@object)
13
+ creator = @space.mock_creator(@object)
14
14
  creator.foobar(1) {:baz}
15
15
  @object.foobar(1).should == :baz
16
16
  proc {@object.foobar(1)}.should raise_error(Errors::TimesCalledError)
17
17
  end
18
18
 
19
+ it "creates a mock Scenario for method when passed a second argument" do
20
+ creator = @space.mock_creator(@object, :foobar).with(1) {:baz}
21
+ @object.foobar(1).should == :baz
22
+ proc {@object.foobar(1)}.should raise_error(Errors::TimesCalledError)
23
+ end
24
+
25
+ it "raises error if passed a method name and a block" do
26
+ proc do
27
+ @space.mock_creator(@object, :foobar) {}
28
+ end.should raise_error(ArgumentError, "Cannot pass in a method name and a block")
29
+ end
30
+
19
31
  it "uses block definition when passed a block" do
20
- creator = @space.create_mock_creator(@object) do |c|
32
+ creator = @space.mock_creator(@object) do |c|
21
33
  c.foobar(1) {:baz}
22
34
  end
23
35
  @object.foobar(1).should == :baz
@@ -25,7 +37,7 @@ describe Space, "#create_mock_creator" do
25
37
  end
26
38
  end
27
39
 
28
- describe Space, "#create_stub_creator" do
40
+ describe Space, "#stub_creator" do
29
41
  it_should_behave_like "RR::Space"
30
42
 
31
43
  before do
@@ -35,14 +47,26 @@ describe Space, "#create_stub_creator" do
35
47
  end
36
48
 
37
49
  it "creates a StubCreator" do
38
- creator = @space.create_stub_creator(@object)
50
+ creator = @space.stub_creator(@object)
39
51
  creator.foobar {:baz}
40
52
  @object.foobar.should == :baz
41
53
  @object.foobar.should == :baz
42
54
  end
43
55
 
56
+ it "creates a stub Scenario for method when passed a second argument" do
57
+ creator = @space.stub_creator(@object, :foobar).with(1) {:baz}
58
+ @object.foobar(1).should == :baz
59
+ @object.foobar(1).should == :baz
60
+ end
61
+
62
+ it "raises error if passed a method name and a block" do
63
+ proc do
64
+ @space.stub_creator(@object, :foobar) {}
65
+ end.should raise_error(ArgumentError, "Cannot pass in a method name and a block")
66
+ end
67
+
44
68
  it "uses block definition when passed a block" do
45
- creator = @space.create_stub_creator(@object) do |c|
69
+ creator = @space.stub_creator(@object) do |c|
46
70
  c.foobar(1) {:return_value}
47
71
  c.foobar.with_any_args {:default}
48
72
  c.baz(1) {:baz_value}
@@ -53,7 +77,7 @@ describe Space, "#create_stub_creator" do
53
77
  end
54
78
  end
55
79
 
56
- describe Space, "#create_mock_probe_creator" do
80
+ describe Space, "#mock_probe_creator" do
57
81
  it_should_behave_like "RR::Space"
58
82
 
59
83
  before do
@@ -66,14 +90,26 @@ describe Space, "#create_mock_probe_creator" do
66
90
  end
67
91
 
68
92
  it "creates a MockProbeCreator" do
69
- creator = @space.create_mock_probe_creator(@object)
93
+ creator = @space.mock_probe_creator(@object)
70
94
  creator.foobar(1)
71
95
  @object.foobar(1).should == :original_foobar
72
96
  proc {@object.foobar(1)}.should raise_error(Errors::TimesCalledError)
73
97
  end
74
98
 
99
+ it "creates a mock probe Scenario for method when passed a second argument" do
100
+ creator = @space.mock_probe_creator(@object, :foobar).with(1)
101
+ @object.foobar(1).should == :original_foobar
102
+ proc {@object.foobar(1)}.should raise_error(Errors::TimesCalledError)
103
+ end
104
+
105
+ it "raises error if passed a method name and a block" do
106
+ proc do
107
+ @space.mock_probe_creator(@object, :foobar) {}
108
+ end.should raise_error(ArgumentError, "Cannot pass in a method name and a block")
109
+ end
110
+
75
111
  it "uses block definition when passed a block" do
76
- creator = @space.create_mock_probe_creator(@object) do |c|
112
+ creator = @space.mock_probe_creator(@object) do |c|
77
113
  c.foobar(1)
78
114
  end
79
115
  @object.foobar(1).should == :original_foobar
@@ -81,7 +117,7 @@ describe Space, "#create_mock_probe_creator" do
81
117
  end
82
118
  end
83
119
 
84
- describe Space, "#create_stub_probe_creator" do
120
+ describe Space, "#stub_probe_creator" do
85
121
  it_should_behave_like "RR::Space"
86
122
 
87
123
  before do
@@ -94,14 +130,26 @@ describe Space, "#create_stub_probe_creator" do
94
130
  end
95
131
 
96
132
  it "creates a StubProbeCreator" do
97
- creator = @space.create_stub_probe_creator(@object)
133
+ creator = @space.stub_probe_creator(@object)
98
134
  creator.foobar
99
135
  @object.foobar(1).should == :original_foobar
100
136
  @object.foobar(1).should == :original_foobar
101
137
  end
102
138
 
139
+ it "creates a stub probe Scenario for method when passed a second argument" do
140
+ creator = @space.stub_probe_creator(@object, :foobar)
141
+ @object.foobar(1).should == :original_foobar
142
+ @object.foobar(1).should == :original_foobar
143
+ end
144
+
145
+ it "raises error if passed a method name and a block" do
146
+ proc do
147
+ @space.stub_probe_creator(@object, :foobar) {}
148
+ end.should raise_error(ArgumentError, "Cannot pass in a method name and a block")
149
+ end
150
+
103
151
  it "uses block definition when passed a block" do
104
- creator = @space.create_stub_probe_creator(@object) do |c|
152
+ creator = @space.stub_probe_creator(@object) do |c|
105
153
  c.foobar(1)
106
154
  end
107
155
  @object.foobar(1).should == :original_foobar
@@ -109,7 +157,7 @@ describe Space, "#create_stub_probe_creator" do
109
157
  end
110
158
  end
111
159
 
112
- describe Space, "#create_do_not_allow_creator" do
160
+ describe Space, "#do_not_allow_creator" do
113
161
  it_should_behave_like "RR::Space"
114
162
 
115
163
  before do
@@ -118,13 +166,24 @@ describe Space, "#create_do_not_allow_creator" do
118
166
  end
119
167
 
120
168
  it "creates a MockCreator" do
121
- creator = @space.create_do_not_allow_creator(@object)
169
+ creator = @space.do_not_allow_creator(@object)
122
170
  creator.foobar(1)
123
171
  proc {@object.foobar(1)}.should raise_error(Errors::TimesCalledError)
124
172
  end
125
173
 
174
+ it "creates a do not allow Scenario for method when passed a second argument" do
175
+ creator = @space.do_not_allow_creator(@object, :foobar).with(1)
176
+ proc {@object.foobar(1)}.should raise_error(Errors::TimesCalledError)
177
+ end
178
+
179
+ it "raises error if passed a method name and a block" do
180
+ proc do
181
+ @space.do_not_allow_creator(@object, :foobar) {}
182
+ end.should raise_error(ArgumentError, "Cannot pass in a method name and a block")
183
+ end
184
+
126
185
  it "uses block definition when passed a block" do
127
- creator = @space.create_do_not_allow_creator(@object) do |c|
186
+ creator = @space.do_not_allow_creator(@object) do |c|
128
187
  c.foobar(1)
129
188
  end
130
189
  proc {@object.foobar(1)}.should raise_error(Errors::TimesCalledError)
data/lib/rr/creator.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  module RR
2
2
  # RR::Creator is the superclass for all creators.
3
3
  class Creator
4
- def initialize(space, subject)
4
+ def initialize(space, subject, &block)
5
5
  @space = space
6
6
  @subject = subject
7
7
  class << self
@@ -12,26 +12,77 @@ module Extensions
12
12
  RR::Space.instance.reset
13
13
  end
14
14
 
15
- # Sets up a MockCreator that generates a Double Scenario that
16
- # acts like a mock.
17
- # mock(object).method_name(arg1, arg2) {return_value}
18
- def mock(subject, &definition)
19
- RR::Space.instance.create_mock_creator(subject, &definition)
15
+ # When passed the object, this method returns a MockCreator
16
+ # that generates a Double Scenario that acts like a mock.
17
+ # mock(object).method_name_1 {return_value_1}
18
+ # mock(object).method_name_2(arg1, arg2) {return_value_2}
19
+ #
20
+ # When passed the object and the method_name, this method returns
21
+ # a mock Scenario with the method already set.
22
+ #
23
+ # mock also takes a block for definitions.
24
+ # mock(object) do
25
+ # method_name_1 {return_value_1}
26
+ # method_name_2(arg_1, arg_2) {return_value_2}
27
+ # end
28
+ def mock(object, method_name=nil, &definition)
29
+ RR::Space.instance.mock_creator(object, method_name, &definition)
20
30
  end
21
31
 
22
- # Sets up a StubCreator that generates a Double Scenario that
23
- # acts like a stub.
24
- # stub(object).method_name {return_value}
25
- def stub(subject, &definition)
26
- RR::Space.instance.create_stub_creator(subject, &definition)
32
+ # When passed the object, this method returns a StubCreator
33
+ # that generates a Double Scenario that acts like a stub.
34
+ # stub(object).method_name_1 {return_value_1}
35
+ # stub(object).method_name_2(arg_1, arg_2) {return_value_2}
36
+ #
37
+ # When passed the object and the method_name, this method returns
38
+ # a stub Scenario with the method already set.
39
+ #
40
+ # stub also takes a block for definitions.
41
+ # stub(object) do
42
+ # method_name_1 {return_value_1}
43
+ # method_name_2(arg_1, arg_2) {return_value_2}
44
+ # end
45
+ def stub(object, method_name=nil, &definition)
46
+ RR::Space.instance.stub_creator(object, method_name, &definition)
27
47
  end
28
48
 
29
- # Sets up a MockProbeCreator that generates a Double Scenario that
30
- # acts like mock verifications while calling the actual method.
49
+ # When passed the object, this method returns a MockProbeCreator
50
+ # that generates a Double Scenario that acts like a mock probe.
31
51
  #
32
52
  # mock_probe(controller.template).render(:partial => "my/socks")
33
53
  #
34
- # Passing a block allows you to intercept the return value.
54
+ # mock_probe(controller.template).render(:partial => "my/socks") do |html|
55
+ # html.should include("My socks are wet")
56
+ # html
57
+ # end
58
+ #
59
+ # mock_probe(controller.template).render(:partial => "my/socks") do |html|
60
+ # html.should include("My socks are wet")
61
+ # "My new return value"
62
+ # end
63
+ #
64
+ # mock_probe also takes a block for definitions.
65
+ # mock_probe(object) do
66
+ # render(:partial => "my/socks")
67
+ #
68
+ # render(:partial => "my/socks") do |html|
69
+ # html.should include("My socks are wet")
70
+ # html
71
+ # end
72
+ #
73
+ # render(:partial => "my/socks") do |html|
74
+ # html.should include("My socks are wet")
75
+ # html
76
+ # end
77
+ #
78
+ # render(:partial => "my/socks") do |html|
79
+ # html.should include("My socks are wet")
80
+ # "My new return value"
81
+ # end
82
+ # end
83
+ #
84
+ # Passing a block to the Scenario (after the method name and arguments)
85
+ # allows you to intercept the return value.
35
86
  # The return value can be modified, validated, and/or overridden by
36
87
  # passing in a block. The return value of the block will replace
37
88
  # the actual return value.
@@ -40,26 +91,55 @@ module Extensions
40
91
  # html.should include("My socks are wet")
41
92
  # "My new return value"
42
93
  # end
43
- def mock_probe(subject, &definition)
44
- RR::Space.instance.create_mock_probe_creator(subject, &definition)
94
+ def mock_probe(object, method_name=nil, &definition)
95
+ RR::Space.instance.mock_probe_creator(object, method_name, &definition)
45
96
  end
46
97
 
47
- # Sets up a StubProbeCreator that generates a Double Scenario that
48
- # acts like mock verifications while calling the actual method.
98
+ # When passed the object, this method returns a StubProbeCreator
99
+ # that generates a Double Scenario that acts like a stub probe.
49
100
  #
50
- # mock_probe(User).new {|user| my_user}
101
+ # stub_probe(User).new {|user| user}
102
+ #
103
+ # stub_probe(User).new do |user|
104
+ # mock(user).valid? {false}
105
+ # user
106
+ # end
107
+ #
108
+ # stub_probe(User).new do |user|
109
+ # mock_probe(user).friends {|friends| friends[0..3]}
110
+ # user
111
+ # end
51
112
  #
52
113
  # Passing a block allows you to intercept the return value.
53
114
  # The return value can be modified, validated, and/or overridden by
54
115
  # passing in a block. The return value of the block will replace
55
116
  # the actual return value.
56
117
  #
57
- # mock_probe(controller.template).render(:partial => "my/socks") do |html|
118
+ # mock_probe(User) do
119
+ # new {|user| user}
120
+ #
121
+ # new do |user|
122
+ # mock(user).valid? {false}
123
+ # end
124
+ #
125
+ # new do |user|
126
+ # mock_probe(user).friends {|friends| friends[0..3]}
127
+ # user
128
+ # end
129
+ # end
130
+ #
131
+ # Passing a block to the Scenario (after the method name and arguments)
132
+ # allows you to intercept the return value.
133
+ # The return value can be modified, validated, and/or overridden by
134
+ # passing in a block. The return value of the block will replace
135
+ # the actual return value.
136
+ #
137
+ # stub_probe(controller.template).render(:partial => "my/socks") do |html|
58
138
  # html.should include("My socks are wet")
59
139
  # "My new return value"
60
140
  # end
61
- def stub_probe(subject, &definition)
62
- RR::Space.instance.create_stub_probe_creator(subject, &definition)
141
+ def stub_probe(object, method_name=nil, &definition)
142
+ RR::Space.instance.stub_probe_creator(object, method_name, &definition)
63
143
  end
64
144
 
65
145
  # Same as mock_probe
@@ -68,8 +148,8 @@ module Extensions
68
148
  # Sets up a DoNotAllowCreator that generates a Double Scenario that
69
149
  # expects never to be called.
70
150
  # do_not_allow(object).method_name
71
- def do_not_allow(subject, &definition)
72
- RR::Space.instance.create_do_not_allow_creator(subject, &definition)
151
+ def do_not_allow(subject, method_name=nil, &definition)
152
+ RR::Space.instance.do_not_allow_creator(subject, method_name, &definition)
73
153
  end
74
154
  alias_method :dont_allow, :do_not_allow
75
155
 
data/lib/rr/space.rb CHANGED
@@ -26,28 +26,28 @@ module RR
26
26
  end
27
27
 
28
28
  # Creates a MockCreator.
29
- def create_mock_creator(subject, &definition)
30
- MockCreator.new(self, subject, &definition)
29
+ def mock_creator(subject, method_name=nil, &definition)
30
+ setup_creator MockCreator, subject, method_name, definition
31
31
  end
32
32
 
33
33
  # Creates a StubCreator.
34
- def create_stub_creator(subject, &definition)
35
- StubCreator.new(self, subject, &definition)
34
+ def stub_creator(subject, method_name=nil, &definition)
35
+ setup_creator StubCreator, subject, method_name, definition
36
36
  end
37
37
 
38
38
  # Creates a MockProbeCreator.
39
- def create_mock_probe_creator(subject, &definition)
40
- MockProbeCreator.new(self, subject, &definition)
39
+ def mock_probe_creator(subject, method_name=nil, &definition)
40
+ setup_creator MockProbeCreator, subject, method_name, definition
41
41
  end
42
42
 
43
43
  # Creates a StubProbeCreator.
44
- def create_stub_probe_creator(subject, &definition)
45
- StubProbeCreator.new(self, subject, &definition)
44
+ def stub_probe_creator(subject, method_name=nil, &definition)
45
+ setup_creator StubProbeCreator, subject, method_name, definition
46
46
  end
47
47
 
48
48
  # Creates a DoNotAllowCreator.
49
- def create_do_not_allow_creator(subject, &definition)
50
- DoNotAllowCreator.new(self, subject, &definition)
49
+ def do_not_allow_creator(subject, method_name=nil, &definition)
50
+ setup_creator DoNotAllowCreator, subject, method_name, definition
51
51
  end
52
52
 
53
53
  # Creates and registers a Scenario to be verified.
@@ -124,6 +124,15 @@ module RR
124
124
  end
125
125
 
126
126
  protected
127
+ def setup_creator(klass, subject, method_name, definition)
128
+ if method_name && definition
129
+ raise ArgumentError, "Cannot pass in a method name and a block"
130
+ end
131
+ creator = klass.new(self, subject, &definition)
132
+ return creator unless method_name
133
+ creator.__send__(method_name)
134
+ end
135
+
127
136
  # Removes the ordered Scenarios from the list
128
137
  def reset_ordered_scenarios
129
138
  @ordered_scenarios.clear
metadata CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.9.3
3
3
  specification_version: 1
4
4
  name: rr
5
5
  version: !ruby/object:Gem::Version
6
- version: 0.2.4
7
- date: 2007-07-19 00:00:00 -07:00
6
+ version: 0.2.5
7
+ date: 2007-07-21 00:00:00 -07:00
8
8
  summary: RR (Double Ruby) is a double framework that features a rich selection of double techniques and a terse syntax. http://xunitpatterns.com/Test%20Double.html
9
9
  require_paths:
10
10
  - lib