rr 0.3.11 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGES +8 -3
- data/README +130 -39
- data/Rakefile +6 -5
- data/lib/rr.rb +8 -8
- data/lib/rr/adapters/rr_methods.rb +228 -0
- data/lib/rr/adapters/rspec.rb +1 -1
- data/lib/rr/adapters/test_unit.rb +1 -1
- data/lib/rr/double.rb +294 -89
- data/lib/rr/{scenario_creator.rb → double_creator.rb} +65 -66
- data/lib/rr/{scenario_definition.rb → double_definition.rb} +36 -36
- data/lib/rr/{scenario_definition_builder.rb → double_definition_builder.rb} +3 -3
- data/lib/rr/double_insertion.rb +132 -0
- data/lib/rr/{scenario_matches.rb → double_matches.rb} +2 -2
- data/lib/rr/{scenario_method_proxy.rb → double_method_proxy.rb} +2 -2
- data/lib/rr/errors/argument_equality_error.rb +3 -3
- data/lib/rr/errors/rr_error.rb +13 -13
- data/lib/rr/errors/scenario_definition_error.rb +3 -3
- data/lib/rr/errors/scenario_not_found_error.rb +3 -3
- data/lib/rr/errors/scenario_order_error.rb +3 -3
- data/lib/rr/errors/times_called_error.rb +3 -3
- data/lib/rr/expectations/any_argument_expectation.rb +1 -1
- data/lib/rr/expectations/argument_equality_expectation.rb +1 -1
- data/lib/rr/expectations/times_called_expectation.rb +1 -1
- data/lib/rr/hash_with_object_id_key.rb +1 -1
- data/lib/rr/space.rb +40 -40
- data/lib/rr/times_called_matchers/any_times_matcher.rb +13 -13
- data/lib/rr/times_called_matchers/at_least_matcher.rb +11 -11
- data/lib/rr/times_called_matchers/at_most_matcher.rb +16 -16
- data/lib/rr/times_called_matchers/integer_matcher.rb +13 -13
- data/lib/rr/times_called_matchers/non_terminal.rb +21 -21
- data/lib/rr/times_called_matchers/proc_matcher.rb +7 -7
- data/lib/rr/times_called_matchers/range_matcher.rb +14 -14
- data/lib/rr/times_called_matchers/terminal.rb +16 -16
- data/lib/rr/times_called_matchers/times_called_matcher.rb +32 -32
- data/spec/core_spec_suite.rb +18 -0
- data/{examples → spec}/environment_fixture_setup.rb +0 -1
- data/{examples/high_level_example.rb → spec/high_level_spec.rb} +11 -11
- data/spec/rr/adapters/rr_methods_argument_matcher_spec.rb +67 -0
- data/spec/rr/adapters/rr_methods_creator_spec.rb +365 -0
- data/spec/rr/adapters/rr_methods_space_spec.rb +134 -0
- data/spec/rr/adapters/rr_methods_spec_helper.rb +11 -0
- data/{examples/rr/extensions/instance_methods_times_matcher_example.rb → spec/rr/adapters/rr_methods_times_matcher_spec.rb} +4 -4
- data/spec/rr/double/double_insertion_bind_spec.rb +78 -0
- data/spec/rr/double/double_insertion_dispatching_spec.rb +221 -0
- data/spec/rr/double/double_insertion_has_original_method_spec.rb +56 -0
- data/spec/rr/double/double_insertion_register_scenario_spec.rb +24 -0
- data/spec/rr/double/double_insertion_reset_spec.rb +89 -0
- data/spec/rr/double/double_insertion_spec.rb +66 -0
- data/spec/rr/double/double_insertion_verify_spec.rb +23 -0
- data/spec/rr/double_creator_spec.rb +454 -0
- data/{examples/rr/scenario_definition_example.rb → spec/rr/double_definition_spec.rb} +143 -143
- data/spec/rr/double_method_proxy_spec.rb +71 -0
- data/spec/rr/double_spec.rb +654 -0
- data/spec/rr/errors/rr_error_spec.rb +65 -0
- data/spec/rr/expectations/any_argument_expectation_spec.rb +47 -0
- data/spec/rr/expectations/anything_argument_equality_expectation_spec.rb +38 -0
- data/spec/rr/expectations/argument_equality_expectation_spec.rb +58 -0
- data/spec/rr/expectations/boolean_argument_equality_expectation_spec.rb +53 -0
- data/spec/rr/expectations/duck_type_argument_equality_expectation_spec.rb +71 -0
- data/spec/rr/expectations/is_a_argument_equality_expectation_spec.rb +51 -0
- data/spec/rr/expectations/numeric_argument_equality_expectation_spec.rb +47 -0
- data/spec/rr/expectations/range_argument_equality_expectation_spec.rb +59 -0
- data/spec/rr/expectations/regexp_argument_equality_expectation_spec.rb +72 -0
- data/spec/rr/expectations/times_called_expectation/times_called_expectation_any_times_spec.rb +43 -0
- data/spec/rr/expectations/times_called_expectation/times_called_expectation_at_least_spec.rb +67 -0
- data/spec/rr/expectations/times_called_expectation/times_called_expectation_at_most_spec.rb +71 -0
- data/spec/rr/expectations/times_called_expectation/times_called_expectation_helper.rb +21 -0
- data/spec/rr/expectations/times_called_expectation/times_called_expectation_integer_spec.rb +103 -0
- data/spec/rr/expectations/times_called_expectation/times_called_expectation_proc_spec.rb +79 -0
- data/spec/rr/expectations/times_called_expectation/times_called_expectation_range_spec.rb +81 -0
- data/spec/rr/expectations/times_called_expectation/times_called_expectation_spec.rb +40 -0
- data/spec/rr/rspec/rspec_adapter_spec.rb +65 -0
- data/spec/rr/rspec/rspec_backtrace_tweaking_spec.rb +52 -0
- data/spec/rr/rspec/rspec_usage_spec.rb +67 -0
- data/spec/rr/space/hash_with_object_id_key_spec.rb +88 -0
- data/spec/rr/space/space_create_spec.rb +278 -0
- data/spec/rr/space/space_helper.rb +7 -0
- data/spec/rr/space/space_register_spec.rb +32 -0
- data/spec/rr/space/space_reset_spec.rb +131 -0
- data/spec/rr/space/space_spec.rb +32 -0
- data/spec/rr/space/space_verify_spec.rb +181 -0
- data/{examples → spec}/rr/test_unit/test_helper.rb +1 -1
- data/{examples → spec}/rr/test_unit/test_unit_backtrace_test.rb +0 -0
- data/{examples → spec}/rr/test_unit/test_unit_integration_test.rb +4 -4
- data/spec/rr/times_called_matchers/any_times_matcher_spec.rb +47 -0
- data/spec/rr/times_called_matchers/at_least_matcher_spec.rb +55 -0
- data/spec/rr/times_called_matchers/at_most_matcher_spec.rb +70 -0
- data/spec/rr/times_called_matchers/integer_matcher_spec.rb +70 -0
- data/spec/rr/times_called_matchers/proc_matcher_spec.rb +55 -0
- data/spec/rr/times_called_matchers/range_matcher_spec.rb +76 -0
- data/spec/rr/times_called_matchers/times_called_matcher_spec.rb +118 -0
- data/spec/rspec_spec_suite.rb +16 -0
- data/spec/spec_helper.rb +9 -0
- data/{examples/example_suite.rb → spec/spec_suite.rb} +3 -3
- data/{examples/test_unit_example_suite.rb → spec/test_unit_spec_suite.rb} +0 -0
- metadata +93 -93
- data/examples/core_example_suite.rb +0 -31
- data/examples/example_helper.rb +0 -9
- data/examples/rr/double/double_bind_example.rb +0 -70
- data/examples/rr/double/double_dispatching_example.rb +0 -236
- data/examples/rr/double/double_example.rb +0 -66
- data/examples/rr/double/double_has_original_method_example.rb +0 -56
- data/examples/rr/double/double_register_scenario_example.rb +0 -24
- data/examples/rr/double/double_reset_example.rb +0 -89
- data/examples/rr/double/double_verify_example.rb +0 -23
- data/examples/rr/errors/rr_error_example.rb +0 -65
- data/examples/rr/expectations/any_argument_expectation_example.rb +0 -52
- data/examples/rr/expectations/anything_argument_equality_expectation_example.rb +0 -38
- data/examples/rr/expectations/argument_equality_expectation_example.rb +0 -62
- data/examples/rr/expectations/boolean_argument_equality_expectation_example.rb +0 -48
- data/examples/rr/expectations/duck_type_argument_equality_expectation_example.rb +0 -67
- data/examples/rr/expectations/is_a_argument_equality_expectation_example.rb +0 -50
- data/examples/rr/expectations/numeric_argument_equality_expectation_example.rb +0 -46
- data/examples/rr/expectations/range_argument_equality_expectation_example.rb +0 -59
- data/examples/rr/expectations/regexp_argument_equality_expectation_example.rb +0 -67
- data/examples/rr/expectations/times_called_expectation/times_called_expectation_any_times_example.rb +0 -50
- data/examples/rr/expectations/times_called_expectation/times_called_expectation_at_least_example.rb +0 -73
- data/examples/rr/expectations/times_called_expectation/times_called_expectation_at_most_example.rb +0 -77
- data/examples/rr/expectations/times_called_expectation/times_called_expectation_example.rb +0 -42
- data/examples/rr/expectations/times_called_expectation/times_called_expectation_helper.rb +0 -20
- data/examples/rr/expectations/times_called_expectation/times_called_expectation_integer_example.rb +0 -111
- data/examples/rr/expectations/times_called_expectation/times_called_expectation_proc_example.rb +0 -88
- data/examples/rr/expectations/times_called_expectation/times_called_expectation_range_example.rb +0 -90
- data/examples/rr/extensions/instance_methods_argument_matcher_example.rb +0 -65
- data/examples/rr/extensions/instance_methods_creator_example.rb +0 -363
- data/examples/rr/extensions/instance_methods_example_helper.rb +0 -11
- data/examples/rr/extensions/instance_methods_space_example.rb +0 -122
- data/examples/rr/rspec/rspec_adapter_example.rb +0 -63
- data/examples/rr/rspec/rspec_backtrace_tweaking_example.rb +0 -36
- data/examples/rr/rspec/rspec_usage_example.rb +0 -65
- data/examples/rr/scenario_creator_example.rb +0 -459
- data/examples/rr/scenario_example.rb +0 -701
- data/examples/rr/scenario_method_proxy_example.rb +0 -71
- data/examples/rr/space/hash_with_object_id_key_example.rb +0 -86
- data/examples/rr/space/space_create_example.rb +0 -278
- data/examples/rr/space/space_example.rb +0 -29
- data/examples/rr/space/space_helper.rb +0 -7
- data/examples/rr/space/space_register_example.rb +0 -32
- data/examples/rr/space/space_reset_example.rb +0 -120
- data/examples/rr/space/space_verify_example.rb +0 -169
- data/examples/rr/times_called_matchers/any_times_matcher_example.rb +0 -63
- data/examples/rr/times_called_matchers/at_least_matcher_example.rb +0 -70
- data/examples/rr/times_called_matchers/at_most_matcher_example.rb +0 -85
- data/examples/rr/times_called_matchers/integer_matcher_example.rb +0 -91
- data/examples/rr/times_called_matchers/proc_matcher_example.rb +0 -77
- data/examples/rr/times_called_matchers/range_matcher_example.rb +0 -97
- data/examples/rr/times_called_matchers/times_called_matcher_example.rb +0 -53
- data/examples/rspec_example_suite.rb +0 -25
- data/lib/rr/extensions/instance_methods.rb +0 -228
- data/lib/rr/scenario.rb +0 -337
@@ -0,0 +1,56 @@
|
|
1
|
+
require "spec/spec_helper"
|
2
|
+
|
3
|
+
module RR
|
4
|
+
describe DoubleInsertion, "#object_has_original_method?" do
|
5
|
+
before do
|
6
|
+
@space = Space.new
|
7
|
+
@object = Object.new
|
8
|
+
@method_name = :to_s
|
9
|
+
@double_insertion = DoubleInsertion.new(@space, @object, @method_name)
|
10
|
+
class << @double_insertion
|
11
|
+
public :original_method_name
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
it "returns true when method is still in object" do
|
16
|
+
@double_insertion.bind
|
17
|
+
@double_insertion.object_has_original_method?.should be_true
|
18
|
+
end
|
19
|
+
|
20
|
+
it "returns true when respond_to is true and methods include method" do
|
21
|
+
@double_insertion.bind
|
22
|
+
def @object.methods
|
23
|
+
[:__rr_original_to_s]
|
24
|
+
end
|
25
|
+
def @object.respond_to?(value)
|
26
|
+
true
|
27
|
+
end
|
28
|
+
|
29
|
+
@double_insertion.object_has_original_method?.should be_true
|
30
|
+
end
|
31
|
+
|
32
|
+
it "returns true when respond_to is true and methods do not include method" do
|
33
|
+
@double_insertion.bind
|
34
|
+
def @object.methods
|
35
|
+
[]
|
36
|
+
end
|
37
|
+
def @object.respond_to?(value)
|
38
|
+
true
|
39
|
+
end
|
40
|
+
|
41
|
+
@double_insertion.object_has_original_method?.should be_true
|
42
|
+
end
|
43
|
+
|
44
|
+
it "returns false when respond_to is false and methods do not include method" do
|
45
|
+
@double_insertion.bind
|
46
|
+
def @object.methods
|
47
|
+
[]
|
48
|
+
end
|
49
|
+
def @object.respond_to?(value)
|
50
|
+
false
|
51
|
+
end
|
52
|
+
|
53
|
+
@double_insertion.object_has_original_method?.should be_false
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
require "spec/spec_helper"
|
2
|
+
|
3
|
+
module RR
|
4
|
+
describe DoubleInsertion, "#register_scenario" do
|
5
|
+
before do
|
6
|
+
@space = Space.new
|
7
|
+
@object = Object.new
|
8
|
+
@method_name = :foobar
|
9
|
+
@object.methods.should_not include(@method_name.to_s)
|
10
|
+
@double_insertion = DoubleInsertion.new(@space, @object, @method_name)
|
11
|
+
def @double_insertion.scenarios
|
12
|
+
@scenarios
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
it "adds the scenario to the scenarios list" do
|
17
|
+
scenario = Double.new(@space, @double_insertion, @space.scenario_definition)
|
18
|
+
|
19
|
+
@double_insertion.scenarios.should_not include(scenario)
|
20
|
+
@double_insertion.register_scenario scenario
|
21
|
+
@double_insertion.scenarios.should include(scenario)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,89 @@
|
|
1
|
+
require "spec/spec_helper"
|
2
|
+
|
3
|
+
module RR
|
4
|
+
describe DoubleInsertion, "#reset", :shared => true do
|
5
|
+
it "cleans up by removing the __rr__method" do
|
6
|
+
@double_insertion.bind
|
7
|
+
@object.methods.should include("__rr__foobar")
|
8
|
+
|
9
|
+
@double_insertion.reset
|
10
|
+
@object.methods.should_not include("__rr__foobar")
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
describe DoubleInsertion, "#reset when method does not exist" do
|
15
|
+
it_should_behave_like "RR::DoubleInsertion#reset"
|
16
|
+
|
17
|
+
before do
|
18
|
+
@space = Space.new
|
19
|
+
@object = Object.new
|
20
|
+
@method_name = :foobar
|
21
|
+
@object.methods.should_not include(@method_name.to_s)
|
22
|
+
@double_insertion = DoubleInsertion.new(@space, @object, @method_name)
|
23
|
+
end
|
24
|
+
|
25
|
+
it "removes the method" do
|
26
|
+
@double_insertion.bind
|
27
|
+
@object.methods.should include(@method_name.to_s)
|
28
|
+
|
29
|
+
@double_insertion.reset
|
30
|
+
@object.methods.should_not include(@method_name.to_s)
|
31
|
+
proc {@object.foobar}.should raise_error(NoMethodError)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
describe DoubleInsertion, "#reset when method exists" do
|
36
|
+
it_should_behave_like "RR::DoubleInsertion#reset"
|
37
|
+
|
38
|
+
before do
|
39
|
+
@space = Space.new
|
40
|
+
@object = Object.new
|
41
|
+
@method_name = :foobar
|
42
|
+
def @object.foobar
|
43
|
+
:original_foobar
|
44
|
+
end
|
45
|
+
@object.methods.should include(@method_name.to_s)
|
46
|
+
@original_method = @object.method(@method_name)
|
47
|
+
@double_insertion = DoubleInsertion.new(@space, @object, @method_name)
|
48
|
+
|
49
|
+
@double_insertion.bind
|
50
|
+
@object.methods.should include(@method_name.to_s)
|
51
|
+
end
|
52
|
+
|
53
|
+
it "rebind original method" do
|
54
|
+
@double_insertion.reset
|
55
|
+
@object.methods.should include(@method_name.to_s)
|
56
|
+
@object.foobar.should == :original_foobar
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
describe DoubleInsertion, "#reset when method with block exists" do
|
61
|
+
it_should_behave_like "RR::DoubleInsertion#reset"
|
62
|
+
|
63
|
+
before do
|
64
|
+
@space = Space.new
|
65
|
+
@object = Object.new
|
66
|
+
@method_name = :foobar
|
67
|
+
def @object.foobar
|
68
|
+
yield(:original_argument)
|
69
|
+
end
|
70
|
+
@object.methods.should include(@method_name.to_s)
|
71
|
+
@original_method = @object.method(@method_name)
|
72
|
+
@double_insertion = DoubleInsertion.new(@space, @object, @method_name)
|
73
|
+
|
74
|
+
@double_insertion.bind
|
75
|
+
@object.methods.should include(@method_name.to_s)
|
76
|
+
end
|
77
|
+
|
78
|
+
it "rebinds original method with block" do
|
79
|
+
@double_insertion.reset
|
80
|
+
@object.methods.should include(@method_name.to_s)
|
81
|
+
|
82
|
+
original_argument = nil
|
83
|
+
@object.foobar do |arg|
|
84
|
+
original_argument = arg
|
85
|
+
end
|
86
|
+
original_argument.should == :original_argument
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
@@ -0,0 +1,66 @@
|
|
1
|
+
require "spec/spec_helper"
|
2
|
+
|
3
|
+
module RR
|
4
|
+
describe DoubleInsertion, :shared => true do
|
5
|
+
it "sets up object and method_name" do
|
6
|
+
@double_insertion.object.should === @object
|
7
|
+
@double_insertion.method_name.should == @method_name.to_sym
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
describe DoubleInsertion, "#initialize where method_name is a symbol" do
|
12
|
+
it_should_behave_like "RR::DoubleInsertion"
|
13
|
+
|
14
|
+
before do
|
15
|
+
@space = Space.new
|
16
|
+
@object = Object.new
|
17
|
+
@method_name = :foobar
|
18
|
+
@object.methods.should_not include(@method_name.to_s)
|
19
|
+
@double_insertion = DoubleInsertion.new(@space, @object, @method_name)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
describe DoubleInsertion, "#initialize where method_name is a string" do
|
24
|
+
it_should_behave_like "RR::DoubleInsertion"
|
25
|
+
|
26
|
+
before do
|
27
|
+
@space = Space.new
|
28
|
+
@object = Object.new
|
29
|
+
@method_name = 'foobar'
|
30
|
+
@object.methods.should_not include(@method_name)
|
31
|
+
@double_insertion = DoubleInsertion.new(@space, @object, @method_name)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
describe DoubleInsertion, "#initialize where method does not exist on object" do
|
36
|
+
it_should_behave_like "RR::DoubleInsertion"
|
37
|
+
|
38
|
+
before do
|
39
|
+
@space = Space.new
|
40
|
+
@object = Object.new
|
41
|
+
@method_name = :foobar
|
42
|
+
@object.methods.should_not include(@method_name.to_s)
|
43
|
+
@double_insertion = DoubleInsertion.new(@space, @object, @method_name)
|
44
|
+
end
|
45
|
+
|
46
|
+
it "object does not have original method" do
|
47
|
+
@double_insertion.object_has_original_method?.should be_false
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
describe DoubleInsertion, "#initialize where method exists on object" do
|
52
|
+
it_should_behave_like "RR::DoubleInsertion"
|
53
|
+
|
54
|
+
before do
|
55
|
+
@space = Space.new
|
56
|
+
@object = Object.new
|
57
|
+
@method_name = :to_s
|
58
|
+
@object.methods.should include(@method_name.to_s)
|
59
|
+
@double_insertion = DoubleInsertion.new(@space, @object, @method_name)
|
60
|
+
end
|
61
|
+
|
62
|
+
it "has a original_method" do
|
63
|
+
@double_insertion.object_has_original_method?.should be_true
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require "spec/spec_helper"
|
2
|
+
|
3
|
+
module RR
|
4
|
+
describe DoubleInsertion, "#verify" do
|
5
|
+
before do
|
6
|
+
@space = Space.new
|
7
|
+
@object = Object.new
|
8
|
+
@method_name = :foobar
|
9
|
+
@object.methods.should_not include(@method_name.to_s)
|
10
|
+
@double_insertion = @space.double_insertion(@object, @method_name)
|
11
|
+
end
|
12
|
+
|
13
|
+
it "verifies each scenario was met" do
|
14
|
+
scenario = Double.new(@space, @double_insertion, @space.scenario_definition)
|
15
|
+
@double_insertion.register_scenario scenario
|
16
|
+
|
17
|
+
scenario.with(1).once.returns {nil}
|
18
|
+
proc {@double_insertion.verify}.should raise_error(Errors::TimesCalledError)
|
19
|
+
@object.foobar(1)
|
20
|
+
proc {@double_insertion.verify}.should_not raise_error
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,454 @@
|
|
1
|
+
require "spec/spec_helper"
|
2
|
+
|
3
|
+
module RR
|
4
|
+
describe DoubleCreator, " strategy definition", :shared => true do
|
5
|
+
it "returns self when passing no args" do
|
6
|
+
creator.__send__(method_name).should === creator
|
7
|
+
end
|
8
|
+
|
9
|
+
it "returns a DoubleMethodProxy when passed a subject" do
|
10
|
+
scenario = creator.__send__(method_name, subject).foobar
|
11
|
+
scenario.should be_instance_of(DoubleDefinition)
|
12
|
+
end
|
13
|
+
|
14
|
+
it "returns a DoubleMethodProxy when passed Kernel" do
|
15
|
+
scenario = creator.__send__(method_name, Kernel).foobar
|
16
|
+
scenario.should be_instance_of(DoubleDefinition)
|
17
|
+
end
|
18
|
+
|
19
|
+
it "raises error if passed a method name and a block" do
|
20
|
+
proc do
|
21
|
+
creator.__send__(method_name, subject, :foobar) {}
|
22
|
+
end.should raise_error(ArgumentError, "Cannot pass in a method name and a block")
|
23
|
+
end
|
24
|
+
|
25
|
+
it "raises error when using mock strategy" do
|
26
|
+
creator.mock
|
27
|
+
proc do
|
28
|
+
creator.__send__(method_name)
|
29
|
+
end.should raise_error(
|
30
|
+
Errors::DoubleDefinitionError,
|
31
|
+
"This Double already has a mock strategy"
|
32
|
+
)
|
33
|
+
end
|
34
|
+
|
35
|
+
it "raises error when using stub strategy" do
|
36
|
+
creator.stub
|
37
|
+
proc do
|
38
|
+
creator.__send__(method_name)
|
39
|
+
end.should raise_error(
|
40
|
+
Errors::DoubleDefinitionError,
|
41
|
+
"This Double already has a stub strategy"
|
42
|
+
)
|
43
|
+
end
|
44
|
+
|
45
|
+
it "raises error when using dont_allow strategy" do
|
46
|
+
creator.dont_allow
|
47
|
+
proc do
|
48
|
+
creator.__send__(method_name)
|
49
|
+
end.should raise_error(
|
50
|
+
Errors::DoubleDefinitionError,
|
51
|
+
"This Double already has a dont_allow strategy"
|
52
|
+
)
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
describe DoubleCreator do
|
57
|
+
attr_reader :creator, :subject, :space, :method_name
|
58
|
+
before(:each) do
|
59
|
+
@space = Space.new
|
60
|
+
@subject = Object.new
|
61
|
+
@creator = DoubleCreator.new(space)
|
62
|
+
end
|
63
|
+
|
64
|
+
describe "#mock" do
|
65
|
+
it_should_behave_like "RR::DoubleCreator strategy definition"
|
66
|
+
|
67
|
+
before do
|
68
|
+
@method_name = :mock
|
69
|
+
end
|
70
|
+
|
71
|
+
it "sets up the RR mock call chain" do
|
72
|
+
creates_mock_call_chain(creator.mock(subject))
|
73
|
+
end
|
74
|
+
|
75
|
+
it "creates a mock Double for method when passed a second argument with rr_mock" do
|
76
|
+
creates_scenario_with_method_name(
|
77
|
+
creator.mock(subject, :foobar)
|
78
|
+
)
|
79
|
+
end
|
80
|
+
|
81
|
+
it "sets up the DoubleDefinition to be in returns block_callback_strategy" do
|
82
|
+
scenario = creator.mock(subject, :foobar)
|
83
|
+
scenario.block_callback_strategy.should == :returns
|
84
|
+
end
|
85
|
+
|
86
|
+
def creates_scenario_with_method_name(scenario)
|
87
|
+
scenario.with(1, 2) {:baz}
|
88
|
+
scenario.times_matcher.should == TimesCalledMatchers::IntegerMatcher.new(1)
|
89
|
+
scenario.argument_expectation.class.should == RR::Expectations::ArgumentEqualityExpectation
|
90
|
+
scenario.argument_expectation.expected_arguments.should == [1, 2]
|
91
|
+
|
92
|
+
subject.foobar(1, 2).should == :baz
|
93
|
+
end
|
94
|
+
|
95
|
+
def creates_mock_call_chain(creator)
|
96
|
+
scenario = creator.foobar(1, 2) {:baz}
|
97
|
+
scenario.times_matcher.should == TimesCalledMatchers::IntegerMatcher.new(1)
|
98
|
+
scenario.argument_expectation.class.should == RR::Expectations::ArgumentEqualityExpectation
|
99
|
+
scenario.argument_expectation.expected_arguments.should == [1, 2]
|
100
|
+
|
101
|
+
subject.foobar(1, 2).should == :baz
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
describe "#stub" do
|
106
|
+
it_should_behave_like "RR::DoubleCreator strategy definition"
|
107
|
+
|
108
|
+
before do
|
109
|
+
@method_name = :stub
|
110
|
+
end
|
111
|
+
|
112
|
+
it "sets up the RR stub call chain" do
|
113
|
+
creates_stub_call_chain(creator.stub(subject))
|
114
|
+
end
|
115
|
+
|
116
|
+
it "creates a stub Double for method when passed a second argument" do
|
117
|
+
creates_scenario_with_method_name(creator.stub(subject, :foobar))
|
118
|
+
end
|
119
|
+
|
120
|
+
it "sets up the DoubleDefinition to be in returns block_callback_strategy" do
|
121
|
+
scenario = creator.stub(subject, :foobar)
|
122
|
+
scenario.block_callback_strategy.should == :returns
|
123
|
+
end
|
124
|
+
|
125
|
+
def creates_scenario_with_method_name(scenario)
|
126
|
+
scenario.with(1, 2) {:baz}
|
127
|
+
scenario.times_matcher.should == TimesCalledMatchers::AnyTimesMatcher.new
|
128
|
+
scenario.argument_expectation.class.should == RR::Expectations::ArgumentEqualityExpectation
|
129
|
+
subject.foobar(1, 2).should == :baz
|
130
|
+
end
|
131
|
+
|
132
|
+
def creates_stub_call_chain(creator)
|
133
|
+
scenario = creator.foobar(1, 2) {:baz}
|
134
|
+
scenario.times_matcher.should == TimesCalledMatchers::AnyTimesMatcher.new
|
135
|
+
scenario.argument_expectation.class.should == RR::Expectations::ArgumentEqualityExpectation
|
136
|
+
subject.foobar(1, 2).should == :baz
|
137
|
+
end
|
138
|
+
end
|
139
|
+
|
140
|
+
describe "#dont_allow" do
|
141
|
+
it_should_behave_like "RR::DoubleCreator strategy definition"
|
142
|
+
|
143
|
+
before do
|
144
|
+
@method_name = :dont_allow
|
145
|
+
end
|
146
|
+
|
147
|
+
it "raises error when proxied" do
|
148
|
+
creator.proxy
|
149
|
+
proc do
|
150
|
+
creator.dont_allow
|
151
|
+
end.should raise_error(
|
152
|
+
Errors::DoubleDefinitionError,
|
153
|
+
"Doubles cannot be proxied when using dont_allow strategy"
|
154
|
+
)
|
155
|
+
end
|
156
|
+
|
157
|
+
it "sets up the RR dont_allow call chain" do
|
158
|
+
creates_dont_allow_call_chain(creator.dont_allow(subject))
|
159
|
+
end
|
160
|
+
|
161
|
+
it "sets up the RR dont_allow call chain" do
|
162
|
+
creates_dont_allow_call_chain(creator.dont_call(subject))
|
163
|
+
end
|
164
|
+
|
165
|
+
it "sets up the RR dont_allow call chain" do
|
166
|
+
creates_dont_allow_call_chain(creator.do_not_allow(subject))
|
167
|
+
end
|
168
|
+
|
169
|
+
it "sets up the RR dont_allow call chain" do
|
170
|
+
creates_dont_allow_call_chain(creator.dont_allow(subject))
|
171
|
+
end
|
172
|
+
|
173
|
+
it "creates a mock Double for method when passed a second argument" do
|
174
|
+
creates_scenario_with_method_name(creator.dont_allow(subject, :foobar))
|
175
|
+
end
|
176
|
+
|
177
|
+
it "creates a mock Double for method when passed a second argument" do
|
178
|
+
creates_scenario_with_method_name(creator.dont_call(subject, :foobar))
|
179
|
+
end
|
180
|
+
|
181
|
+
it "creates a mock Double for method when passed a second argument" do
|
182
|
+
creates_scenario_with_method_name(creator.do_not_allow(subject, :foobar))
|
183
|
+
end
|
184
|
+
|
185
|
+
it "creates a mock Double for method when passed a second argument" do
|
186
|
+
creates_scenario_with_method_name(creator.dont_allow(subject, :foobar))
|
187
|
+
end
|
188
|
+
|
189
|
+
def creates_scenario_with_method_name(scenario)
|
190
|
+
scenario.with(1, 2)
|
191
|
+
scenario.times_matcher.should == TimesCalledMatchers::IntegerMatcher.new(0)
|
192
|
+
scenario.argument_expectation.class.should == RR::Expectations::ArgumentEqualityExpectation
|
193
|
+
scenario.argument_expectation.expected_arguments.should == [1, 2]
|
194
|
+
|
195
|
+
proc do
|
196
|
+
subject.foobar(1, 2)
|
197
|
+
end.should raise_error(Errors::TimesCalledError)
|
198
|
+
reset
|
199
|
+
nil
|
200
|
+
end
|
201
|
+
|
202
|
+
def creates_dont_allow_call_chain(creator)
|
203
|
+
scenario = creator.foobar(1, 2)
|
204
|
+
scenario.times_matcher.should == TimesCalledMatchers::IntegerMatcher.new(0)
|
205
|
+
scenario.argument_expectation.class.should == RR::Expectations::ArgumentEqualityExpectation
|
206
|
+
scenario.argument_expectation.expected_arguments.should == [1, 2]
|
207
|
+
|
208
|
+
proc do
|
209
|
+
subject.foobar(1, 2)
|
210
|
+
end.should raise_error(Errors::TimesCalledError)
|
211
|
+
reset
|
212
|
+
nil
|
213
|
+
end
|
214
|
+
end
|
215
|
+
|
216
|
+
describe "(#proxy or #proxy) and #stub" do
|
217
|
+
before do
|
218
|
+
class << subject
|
219
|
+
def foobar(*args)
|
220
|
+
:original_foobar
|
221
|
+
end
|
222
|
+
end
|
223
|
+
end
|
224
|
+
|
225
|
+
it "raises error when using dont_allow strategy" do
|
226
|
+
creator.dont_allow
|
227
|
+
proc do
|
228
|
+
creator.proxy
|
229
|
+
end.should raise_error(
|
230
|
+
Errors::DoubleDefinitionError,
|
231
|
+
"Doubles cannot be proxied when using dont_allow strategy"
|
232
|
+
)
|
233
|
+
end
|
234
|
+
|
235
|
+
it "sets up the RR proxy call chain" do
|
236
|
+
scenario = creator.stub.proxy(subject).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
|
+
|
242
|
+
it "sets up the RR proxy call chain" do
|
243
|
+
scenario = creator.stub.proxy(subject).foobar(1, 2) {:baz}
|
244
|
+
scenario.times_matcher.should == TimesCalledMatchers::AnyTimesMatcher.new
|
245
|
+
scenario.argument_expectation.class.should == RR::Expectations::ArgumentEqualityExpectation
|
246
|
+
subject.foobar(1, 2).should == :baz
|
247
|
+
end
|
248
|
+
|
249
|
+
it "creates a proxy Double for method when passed a second argument" do
|
250
|
+
scenario = creator.stub.proxy(subject, :foobar)
|
251
|
+
scenario.with(1, 2) {:baz}
|
252
|
+
scenario.times_matcher.should == TimesCalledMatchers::AnyTimesMatcher.new
|
253
|
+
scenario.argument_expectation.class.should == RR::Expectations::ArgumentEqualityExpectation
|
254
|
+
subject.foobar(1, 2).should == :baz
|
255
|
+
end
|
256
|
+
|
257
|
+
it "creates a proxy Double for method when passed a second argument" do
|
258
|
+
scenario = creator.stub.proxy(subject, :foobar)
|
259
|
+
scenario.with(1, 2) {:baz}
|
260
|
+
scenario.times_matcher.should == TimesCalledMatchers::AnyTimesMatcher.new
|
261
|
+
scenario.argument_expectation.class.should == RR::Expectations::ArgumentEqualityExpectation
|
262
|
+
subject.foobar(1, 2).should == :baz
|
263
|
+
end
|
264
|
+
|
265
|
+
it "sets up the DoubleDefinition to be in after_call block_callback_strategy" do
|
266
|
+
def subject.foobar
|
267
|
+
:original_implementation_value
|
268
|
+
end
|
269
|
+
|
270
|
+
args = nil
|
271
|
+
scenario = creator.stub.proxy(subject, :foobar).with() do |*args|
|
272
|
+
args = args
|
273
|
+
end
|
274
|
+
subject.foobar
|
275
|
+
args.should == [:original_implementation_value]
|
276
|
+
scenario.block_callback_strategy.should == :after_call
|
277
|
+
end
|
278
|
+
end
|
279
|
+
|
280
|
+
describe "#instance_of" do
|
281
|
+
it "raises an error when not passed a class" do
|
282
|
+
proc do
|
283
|
+
creator.instance_of(Object.new)
|
284
|
+
end.should raise_error(ArgumentError, "instance_of only accepts class objects")
|
285
|
+
end
|
286
|
+
|
287
|
+
it "sets up the RR proxy call chain" do
|
288
|
+
klass = Class.new
|
289
|
+
scenario = creator.stub.instance_of(klass).foobar(1, 2) {:baz}
|
290
|
+
scenario.times_matcher.should == TimesCalledMatchers::AnyTimesMatcher.new
|
291
|
+
scenario.argument_expectation.class.should == RR::Expectations::ArgumentEqualityExpectation
|
292
|
+
klass.new.foobar(1, 2).should == :baz
|
293
|
+
end
|
294
|
+
|
295
|
+
it "creates a proxy Double for method when passed a second argument" do
|
296
|
+
klass = Class.new
|
297
|
+
scenario = creator.stub.instance_of(klass, :foobar)
|
298
|
+
scenario.with(1, 2) {:baz}
|
299
|
+
scenario.times_matcher.should == TimesCalledMatchers::AnyTimesMatcher.new
|
300
|
+
scenario.argument_expectation.class.should == RR::Expectations::ArgumentEqualityExpectation
|
301
|
+
klass.new.foobar(1, 2).should == :baz
|
302
|
+
end
|
303
|
+
end
|
304
|
+
|
305
|
+
describe "#create! using no strategy" do
|
306
|
+
it "raises error" do
|
307
|
+
proc do
|
308
|
+
creator.create!(subject, :foobar, 1, 2)
|
309
|
+
end.should raise_error(
|
310
|
+
Errors::DoubleDefinitionError,
|
311
|
+
"This Double has no strategy"
|
312
|
+
)
|
313
|
+
end
|
314
|
+
end
|
315
|
+
|
316
|
+
describe "#create! using mock strategy" do
|
317
|
+
before do
|
318
|
+
creator.mock
|
319
|
+
end
|
320
|
+
|
321
|
+
it "sets expectations on the subject" do
|
322
|
+
creator.create!(subject, :foobar, 1, 2) {:baz}.twice
|
323
|
+
|
324
|
+
subject.foobar(1, 2).should == :baz
|
325
|
+
subject.foobar(1, 2).should == :baz
|
326
|
+
proc {subject.foobar(1, 2)}.should raise_error(Errors::TimesCalledError)
|
327
|
+
end
|
328
|
+
end
|
329
|
+
|
330
|
+
describe "#create! using stub strategy" do
|
331
|
+
before do
|
332
|
+
creator.stub
|
333
|
+
end
|
334
|
+
|
335
|
+
it "stubs the subject without any args" do
|
336
|
+
creator.create!(subject, :foobar) {:baz}
|
337
|
+
subject.foobar.should == :baz
|
338
|
+
end
|
339
|
+
|
340
|
+
it "stubs the subject mapping passed in args with the output" do
|
341
|
+
creator.create!(subject, :foobar, 1, 2) {:one_two}
|
342
|
+
creator.create!(subject, :foobar, 1) {:one}
|
343
|
+
creator.create!(subject, :foobar) {:nothing}
|
344
|
+
subject.foobar.should == :nothing
|
345
|
+
subject.foobar(1).should == :one
|
346
|
+
subject.foobar(1, 2).should == :one_two
|
347
|
+
end
|
348
|
+
end
|
349
|
+
|
350
|
+
describe "#create! using dont_allow strategy" do
|
351
|
+
before do
|
352
|
+
creator.dont_allow
|
353
|
+
end
|
354
|
+
|
355
|
+
it "sets expectation for method to never be called with any arguments when on arguments passed in" do
|
356
|
+
creator.create!(subject, :foobar)
|
357
|
+
proc {subject.foobar}.should raise_error(Errors::TimesCalledError)
|
358
|
+
proc {subject.foobar(1, 2)}.should raise_error(Errors::TimesCalledError)
|
359
|
+
end
|
360
|
+
|
361
|
+
it "sets expectation for method to never be called with passed in arguments" do
|
362
|
+
creator.create!(subject, :foobar, 1, 2)
|
363
|
+
proc {subject.foobar}.should raise_error(Errors::DoubleNotFoundError)
|
364
|
+
proc {subject.foobar(1, 2)}.should raise_error(Errors::TimesCalledError)
|
365
|
+
end
|
366
|
+
|
367
|
+
it "sets expectation for method to never be called with no arguments when with_no_args is set" do
|
368
|
+
creator.create!(subject, :foobar).with_no_args
|
369
|
+
proc {subject.foobar}.should raise_error(Errors::TimesCalledError)
|
370
|
+
proc {subject.foobar(1, 2)}.should raise_error(Errors::DoubleNotFoundError)
|
371
|
+
end
|
372
|
+
end
|
373
|
+
|
374
|
+
describe "#create! using mock strategy with proxy" do
|
375
|
+
before do
|
376
|
+
creator.mock
|
377
|
+
creator.proxy
|
378
|
+
end
|
379
|
+
|
380
|
+
it "sets expectations on the subject while calling the original method" do
|
381
|
+
def subject.foobar(*args)
|
382
|
+
; :baz;
|
383
|
+
end
|
384
|
+
creator.create!(subject, :foobar, 1, 2).twice
|
385
|
+
subject.foobar(1, 2).should == :baz
|
386
|
+
subject.foobar(1, 2).should == :baz
|
387
|
+
proc {subject.foobar(1, 2)}.should raise_error(Errors::TimesCalledError)
|
388
|
+
end
|
389
|
+
|
390
|
+
it "sets after_call on the scenario when passed a block" do
|
391
|
+
real_value = Object.new
|
392
|
+
(
|
393
|
+
class << subject;
|
394
|
+
self;
|
395
|
+
end).class_eval do
|
396
|
+
define_method(:foobar) {real_value}
|
397
|
+
end
|
398
|
+
creator.create!(subject, :foobar, 1, 2) do |value|
|
399
|
+
mock(value).a_method {99}
|
400
|
+
value
|
401
|
+
end
|
402
|
+
|
403
|
+
return_value = subject.foobar(1, 2)
|
404
|
+
return_value.should === return_value
|
405
|
+
return_value.a_method.should == 99
|
406
|
+
end
|
407
|
+
end
|
408
|
+
|
409
|
+
describe "#create! using stub strategy with proxy" do
|
410
|
+
before do
|
411
|
+
creator.stub
|
412
|
+
creator.proxy
|
413
|
+
end
|
414
|
+
|
415
|
+
it "sets up a scenario with passed in arguments" do
|
416
|
+
def subject.foobar(*args)
|
417
|
+
; :baz;
|
418
|
+
end
|
419
|
+
creator.create!(subject, :foobar, 1, 2)
|
420
|
+
proc do
|
421
|
+
subject.foobar
|
422
|
+
end.should raise_error(Errors::DoubleNotFoundError)
|
423
|
+
end
|
424
|
+
|
425
|
+
it "sets expectations on the subject while calling the original method" do
|
426
|
+
def subject.foobar(*args)
|
427
|
+
; :baz;
|
428
|
+
end
|
429
|
+
creator.create!(subject, :foobar, 1, 2) {:new_value}
|
430
|
+
10.times do
|
431
|
+
subject.foobar(1, 2).should == :new_value
|
432
|
+
end
|
433
|
+
end
|
434
|
+
|
435
|
+
it "sets after_call on the scenario when passed a block" do
|
436
|
+
real_value = Object.new
|
437
|
+
(
|
438
|
+
class << subject;
|
439
|
+
self;
|
440
|
+
end).class_eval do
|
441
|
+
define_method(:foobar) {real_value}
|
442
|
+
end
|
443
|
+
creator.create!(subject, :foobar, 1, 2) do |value|
|
444
|
+
mock(value).a_method {99}
|
445
|
+
value
|
446
|
+
end
|
447
|
+
|
448
|
+
return_value = subject.foobar(1, 2)
|
449
|
+
return_value.should === return_value
|
450
|
+
return_value.a_method.should == 99
|
451
|
+
end
|
452
|
+
end
|
453
|
+
end
|
454
|
+
end
|