rr 0.2.5 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (40) hide show
  1. data/CHANGES +5 -0
  2. data/README +20 -9
  3. data/Rakefile +1 -1
  4. data/examples/example_suite.rb +1 -1
  5. data/examples/high_level_example.rb +4 -4
  6. data/examples/rr/double/double_dispatching_example.rb +41 -41
  7. data/examples/rr/double/double_verify_example.rb +1 -1
  8. data/examples/rr/expectations/times_called_expectation/times_called_expectation_helper.rb +2 -2
  9. data/examples/rr/extensions/instance_methods_creator_example.rb +48 -38
  10. data/examples/rr/extensions/instance_methods_space_example.rb +8 -8
  11. data/examples/rr/rspec/rspec_adapter_example.rb +9 -9
  12. data/examples/rr/rspec/rspec_usage_example.rb +16 -2
  13. data/examples/rr/scenario_creator_example.rb +400 -0
  14. data/examples/rr/scenario_example.rb +19 -4
  15. data/examples/rr/scenario_method_proxy_example.rb +71 -0
  16. data/examples/rr/space/space_create_example.rb +93 -106
  17. data/examples/rr/space/space_example.rb +2 -2
  18. data/examples/rr/space/space_register_example.rb +3 -3
  19. data/examples/rr/space/space_reset_example.rb +11 -11
  20. data/examples/rr/space/space_verify_example.rb +12 -12
  21. data/examples/rr/test_unit/test_unit_integration_test.rb +11 -2
  22. data/lib/rr.rb +10 -12
  23. data/lib/rr/errors/scenario_definition_error.rb +6 -0
  24. data/lib/rr/extensions/instance_methods.rb +84 -84
  25. data/lib/rr/scenario.rb +18 -3
  26. data/lib/rr/scenario_creator.rb +233 -0
  27. data/lib/rr/scenario_method_proxy.rb +19 -0
  28. data/lib/rr/space.rb +12 -32
  29. metadata +7 -13
  30. data/examples/rr/do_not_allow_creator_example.rb +0 -111
  31. data/examples/rr/mock_creator_example.rb +0 -87
  32. data/examples/rr/mock_probe_creator_example.rb +0 -120
  33. data/examples/rr/stub_creator_example.rb +0 -96
  34. data/examples/rr/stub_probe_creator_example.rb +0 -127
  35. data/lib/rr/creator.rb +0 -16
  36. data/lib/rr/do_not_allow_creator.rb +0 -33
  37. data/lib/rr/mock_creator.rb +0 -26
  38. data/lib/rr/mock_probe_creator.rb +0 -36
  39. data/lib/rr/stub_creator.rb +0 -30
  40. data/lib/rr/stub_probe_creator.rb +0 -42
@@ -31,7 +31,7 @@ module Extensions
31
31
  end
32
32
 
33
33
  def verify_all_doubles
34
- double1 = @space.create_double(@object1, @method_name)
34
+ double1 = @space.double(@object1, @method_name)
35
35
  double1_verify_calls = 0
36
36
  double1_reset_calls = 0
37
37
  (class << double1; self; end).class_eval do
@@ -42,7 +42,7 @@ module Extensions
42
42
  double1_reset_calls += 1
43
43
  end
44
44
  end
45
- double2 = @space.create_double(@object2, @method_name)
45
+ double2 = @space.double(@object2, @method_name)
46
46
  double2_verify_calls = 0
47
47
  double2_reset_calls = 0
48
48
  (class << double2; self; end).class_eval do
@@ -82,11 +82,11 @@ module Extensions
82
82
  end
83
83
 
84
84
  def remove_ordered_scenarios
85
- double1 = @space.create_double(@object1, :foobar1)
86
- double2 = @space.create_double(@object1, :foobar2)
85
+ double1 = @space.double(@object1, :foobar1)
86
+ double2 = @space.double(@object1, :foobar2)
87
87
 
88
- scenario1 = @space.create_scenario(double1)
89
- scenario2 = @space.create_scenario(double2)
88
+ scenario1 = @space.scenario(double1)
89
+ scenario2 = @space.scenario(double2)
90
90
 
91
91
  scenario1.ordered
92
92
  scenario2.ordered
@@ -98,14 +98,14 @@ module Extensions
98
98
  end
99
99
 
100
100
  def reset_all_doubles
101
- double1 = @space.create_double(@object1, @method_name)
101
+ double1 = @space.double(@object1, @method_name)
102
102
  double1_reset_calls = 0
103
103
  (class << double1; self; end).class_eval do
104
104
  define_method(:reset) do ||
105
105
  double1_reset_calls += 1
106
106
  end
107
107
  end
108
- double2 = @space.create_double(@object2, @method_name)
108
+ double2 = @space.double(@object2, @method_name)
109
109
  double2_reset_calls = 0
110
110
  (class << double2; self; end).class_eval do
111
111
  define_method(:reset) do ||
@@ -12,11 +12,11 @@ module Adapters
12
12
  end
13
13
 
14
14
  it "resets the doubles" do
15
- RR::Space.instance.create_double(@subject, @method_name)
16
- RR::Space.instance.doubles.should_not be_empty
15
+ RR::Space.double(@subject, @method_name)
16
+ RR::Space.doubles.should_not be_empty
17
17
 
18
18
  @fixture.setup_mocks_for_rspec
19
- RR::Space.instance.doubles.should be_empty
19
+ RR::Space.doubles.should be_empty
20
20
  end
21
21
  end
22
22
 
@@ -30,15 +30,15 @@ module Adapters
30
30
  end
31
31
 
32
32
  it "verifies the doubles" do
33
- double = RR::Space.instance.create_double(@subject, @method_name)
34
- scenario = RR::Space.instance.create_scenario(double)
33
+ double = RR::Space.double(@subject, @method_name)
34
+ scenario = RR::Space.scenario(double)
35
35
 
36
36
  scenario.once
37
37
 
38
38
  proc do
39
39
  @fixture.verify_mocks_for_rspec
40
40
  end.should raise_error(::RR::Errors::TimesCalledError)
41
- RR::Space.instance.doubles.should be_empty
41
+ RR::Space.doubles.should be_empty
42
42
  end
43
43
  end
44
44
 
@@ -52,11 +52,11 @@ module Adapters
52
52
  end
53
53
 
54
54
  it "resets the doubles" do
55
- RR::Space.instance.create_double(@subject, @method_name)
56
- RR::Space.instance.doubles.should_not be_empty
55
+ RR::Space.double(@subject, @method_name)
56
+ RR::Space.doubles.should_not be_empty
57
57
 
58
58
  @fixture.teardown_mocks_for_rspec
59
- RR::Space.instance.doubles.should be_empty
59
+ RR::Space.doubles.should be_empty
60
60
  end
61
61
  end
62
62
  end
@@ -22,7 +22,7 @@ describe RR, "#stub" do
22
22
  end
23
23
  end
24
24
 
25
- describe RR, "#probe" do
25
+ describe RR, "#mock and #probe" do
26
26
  before do
27
27
  @subject = Object.new
28
28
  def @subject.foobar
@@ -31,7 +31,21 @@ describe RR, "#probe" do
31
31
  end
32
32
 
33
33
  it "creates a probe Double Scenario" do
34
- probe(@subject).foobar
34
+ mock.probe(@subject).foobar
35
+ @subject.foobar.should == :baz
36
+ end
37
+ end
38
+
39
+ describe RR, "#stub and #probe" do
40
+ before do
41
+ @subject = Object.new
42
+ def @subject.foobar
43
+ :baz
44
+ end
45
+ end
46
+
47
+ it "creates a probe Double Scenario" do
48
+ stub.probe(@subject).foobar
35
49
  @subject.foobar.should == :baz
36
50
  end
37
51
  end
@@ -0,0 +1,400 @@
1
+ require "examples/example_helper"
2
+
3
+ module RR
4
+ describe ScenarioCreator, :shared => true do
5
+ before(:each) do
6
+ @space = Space.new
7
+ @subject = Object.new
8
+ @creator = ScenarioCreator.new(@space)
9
+ end
10
+ end
11
+
12
+ describe ScenarioCreator, " strategy definition", :shared => true do
13
+ it_should_behave_like "RR::ScenarioCreator"
14
+
15
+ it "returns self when passing no args" do
16
+ @creator.__send__(@method_name).should === @creator
17
+ end
18
+
19
+ it "returns a ScenarioMethodProxy when passed a subject" do
20
+ scenario = @creator.__send__(@method_name, @subject).foobar
21
+ scenario.should be_instance_of(Scenario)
22
+ end
23
+
24
+ it "raises error if passed a method name and a block" do
25
+ proc do
26
+ @creator.__send__(@method_name, @subject, :foobar) {}
27
+ end.should raise_error(ArgumentError, "Cannot pass in a method name and a block")
28
+ end
29
+
30
+ it "raises error when using mock strategy" do
31
+ @creator.mock
32
+ proc do
33
+ @creator.__send__(@method_name)
34
+ end.should raise_error(
35
+ Errors::ScenarioDefinitionError,
36
+ "This Scenario already has a mock strategy"
37
+ )
38
+ end
39
+
40
+ it "raises error when using stub strategy" do
41
+ @creator.stub
42
+ proc do
43
+ @creator.__send__(@method_name)
44
+ end.should raise_error(
45
+ Errors::ScenarioDefinitionError,
46
+ "This Scenario already has a stub strategy"
47
+ )
48
+ end
49
+
50
+ it "raises error when using do_not_call strategy" do
51
+ @creator.do_not_call
52
+ proc do
53
+ @creator.__send__(@method_name)
54
+ end.should raise_error(
55
+ Errors::ScenarioDefinitionError,
56
+ "This Scenario already has a do_not_call strategy"
57
+ )
58
+ end
59
+ end
60
+
61
+ describe ScenarioCreator, "#mock" do
62
+ it_should_behave_like "RR::ScenarioCreator strategy definition"
63
+
64
+ before do
65
+ @method_name = :mock
66
+ end
67
+
68
+ it "sets up the RR mock call chain" do
69
+ should create_mock_call_chain(@creator.mock(@subject))
70
+ end
71
+
72
+ it "creates a mock Scenario for method when passed a second argument with rr_mock" do
73
+ should create_scenario_with_method_name(
74
+ @creator.mock(@subject, :foobar)
75
+ )
76
+ end
77
+
78
+ def create_scenario_with_method_name(scenario)
79
+ method_name = scenario.method_name
80
+ scenario.with(1, 2) {:baz}
81
+ scenario.times_matcher.should == TimesCalledMatchers::IntegerMatcher.new(1)
82
+ scenario.argument_expectation.class.should == RR::Expectations::ArgumentEqualityExpectation
83
+ scenario.argument_expectation.expected_arguments.should == [1, 2]
84
+
85
+ @subject.__send__(method_name, 1, 2).should == :baz
86
+ end
87
+
88
+ def create_mock_call_chain(creator)
89
+ scenario = creator.foobar(1, 2) {:baz}
90
+ scenario.times_matcher.should == TimesCalledMatchers::IntegerMatcher.new(1)
91
+ scenario.argument_expectation.class.should == RR::Expectations::ArgumentEqualityExpectation
92
+ scenario.argument_expectation.expected_arguments.should == [1, 2]
93
+
94
+ @subject.foobar(1, 2).should == :baz
95
+ end
96
+ end
97
+
98
+ describe ScenarioCreator, "#stub" do
99
+ it_should_behave_like "RR::ScenarioCreator strategy definition"
100
+
101
+ before do
102
+ @method_name = :stub
103
+ end
104
+
105
+ it "sets up the RR stub call chain" do
106
+ should create_stub_call_chain(@creator.stub(@subject))
107
+ end
108
+
109
+ it "creates a stub Scenario for method when passed a second argument" do
110
+ should create_scenario_with_method_name(@creator.stub(@subject, :foobar))
111
+ end
112
+
113
+ def create_scenario_with_method_name(scenario)
114
+ method_name = scenario.method_name
115
+ scenario.with(1, 2) {:baz}
116
+ scenario.times_matcher.should == TimesCalledMatchers::AnyTimesMatcher.new
117
+ scenario.argument_expectation.class.should == RR::Expectations::ArgumentEqualityExpectation
118
+ @subject.__send__(method_name, 1, 2).should == :baz
119
+ end
120
+
121
+ def create_stub_call_chain(creator)
122
+ scenario = creator.foobar(1, 2) {:baz}
123
+ scenario.times_matcher.should == TimesCalledMatchers::AnyTimesMatcher.new
124
+ scenario.argument_expectation.class.should == RR::Expectations::ArgumentEqualityExpectation
125
+ @subject.foobar(1, 2).should == :baz
126
+ end
127
+ end
128
+
129
+ describe ScenarioCreator, "#do_not_call" do
130
+ it_should_behave_like "RR::ScenarioCreator strategy definition"
131
+
132
+ before do
133
+ @method_name = :do_not_call
134
+ end
135
+
136
+ it "raises error when probed" do
137
+ @creator.probe
138
+ proc do
139
+ @creator.do_not_call
140
+ end.should raise_error(
141
+ Errors::ScenarioDefinitionError,
142
+ "Scenarios cannot be probed when using do_not_call strategy"
143
+ )
144
+ end
145
+
146
+ it "sets up the RR do_not_call call chain" do
147
+ should create_do_not_call_call_chain(@creator.do_not_call(@subject))
148
+ end
149
+
150
+ it "sets up the RR do_not_call call chain" do
151
+ should create_do_not_call_call_chain(@creator.dont_call(@subject))
152
+ end
153
+
154
+ it "sets up the RR do_not_call call chain" do
155
+ should create_do_not_call_call_chain(@creator.do_not_allow(@subject))
156
+ end
157
+
158
+ it "sets up the RR do_not_call call chain" do
159
+ should create_do_not_call_call_chain(@creator.dont_allow(@subject))
160
+ end
161
+
162
+ it "creates a mock Scenario for method when passed a second argument" do
163
+ should create_scenario_with_method_name(@creator.do_not_call(@subject, :foobar))
164
+ end
165
+
166
+ it "creates a mock Scenario for method when passed a second argument" do
167
+ should create_scenario_with_method_name(@creator.dont_call(@subject, :foobar))
168
+ end
169
+
170
+ it "creates a mock Scenario for method when passed a second argument" do
171
+ should create_scenario_with_method_name(@creator.do_not_allow(@subject, :foobar))
172
+ end
173
+
174
+ it "creates a mock Scenario for method when passed a second argument" do
175
+ should create_scenario_with_method_name(@creator.dont_allow(@subject, :foobar))
176
+ end
177
+
178
+ def create_scenario_with_method_name(scenario)
179
+ method_name = scenario.method_name
180
+ scenario.with(1, 2)
181
+ scenario.times_matcher.should == TimesCalledMatchers::IntegerMatcher.new(0)
182
+ scenario.argument_expectation.class.should == RR::Expectations::ArgumentEqualityExpectation
183
+ scenario.argument_expectation.expected_arguments.should == [1, 2]
184
+
185
+ proc do
186
+ @subject.__send__(method_name, 1, 2)
187
+ end.should raise_error(Errors::TimesCalledError)
188
+ reset
189
+ nil
190
+ end
191
+
192
+ def create_do_not_call_call_chain(creator)
193
+ scenario = creator.foobar(1, 2)
194
+ scenario.times_matcher.should == TimesCalledMatchers::IntegerMatcher.new(0)
195
+ scenario.argument_expectation.class.should == RR::Expectations::ArgumentEqualityExpectation
196
+ scenario.argument_expectation.expected_arguments.should == [1, 2]
197
+
198
+ proc do
199
+ @subject.foobar(1, 2)
200
+ end.should raise_error(Errors::TimesCalledError)
201
+ reset
202
+ nil
203
+ end
204
+ end
205
+
206
+ describe ScenarioCreator, "#probe" do
207
+ it_should_behave_like "RR::ScenarioCreator"
208
+
209
+ it "raises error when using do_not_call strategy" do
210
+ @creator.do_not_call
211
+ proc do
212
+ @creator.probe
213
+ end.should raise_error(
214
+ Errors::ScenarioDefinitionError,
215
+ "Scenarios cannot be probed when using do_not_call strategy"
216
+ )
217
+ end
218
+
219
+ it "sets up the RR stub call chain" do
220
+ should create_probe_call_chain(@creator.stub.probe(@subject))
221
+ end
222
+
223
+ it "creates a probe Scenario for method when passed a second argument" do
224
+ should create_scenario_with_method_name(@creator.stub.probe(@subject, :foobar))
225
+ end
226
+
227
+ def create_scenario_with_method_name(scenario)
228
+ method_name = scenario.method_name
229
+ scenario.with(1, 2) {:baz}
230
+ scenario.times_matcher.should == TimesCalledMatchers::AnyTimesMatcher.new
231
+ scenario.argument_expectation.class.should == RR::Expectations::ArgumentEqualityExpectation
232
+ @subject.__send__(method_name, 1, 2).should == :baz
233
+ end
234
+
235
+ def create_probe_call_chain(creator)
236
+ scenario = creator.foobar(1, 2) {:baz}
237
+ scenario.times_matcher.should == TimesCalledMatchers::AnyTimesMatcher.new
238
+ scenario.argument_expectation.class.should == RR::Expectations::ArgumentEqualityExpectation
239
+ @subject.foobar(1, 2).should == :baz
240
+ end
241
+ end
242
+
243
+ describe ScenarioCreator, "#create! using no strategy" do
244
+ it_should_behave_like "RR::ScenarioCreator"
245
+
246
+ it "raises error" do
247
+ proc do
248
+ @creator.create!(@subject, :foobar, 1, 2)
249
+ end.should raise_error(
250
+ Errors::ScenarioDefinitionError,
251
+ "This Scenario has no strategy"
252
+ )
253
+ end
254
+ end
255
+
256
+ describe ScenarioCreator, "#create!", :shared => true do
257
+ it_should_behave_like "RR::ScenarioCreator"
258
+
259
+ it "initializes creator with passed in object" do
260
+ @creator.create!(@subject, :foobar)
261
+ @creator.subject.should === @subject
262
+ end
263
+ end
264
+
265
+ describe ScenarioCreator, "#create! using mock strategy" do
266
+ it_should_behave_like "RR::ScenarioCreator#create!"
267
+
268
+ before do
269
+ @creator.mock
270
+ end
271
+
272
+ it "sets expectations on the subject" do
273
+ @creator.create!(@subject, :foobar, 1, 2) {:baz}.twice
274
+
275
+ @subject.foobar(1, 2).should == :baz
276
+ @subject.foobar(1, 2).should == :baz
277
+ proc {@subject.foobar(1, 2)}.should raise_error(Errors::TimesCalledError)
278
+ end
279
+ end
280
+
281
+ describe ScenarioCreator, "#create! using stub strategy" do
282
+ it_should_behave_like "RR::ScenarioCreator#create!"
283
+
284
+ before do
285
+ @creator.stub
286
+ end
287
+
288
+ it "stubs the subject without any args" do
289
+ @creator.create!(@subject, :foobar) {:baz}
290
+ @subject.foobar.should == :baz
291
+ end
292
+
293
+ it "stubs the subject mapping passed in args with the output" do
294
+ @creator.create!(@subject, :foobar, 1, 2) {:one_two}
295
+ @creator.create!(@subject, :foobar, 1) {:one}
296
+ @creator.create!(@subject, :foobar) {:nothing}
297
+ @subject.foobar.should == :nothing
298
+ @subject.foobar(1).should == :one
299
+ @subject.foobar(1, 2).should == :one_two
300
+ end
301
+ end
302
+
303
+ describe ScenarioCreator, "#create! using do_not_call strategy" do
304
+ it_should_behave_like "RR::ScenarioCreator#create!"
305
+
306
+ before do
307
+ @creator.do_not_call
308
+ end
309
+
310
+ it "sets expectation for method to never be called with any arguments when on arguments passed in" do
311
+ @creator.create!(@subject, :foobar)
312
+ proc {@subject.foobar}.should raise_error(Errors::TimesCalledError)
313
+ proc {@subject.foobar(1, 2)}.should raise_error(Errors::TimesCalledError)
314
+ end
315
+
316
+ it "sets expectation for method to never be called with passed in arguments" do
317
+ @creator.create!(@subject, :foobar, 1, 2)
318
+ proc {@subject.foobar}.should raise_error(Errors::ScenarioNotFoundError)
319
+ proc {@subject.foobar(1, 2)}.should raise_error(Errors::TimesCalledError)
320
+ end
321
+
322
+ it "sets expectation for method to never be called with no arguments when with_no_args is set" do
323
+ @creator.create!(@subject, :foobar).with_no_args
324
+ proc {@subject.foobar}.should raise_error(Errors::TimesCalledError)
325
+ proc {@subject.foobar(1, 2)}.should raise_error(Errors::ScenarioNotFoundError)
326
+ end
327
+ end
328
+
329
+ describe ScenarioCreator, "#create! using mock strategy with probe" do
330
+ it_should_behave_like "RR::ScenarioCreator#create!"
331
+
332
+ before do
333
+ @creator.mock
334
+ @creator.probe
335
+ end
336
+
337
+ it "sets expectations on the subject while calling the original method" do
338
+ def @subject.foobar(*args); :baz; end
339
+ @creator.create!(@subject, :foobar,1, 2).twice
340
+ @subject.foobar(1, 2).should == :baz
341
+ @subject.foobar(1, 2).should == :baz
342
+ proc {@subject.foobar(1, 2)}.should raise_error(Errors::TimesCalledError)
343
+ end
344
+
345
+ it "sets after_call on the scenario when passed a block" do
346
+ real_value = Object.new
347
+ (class << @subject; self; end).class_eval do
348
+ define_method(:foobar) {real_value}
349
+ end
350
+ @creator.create!(@subject, :foobar, 1, 2) do |value|
351
+ mock(value).a_method {99}
352
+ value
353
+ end
354
+
355
+ return_value = @subject.foobar(1, 2)
356
+ return_value.should === return_value
357
+ return_value.a_method.should == 99
358
+ end
359
+ end
360
+
361
+ describe ScenarioCreator, "#create! using stub strategy with probe" do
362
+ it_should_behave_like "RR::ScenarioCreator#create!"
363
+
364
+ before do
365
+ @creator.stub
366
+ @creator.probe
367
+ end
368
+
369
+ it "sets up a scenario with passed in arguments" do
370
+ def @subject.foobar(*args); :baz; end
371
+ @creator.create!(@subject, :foobar, 1, 2)
372
+ proc do
373
+ @subject.foobar
374
+ end.should raise_error(Errors::ScenarioNotFoundError)
375
+ end
376
+
377
+ it "sets expectations on the subject while calling the original method" do
378
+ def @subject.foobar(*args); :baz; end
379
+ @creator.create!(@subject, :foobar, 1, 2) {:new_value}
380
+ 10.times do
381
+ @subject.foobar(1, 2).should == :new_value
382
+ end
383
+ end
384
+
385
+ it "sets after_call on the scenario when passed a block" do
386
+ real_value = Object.new
387
+ (class << @subject; self; end).class_eval do
388
+ define_method(:foobar) {real_value}
389
+ end
390
+ @creator.create!(@subject, :foobar, 1, 2) do |value|
391
+ mock(value).a_method {99}
392
+ value
393
+ end
394
+
395
+ return_value = @subject.foobar(1, 2)
396
+ return_value.should === return_value
397
+ return_value.a_method.should == 99
398
+ end
399
+ end
400
+ end