rr 0.4.10 → 0.6.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGES +14 -0
- data/README.rdoc +67 -13
- data/Rakefile +1 -1
- data/lib/rr.rb +29 -9
- data/lib/rr/adapters/rr_methods.rb +38 -158
- data/lib/rr/double.rb +46 -41
- data/lib/rr/double_definitions/child_double_definition_creator.rb +23 -0
- data/lib/rr/double_definitions/double_definition.rb +212 -0
- data/lib/rr/double_definitions/double_definition_creator.rb +153 -0
- data/lib/rr/double_definitions/double_definition_creator_proxy.rb +25 -0
- data/lib/rr/double_definitions/strategies/implementation/implementation_strategy.rb +15 -0
- data/lib/rr/double_definitions/strategies/implementation/proxy.rb +62 -0
- data/lib/rr/double_definitions/strategies/implementation/reimplementation.rb +14 -0
- data/lib/rr/double_definitions/strategies/scope/instance.rb +15 -0
- data/lib/rr/double_definitions/strategies/scope/instance_of_class.rb +43 -0
- data/lib/rr/double_definitions/strategies/scope/scope_strategy.rb +15 -0
- data/lib/rr/double_definitions/strategies/strategy.rb +70 -0
- data/lib/rr/double_definitions/strategies/verification/dont_allow.rb +34 -0
- data/lib/rr/double_definitions/strategies/verification/mock.rb +44 -0
- data/lib/rr/double_definitions/strategies/verification/stub.rb +45 -0
- data/lib/rr/double_definitions/strategies/verification/verification_strategy.rb +15 -0
- data/lib/rr/double_injection.rb +21 -15
- data/lib/rr/expectations/argument_equality_expectation.rb +2 -1
- data/lib/rr/space.rb +23 -22
- data/lib/rr/wildcard_matchers/hash_including.rb +29 -0
- data/lib/rr/wildcard_matchers/satisfy.rb +26 -0
- data/spec/high_level_spec.rb +111 -64
- data/spec/rr/adapters/rr_methods_argument_matcher_spec.rb +1 -1
- data/spec/rr/adapters/rr_methods_creator_spec.rb +99 -315
- data/spec/rr/adapters/rr_methods_space_spec.rb +90 -109
- data/spec/rr/adapters/rr_methods_spec_helper.rb +1 -1
- data/spec/rr/adapters/rr_methods_times_matcher_spec.rb +1 -1
- data/spec/rr/double_definitions/child_double_definition_creator_spec.rb +103 -0
- data/spec/rr/double_definitions/double_definition_creator_proxy_spec.rb +83 -0
- data/spec/rr/double_definitions/double_definition_creator_spec.rb +495 -0
- data/spec/rr/double_definitions/double_definition_spec.rb +1116 -0
- data/spec/rr/double_injection/double_injection_bind_spec.rb +111 -0
- data/spec/rr/double_injection/double_injection_dispatching_spec.rb +245 -0
- data/spec/rr/{double → double_injection}/double_injection_has_original_method_spec.rb +9 -9
- data/spec/rr/double_injection/double_injection_reset_spec.rb +90 -0
- data/spec/rr/double_injection/double_injection_spec.rb +77 -0
- data/spec/rr/double_injection/double_injection_verify_spec.rb +29 -0
- data/spec/rr/double_spec.rb +156 -136
- data/spec/rr/errors/rr_error_spec.rb +1 -1
- data/spec/rr/expectations/any_argument_expectation_spec.rb +1 -1
- data/spec/rr/expectations/anything_argument_equality_expectation_spec.rb +6 -30
- data/spec/rr/expectations/argument_equality_expectation_spec.rb +35 -18
- data/spec/rr/expectations/boolean_argument_equality_expectation_spec.rb +22 -41
- data/spec/rr/expectations/hash_including_argument_equality_expectation_spec.rb +82 -0
- data/spec/rr/expectations/hash_including_spec.rb +17 -0
- data/spec/rr/expectations/satisfy_argument_equality_expectation_spec.rb +59 -0
- data/spec/rr/expectations/satisfy_spec.rb +14 -0
- data/spec/rr/expectations/times_called_expectation/times_called_expectation_any_times_spec.rb +30 -28
- data/spec/rr/expectations/times_called_expectation/times_called_expectation_at_least_spec.rb +55 -54
- data/spec/rr/expectations/times_called_expectation/times_called_expectation_at_most_spec.rb +49 -48
- data/spec/rr/expectations/times_called_expectation/times_called_expectation_helper.rb +9 -7
- data/spec/rr/expectations/times_called_expectation/times_called_expectation_integer_spec.rb +77 -76
- data/spec/rr/expectations/times_called_expectation/times_called_expectation_proc_spec.rb +58 -57
- data/spec/rr/expectations/times_called_expectation/times_called_expectation_range_spec.rb +59 -58
- data/spec/rr/expectations/times_called_expectation/times_called_expectation_spec.rb +25 -24
- data/spec/rr/rspec/rspec_adapter_spec.rb +12 -11
- data/spec/rr/rspec/rspec_backtrace_tweaking_spec.rb +10 -8
- data/spec/rr/rspec/rspec_usage_spec.rb +1 -1
- data/spec/rr/space/hash_with_object_id_key_spec.rb +1 -1
- data/spec/rr/space/space_spec.rb +330 -192
- data/spec/rr/test_unit/test_helper.rb +1 -2
- data/spec/rr/test_unit/test_unit_backtrace_test.rb +1 -2
- data/spec/rr/test_unit/test_unit_integration_test.rb +1 -2
- data/spec/rr/times_called_matchers/any_times_matcher_spec.rb +1 -1
- data/spec/rr/times_called_matchers/at_least_matcher_spec.rb +1 -1
- data/spec/rr/times_called_matchers/at_most_matcher_spec.rb +1 -1
- data/spec/rr/times_called_matchers/integer_matcher_spec.rb +1 -1
- data/spec/rr/times_called_matchers/proc_matcher_spec.rb +1 -1
- data/spec/rr/times_called_matchers/range_matcher_spec.rb +1 -1
- data/spec/rr/times_called_matchers/times_called_matcher_spec.rb +1 -1
- data/spec/rr/wildcard_matchers/anything_spec.rb +24 -0
- data/spec/rr/wildcard_matchers/boolean_spec.rb +36 -0
- data/spec/rr/wildcard_matchers/duck_type_spec.rb +52 -0
- data/spec/rr/wildcard_matchers/is_a_spec.rb +32 -0
- data/spec/rr/wildcard_matchers/numeric_spec.rb +32 -0
- data/spec/rr/wildcard_matchers/range_spec.rb +35 -0
- data/spec/rr/wildcard_matchers/regexp_spec.rb +43 -0
- data/spec/rr_spec.rb +28 -0
- data/spec/spec_helper.rb +84 -0
- metadata +43 -29
- data/lib/rr/double_creator.rb +0 -271
- data/lib/rr/double_definition.rb +0 -179
- data/lib/rr/double_definition_builder.rb +0 -44
- data/lib/rr/double_definition_creator.rb +0 -156
- data/lib/rr/double_definition_creator_proxy.rb +0 -20
- data/spec/rr/double/double_injection_bind_spec.rb +0 -105
- data/spec/rr/double/double_injection_dispatching_spec.rb +0 -228
- data/spec/rr/double/double_injection_reset_spec.rb +0 -86
- data/spec/rr/double/double_injection_spec.rb +0 -72
- data/spec/rr/double/double_injection_verify_spec.rb +0 -24
- data/spec/rr/double_definition_creator_proxy_spec.rb +0 -85
- data/spec/rr/double_definition_creator_spec.rb +0 -496
- data/spec/rr/double_definition_spec.rb +0 -815
- data/spec/rr/expectations/anything_spec.rb +0 -14
- data/spec/rr/expectations/boolean_spec.rb +0 -14
- data/spec/rr/expectations/duck_type_argument_equality_expectation_spec.rb +0 -71
- data/spec/rr/expectations/duck_type_spec.rb +0 -14
- data/spec/rr/expectations/is_a_argument_equality_expectation_spec.rb +0 -51
- data/spec/rr/expectations/is_a_spec.rb +0 -14
- data/spec/rr/expectations/numeric_argument_equality_expectation_spec.rb +0 -47
- data/spec/rr/expectations/numeric_spec.rb +0 -14
- data/spec/rr/expectations/range_argument_equality_expectation_spec.rb +0 -59
- data/spec/rr/expectations/range_spec.rb +0 -10
- data/spec/rr/expectations/regexp_argument_equality_expectation_spec.rb +0 -72
- data/spec/rr/expectations/regexp_spec.rb +0 -10
@@ -1,86 +0,0 @@
|
|
1
|
-
require "spec/spec_helper"
|
2
|
-
|
3
|
-
module RR
|
4
|
-
describe DoubleInjection, "#reset", :shared => true do
|
5
|
-
it "cleans up by removing the __rr__method" do
|
6
|
-
@double_injection.bind
|
7
|
-
@object.methods.should include("__rr__foobar")
|
8
|
-
|
9
|
-
@double_injection.reset
|
10
|
-
@object.methods.should_not include("__rr__foobar")
|
11
|
-
end
|
12
|
-
end
|
13
|
-
|
14
|
-
describe DoubleInjection, "#reset when method does not exist" do
|
15
|
-
it_should_behave_like "RR::DoubleInjection#reset"
|
16
|
-
|
17
|
-
before do
|
18
|
-
@object = Object.new
|
19
|
-
@method_name = :foobar
|
20
|
-
@object.methods.should_not include(@method_name.to_s)
|
21
|
-
@double_injection = DoubleInjection.new(@object, @method_name)
|
22
|
-
end
|
23
|
-
|
24
|
-
it "removes the method" do
|
25
|
-
@double_injection.bind
|
26
|
-
@object.methods.should include(@method_name.to_s)
|
27
|
-
|
28
|
-
@double_injection.reset
|
29
|
-
@object.methods.should_not include(@method_name.to_s)
|
30
|
-
lambda {@object.foobar}.should raise_error(NoMethodError)
|
31
|
-
end
|
32
|
-
end
|
33
|
-
|
34
|
-
describe DoubleInjection, "#reset when method exists" do
|
35
|
-
it_should_behave_like "RR::DoubleInjection#reset"
|
36
|
-
|
37
|
-
before do
|
38
|
-
@object = Object.new
|
39
|
-
@method_name = :foobar
|
40
|
-
def @object.foobar
|
41
|
-
:original_foobar
|
42
|
-
end
|
43
|
-
@object.methods.should include(@method_name.to_s)
|
44
|
-
@original_method = @object.method(@method_name)
|
45
|
-
@double_injection = DoubleInjection.new(@object, @method_name)
|
46
|
-
|
47
|
-
@double_injection.bind
|
48
|
-
@object.methods.should include(@method_name.to_s)
|
49
|
-
end
|
50
|
-
|
51
|
-
it "rebind original method" do
|
52
|
-
@double_injection.reset
|
53
|
-
@object.methods.should include(@method_name.to_s)
|
54
|
-
@object.foobar.should == :original_foobar
|
55
|
-
end
|
56
|
-
end
|
57
|
-
|
58
|
-
describe DoubleInjection, "#reset when method with block exists" do
|
59
|
-
it_should_behave_like "RR::DoubleInjection#reset"
|
60
|
-
|
61
|
-
before do
|
62
|
-
@object = Object.new
|
63
|
-
@method_name = :foobar
|
64
|
-
def @object.foobar
|
65
|
-
yield(:original_argument)
|
66
|
-
end
|
67
|
-
@object.methods.should include(@method_name.to_s)
|
68
|
-
@original_method = @object.method(@method_name)
|
69
|
-
@double_injection = DoubleInjection.new(@object, @method_name)
|
70
|
-
|
71
|
-
@double_injection.bind
|
72
|
-
@object.methods.should include(@method_name.to_s)
|
73
|
-
end
|
74
|
-
|
75
|
-
it "rebinds original method with block" do
|
76
|
-
@double_injection.reset
|
77
|
-
@object.methods.should include(@method_name.to_s)
|
78
|
-
|
79
|
-
original_argument = nil
|
80
|
-
@object.foobar do |arg|
|
81
|
-
original_argument = arg
|
82
|
-
end
|
83
|
-
original_argument.should == :original_argument
|
84
|
-
end
|
85
|
-
end
|
86
|
-
end
|
@@ -1,72 +0,0 @@
|
|
1
|
-
require "spec/spec_helper"
|
2
|
-
|
3
|
-
module RR
|
4
|
-
describe DoubleInjection, :shared => true do
|
5
|
-
it "sets up object and method_name" do
|
6
|
-
@double_injection.object.should === @object
|
7
|
-
@double_injection.method_name.should == @method_name.to_sym
|
8
|
-
end
|
9
|
-
end
|
10
|
-
|
11
|
-
describe DoubleInjection, "#initialize where method_name is a symbol" do
|
12
|
-
it_should_behave_like "RR::DoubleInjection"
|
13
|
-
|
14
|
-
before do
|
15
|
-
@object = Object.new
|
16
|
-
@method_name = :foobar
|
17
|
-
@object.methods.should_not include(@method_name.to_s)
|
18
|
-
@double_injection = DoubleInjection.new(@object, @method_name)
|
19
|
-
end
|
20
|
-
end
|
21
|
-
|
22
|
-
describe DoubleInjection, "#initialize where method_name is a string" do
|
23
|
-
it_should_behave_like "RR::DoubleInjection"
|
24
|
-
|
25
|
-
before do
|
26
|
-
@object = Object.new
|
27
|
-
@method_name = 'foobar'
|
28
|
-
@object.methods.should_not include(@method_name)
|
29
|
-
@double_injection = DoubleInjection.new(@object, @method_name)
|
30
|
-
end
|
31
|
-
end
|
32
|
-
|
33
|
-
describe DoubleInjection, "#initialize where method does not exist on object" do
|
34
|
-
it_should_behave_like "RR::DoubleInjection"
|
35
|
-
|
36
|
-
before do
|
37
|
-
@object = Object.new
|
38
|
-
@method_name = :foobar
|
39
|
-
@object.methods.should_not include(@method_name.to_s)
|
40
|
-
@double_injection = DoubleInjection.new(@object, @method_name)
|
41
|
-
end
|
42
|
-
|
43
|
-
it "object does not have original method" do
|
44
|
-
@double_injection.object_has_original_method?.should be_false
|
45
|
-
end
|
46
|
-
end
|
47
|
-
|
48
|
-
describe DoubleInjection, "#initialize where method exists on object" do
|
49
|
-
it_should_behave_like "RR::DoubleInjection"
|
50
|
-
|
51
|
-
before do
|
52
|
-
@object = Object.new
|
53
|
-
@method_name = :to_s
|
54
|
-
@object.methods.should include(@method_name.to_s)
|
55
|
-
@double_injection = DoubleInjection.new(@object, @method_name)
|
56
|
-
end
|
57
|
-
|
58
|
-
it "has a original_method" do
|
59
|
-
@double_injection.object_has_original_method?.should be_true
|
60
|
-
end
|
61
|
-
end
|
62
|
-
|
63
|
-
describe DoubleInjection, "#initialize where method_name is ==" do
|
64
|
-
it_should_behave_like "RR::DoubleInjection"
|
65
|
-
|
66
|
-
before do
|
67
|
-
@object = Object.new
|
68
|
-
@method_name = '=='
|
69
|
-
@double_injection = DoubleInjection.new(@object, @method_name)
|
70
|
-
end
|
71
|
-
end
|
72
|
-
end
|
@@ -1,24 +0,0 @@
|
|
1
|
-
require "spec/spec_helper"
|
2
|
-
|
3
|
-
module RR
|
4
|
-
describe DoubleInjection, "#verify" do
|
5
|
-
it_should_behave_like "Swapped Space"
|
6
|
-
before do
|
7
|
-
@space = Space.instance
|
8
|
-
@object = Object.new
|
9
|
-
@method_name = :foobar
|
10
|
-
@object.methods.should_not include(@method_name.to_s)
|
11
|
-
@double_injection = @space.double_injection(@object, @method_name)
|
12
|
-
end
|
13
|
-
|
14
|
-
it "verifies each double was met" do
|
15
|
-
double = Double.new(@double_injection, DoubleDefinition.new)
|
16
|
-
@double_injection.register_double double
|
17
|
-
|
18
|
-
double.with(1).once.returns {nil}
|
19
|
-
lambda {@double_injection.verify}.should raise_error(Errors::TimesCalledError)
|
20
|
-
@object.foobar(1)
|
21
|
-
lambda {@double_injection.verify}.should_not raise_error
|
22
|
-
end
|
23
|
-
end
|
24
|
-
end
|
@@ -1,85 +0,0 @@
|
|
1
|
-
require "spec/spec_helper"
|
2
|
-
|
3
|
-
module RR
|
4
|
-
describe DoubleDefinitionCreatorProxy do
|
5
|
-
attr_reader :space, :subject, :creator, :the_proxy
|
6
|
-
it_should_behave_like "Swapped Space"
|
7
|
-
|
8
|
-
before(:each) do
|
9
|
-
@space = Space.instance
|
10
|
-
@subject = Object.new
|
11
|
-
@creator = DoubleDefinitionCreator.new
|
12
|
-
creator.mock
|
13
|
-
end
|
14
|
-
|
15
|
-
class << self
|
16
|
-
define_method("initializes proxy with passed in creator") do
|
17
|
-
it "initializes proxy with passed in creator" do
|
18
|
-
class << the_proxy
|
19
|
-
attr_reader :creator
|
20
|
-
end
|
21
|
-
the_proxy.creator.should === creator
|
22
|
-
end
|
23
|
-
end
|
24
|
-
end
|
25
|
-
|
26
|
-
describe ".new" do
|
27
|
-
it "does not undefine object_id" do
|
28
|
-
the_proxy = DoubleDefinitionCreatorProxy.new(creator, subject)
|
29
|
-
the_proxy.object_id.class.should == Fixnum
|
30
|
-
end
|
31
|
-
|
32
|
-
context "without block" do
|
33
|
-
before do
|
34
|
-
@the_proxy = DoubleDefinitionCreatorProxy.new(creator, subject)
|
35
|
-
end
|
36
|
-
|
37
|
-
send "initializes proxy with passed in creator"
|
38
|
-
|
39
|
-
it "clears out all methods from proxy" do
|
40
|
-
proxy_subclass = Class.new(DoubleDefinitionCreatorProxy) do
|
41
|
-
def i_should_be_a_double
|
42
|
-
end
|
43
|
-
end
|
44
|
-
proxy_subclass.instance_methods.map {|m| m.to_s}.should include('i_should_be_a_double')
|
45
|
-
|
46
|
-
proxy = proxy_subclass.new(creator, subject)
|
47
|
-
proxy.i_should_be_a_double.should be_instance_of(DoubleDefinition)
|
48
|
-
end
|
49
|
-
end
|
50
|
-
|
51
|
-
context "with block" do
|
52
|
-
before do
|
53
|
-
@the_proxy = DoubleDefinitionCreatorProxy.new(creator, subject) do |b|
|
54
|
-
b.foobar(1, 2) {:one_two}
|
55
|
-
b.foobar(1) {:one}
|
56
|
-
b.foobar.with_any_args {:default}
|
57
|
-
b.baz() {:baz_result}
|
58
|
-
end
|
59
|
-
end
|
60
|
-
|
61
|
-
send "initializes proxy with passed in creator"
|
62
|
-
|
63
|
-
it "creates double_injections" do
|
64
|
-
subject.foobar(1, 2).should == :one_two
|
65
|
-
subject.foobar(1).should == :one
|
66
|
-
subject.foobar(:something).should == :default
|
67
|
-
subject.baz.should == :baz_result
|
68
|
-
end
|
69
|
-
|
70
|
-
it "clears out all methods from proxy" do
|
71
|
-
proxy_subclass = Class.new(DoubleDefinitionCreatorProxy) do
|
72
|
-
def i_should_be_a_double
|
73
|
-
end
|
74
|
-
end
|
75
|
-
proxy_subclass.instance_methods.map {|m| m.to_s}.should include('i_should_be_a_double')
|
76
|
-
|
77
|
-
proxy_subclass.new(creator, subject) do |m|
|
78
|
-
m.i_should_be_a_double.should be_instance_of(DoubleDefinition)
|
79
|
-
end
|
80
|
-
end
|
81
|
-
end
|
82
|
-
end
|
83
|
-
|
84
|
-
end
|
85
|
-
end
|
@@ -1,496 +0,0 @@
|
|
1
|
-
require "spec/spec_helper"
|
2
|
-
|
3
|
-
module RR
|
4
|
-
describe DoubleDefinitionCreator, " 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 DoubleDefinitionCreatorProxy when passed a subject" do
|
10
|
-
double = creator.__send__(method_name, subject).foobar
|
11
|
-
double.should be_instance_of(DoubleDefinition)
|
12
|
-
end
|
13
|
-
|
14
|
-
it "returns a DoubleDefinitionCreatorProxy when passed Kernel" do
|
15
|
-
double = creator.__send__(method_name, Kernel).foobar
|
16
|
-
double.should be_instance_of(DoubleDefinition)
|
17
|
-
end
|
18
|
-
|
19
|
-
it "raises error if passed a method name and a block" do
|
20
|
-
lambda 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
|
-
lambda 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
|
-
lambda 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
|
-
lambda 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 DoubleDefinitionCreator do
|
57
|
-
attr_reader :creator, :subject, :space, :method_name
|
58
|
-
it_should_behave_like "Swapped Space"
|
59
|
-
before(:each) do
|
60
|
-
@space = Space.instance
|
61
|
-
@subject = Object.new
|
62
|
-
@creator = DoubleDefinitionCreator.new
|
63
|
-
end
|
64
|
-
|
65
|
-
describe "#mock" do
|
66
|
-
it_should_behave_like "RR::DoubleDefinitionCreator strategy definition"
|
67
|
-
|
68
|
-
before do
|
69
|
-
@method_name = :mock
|
70
|
-
end
|
71
|
-
|
72
|
-
it "sets up the RR mock call chain" do
|
73
|
-
creates_mock_call_chain(creator.mock(subject))
|
74
|
-
end
|
75
|
-
|
76
|
-
it "sets up the DoubleDefinition to be in returns block_callback_strategy" do
|
77
|
-
double = creator.mock(subject, :foobar)
|
78
|
-
double.block_callback_strategy.should == :returns
|
79
|
-
end
|
80
|
-
|
81
|
-
def creates_mock_call_chain(creator)
|
82
|
-
double = creator.foobar(1, 2) {:baz}
|
83
|
-
double.times_matcher.should == TimesCalledMatchers::IntegerMatcher.new(1)
|
84
|
-
double.argument_expectation.class.should == RR::Expectations::ArgumentEqualityExpectation
|
85
|
-
double.argument_expectation.expected_arguments.should == [1, 2]
|
86
|
-
|
87
|
-
subject.foobar(1, 2).should == :baz
|
88
|
-
end
|
89
|
-
|
90
|
-
context "when passed a second argument" do
|
91
|
-
it "creates a mock Double for method" do
|
92
|
-
creates_double_with_method_name( creator.mock(subject, :foobar) )
|
93
|
-
end
|
94
|
-
|
95
|
-
def creates_double_with_method_name(double)
|
96
|
-
double.with(1, 2) {:baz}
|
97
|
-
double.times_matcher.should == TimesCalledMatchers::IntegerMatcher.new(1)
|
98
|
-
double.argument_expectation.class.should == RR::Expectations::ArgumentEqualityExpectation
|
99
|
-
double.argument_expectation.expected_arguments.should == [1, 2]
|
100
|
-
|
101
|
-
subject.foobar(1, 2).should == :baz
|
102
|
-
end
|
103
|
-
end
|
104
|
-
|
105
|
-
context "when passed method_name and block" do
|
106
|
-
it "raises error" do
|
107
|
-
lambda do
|
108
|
-
@creator.mock(@object, :foobar) {}
|
109
|
-
end.should raise_error(ArgumentError, "Cannot pass in a method name and a block")
|
110
|
-
end
|
111
|
-
end
|
112
|
-
end
|
113
|
-
|
114
|
-
describe "#stub" do
|
115
|
-
it_should_behave_like "RR::DoubleDefinitionCreator strategy definition"
|
116
|
-
|
117
|
-
before do
|
118
|
-
@method_name = :stub
|
119
|
-
end
|
120
|
-
|
121
|
-
it "sets up the RR stub call chain" do
|
122
|
-
creates_stub_call_chain(creator.stub(subject))
|
123
|
-
end
|
124
|
-
|
125
|
-
it "sets up the DoubleDefinition to be in returns block_callback_strategy" do
|
126
|
-
double = creator.stub(subject, :foobar)
|
127
|
-
double.block_callback_strategy.should == :returns
|
128
|
-
end
|
129
|
-
|
130
|
-
context "when passed a second argument" do
|
131
|
-
it "creates a stub Double for method when passed a second argument" do
|
132
|
-
creates_double_with_method_name(creator.stub(subject, :foobar))
|
133
|
-
end
|
134
|
-
|
135
|
-
def creates_double_with_method_name(double)
|
136
|
-
double.with(1, 2) {:baz}
|
137
|
-
double.times_matcher.should == TimesCalledMatchers::AnyTimesMatcher.new
|
138
|
-
double.argument_expectation.class.should == RR::Expectations::ArgumentEqualityExpectation
|
139
|
-
subject.foobar(1, 2).should == :baz
|
140
|
-
end
|
141
|
-
end
|
142
|
-
|
143
|
-
def creates_stub_call_chain(creator)
|
144
|
-
double = creator.foobar(1, 2) {:baz}
|
145
|
-
double.times_matcher.should == TimesCalledMatchers::AnyTimesMatcher.new
|
146
|
-
double.argument_expectation.class.should == RR::Expectations::ArgumentEqualityExpectation
|
147
|
-
subject.foobar(1, 2).should == :baz
|
148
|
-
end
|
149
|
-
|
150
|
-
context "when passed method_name and block" do
|
151
|
-
it "raises error" do
|
152
|
-
lambda do
|
153
|
-
@creator.stub(@object, :foobar) {}
|
154
|
-
end.should raise_error(ArgumentError, "Cannot pass in a method name and a block")
|
155
|
-
end
|
156
|
-
end
|
157
|
-
end
|
158
|
-
|
159
|
-
describe "#dont_allow" do
|
160
|
-
it_should_behave_like "RR::DoubleDefinitionCreator strategy definition"
|
161
|
-
|
162
|
-
before do
|
163
|
-
@method_name = :dont_allow
|
164
|
-
end
|
165
|
-
|
166
|
-
it "raises error when proxied" do
|
167
|
-
creator.proxy
|
168
|
-
lambda do
|
169
|
-
creator.dont_allow
|
170
|
-
end.should raise_error(
|
171
|
-
Errors::DoubleDefinitionError,
|
172
|
-
"Doubles cannot be proxied when using dont_allow strategy"
|
173
|
-
)
|
174
|
-
end
|
175
|
-
|
176
|
-
it "sets up the RR dont_allow call chain" do
|
177
|
-
creates_dont_allow_call_chain(creator.dont_allow(subject))
|
178
|
-
end
|
179
|
-
|
180
|
-
it "sets up the RR dont_allow call chain" do
|
181
|
-
creates_dont_allow_call_chain(creator.dont_call(subject))
|
182
|
-
end
|
183
|
-
|
184
|
-
it "sets up the RR dont_allow call chain" do
|
185
|
-
creates_dont_allow_call_chain(creator.do_not_allow(subject))
|
186
|
-
end
|
187
|
-
|
188
|
-
it "sets up the RR dont_allow call chain" do
|
189
|
-
creates_dont_allow_call_chain(creator.dont_allow(subject))
|
190
|
-
end
|
191
|
-
|
192
|
-
context "when passed a second argument_expectation" do
|
193
|
-
it "creates a mock Double for method" do
|
194
|
-
creates_double_with_method_name(creator.dont_allow(subject, :foobar))
|
195
|
-
end
|
196
|
-
|
197
|
-
it "creates a mock Double for method" do
|
198
|
-
creates_double_with_method_name(creator.dont_call(subject, :foobar))
|
199
|
-
end
|
200
|
-
|
201
|
-
it "creates a mock Double for method" do
|
202
|
-
creates_double_with_method_name(creator.do_not_allow(subject, :foobar))
|
203
|
-
end
|
204
|
-
|
205
|
-
it "creates a mock Double for method" do
|
206
|
-
creates_double_with_method_name(creator.dont_allow(subject, :foobar))
|
207
|
-
end
|
208
|
-
|
209
|
-
def creates_double_with_method_name(double)
|
210
|
-
double.with(1, 2)
|
211
|
-
double.times_matcher.should == TimesCalledMatchers::IntegerMatcher.new(0)
|
212
|
-
double.argument_expectation.class.should == RR::Expectations::ArgumentEqualityExpectation
|
213
|
-
double.argument_expectation.expected_arguments.should == [1, 2]
|
214
|
-
|
215
|
-
lambda do
|
216
|
-
subject.foobar(1, 2)
|
217
|
-
end.should raise_error(Errors::TimesCalledError)
|
218
|
-
reset
|
219
|
-
nil
|
220
|
-
end
|
221
|
-
end
|
222
|
-
|
223
|
-
def creates_dont_allow_call_chain(creator)
|
224
|
-
double = creator.foobar(1, 2)
|
225
|
-
double.times_matcher.should == TimesCalledMatchers::IntegerMatcher.new(0)
|
226
|
-
double.argument_expectation.class.should == RR::Expectations::ArgumentEqualityExpectation
|
227
|
-
double.argument_expectation.expected_arguments.should == [1, 2]
|
228
|
-
|
229
|
-
lambda do
|
230
|
-
subject.foobar(1, 2)
|
231
|
-
end.should raise_error(Errors::TimesCalledError)
|
232
|
-
reset
|
233
|
-
nil
|
234
|
-
end
|
235
|
-
|
236
|
-
context "when passed method_name and block" do
|
237
|
-
it "raises error" do
|
238
|
-
lambda do
|
239
|
-
@creator.dont_allow(@object, :foobar) {}
|
240
|
-
end.should raise_error(ArgumentError, "Cannot pass in a method name and a block")
|
241
|
-
end
|
242
|
-
end
|
243
|
-
end
|
244
|
-
|
245
|
-
describe "(#proxy or #proxy) and #stub" do
|
246
|
-
before do
|
247
|
-
class << subject
|
248
|
-
def foobar(*args)
|
249
|
-
:original_foobar
|
250
|
-
end
|
251
|
-
end
|
252
|
-
end
|
253
|
-
|
254
|
-
it "raises error when using dont_allow strategy" do
|
255
|
-
creator.dont_allow
|
256
|
-
lambda do
|
257
|
-
creator.proxy
|
258
|
-
end.should raise_error(
|
259
|
-
Errors::DoubleDefinitionError,
|
260
|
-
"Doubles cannot be proxied when using dont_allow strategy"
|
261
|
-
)
|
262
|
-
end
|
263
|
-
|
264
|
-
it "sets up the RR proxy call chain" do
|
265
|
-
double = creator.stub.proxy(subject).foobar(1, 2) {:baz}
|
266
|
-
double.times_matcher.should == TimesCalledMatchers::AnyTimesMatcher.new
|
267
|
-
double.argument_expectation.class.should == RR::Expectations::ArgumentEqualityExpectation
|
268
|
-
subject.foobar(1, 2).should == :baz
|
269
|
-
end
|
270
|
-
|
271
|
-
it "sets up the RR proxy call chain" do
|
272
|
-
double = creator.stub.proxy(subject).foobar(1, 2) {:baz}
|
273
|
-
double.times_matcher.should == TimesCalledMatchers::AnyTimesMatcher.new
|
274
|
-
double.argument_expectation.class.should == RR::Expectations::ArgumentEqualityExpectation
|
275
|
-
subject.foobar(1, 2).should == :baz
|
276
|
-
end
|
277
|
-
|
278
|
-
context "when passed a second argument" do
|
279
|
-
it "creates a proxy Double for method" do
|
280
|
-
double = creator.stub.proxy(subject, :foobar)
|
281
|
-
double.with(1, 2) {:baz}
|
282
|
-
double.times_matcher.should == TimesCalledMatchers::AnyTimesMatcher.new
|
283
|
-
double.argument_expectation.class.should == RR::Expectations::ArgumentEqualityExpectation
|
284
|
-
subject.foobar(1, 2).should == :baz
|
285
|
-
end
|
286
|
-
|
287
|
-
it "creates a proxy Double for method" do
|
288
|
-
double = creator.stub.proxy(subject, :foobar)
|
289
|
-
double.with(1, 2) {:baz}
|
290
|
-
double.times_matcher.should == TimesCalledMatchers::AnyTimesMatcher.new
|
291
|
-
double.argument_expectation.class.should == RR::Expectations::ArgumentEqualityExpectation
|
292
|
-
subject.foobar(1, 2).should == :baz
|
293
|
-
end
|
294
|
-
end
|
295
|
-
|
296
|
-
it "sets up the DoubleDefinition to be in after_call block_callback_strategy" do
|
297
|
-
def subject.foobar
|
298
|
-
:original_implementation_value
|
299
|
-
end
|
300
|
-
|
301
|
-
args = nil
|
302
|
-
double = creator.stub.proxy(subject, :foobar).with() do |*args|
|
303
|
-
args = args
|
304
|
-
end
|
305
|
-
subject.foobar
|
306
|
-
args.should == [:original_implementation_value]
|
307
|
-
double.block_callback_strategy.should == :after_call
|
308
|
-
end
|
309
|
-
|
310
|
-
context "when passed method_name and block" do
|
311
|
-
it "raises error" do
|
312
|
-
lambda do
|
313
|
-
@creator.proxy(@object, :foobar) {}
|
314
|
-
end.should raise_error(ArgumentError, "Cannot pass in a method name and a block")
|
315
|
-
end
|
316
|
-
end
|
317
|
-
end
|
318
|
-
|
319
|
-
describe "#instance_of" do
|
320
|
-
it "raises an error when not passed a class" do
|
321
|
-
lambda do
|
322
|
-
creator.instance_of(Object.new)
|
323
|
-
end.should raise_error(ArgumentError, "instance_of only accepts class objects")
|
324
|
-
end
|
325
|
-
|
326
|
-
it "sets up the RR proxy call chain" do
|
327
|
-
klass = Class.new
|
328
|
-
double = creator.stub.instance_of(klass).foobar(1, 2) {:baz}
|
329
|
-
double.times_matcher.should == TimesCalledMatchers::AnyTimesMatcher.new
|
330
|
-
double.argument_expectation.class.should == RR::Expectations::ArgumentEqualityExpectation
|
331
|
-
klass.new.foobar(1, 2).should == :baz
|
332
|
-
end
|
333
|
-
|
334
|
-
context "when passed a second argument" do
|
335
|
-
it "creates a proxy Double for method" do
|
336
|
-
klass = Class.new
|
337
|
-
double = creator.stub.instance_of(klass, :foobar)
|
338
|
-
double.with(1, 2) {:baz}
|
339
|
-
double.times_matcher.should == TimesCalledMatchers::AnyTimesMatcher.new
|
340
|
-
double.argument_expectation.class.should == RR::Expectations::ArgumentEqualityExpectation
|
341
|
-
klass.new.foobar(1, 2).should == :baz
|
342
|
-
end
|
343
|
-
end
|
344
|
-
|
345
|
-
it "raises error" do
|
346
|
-
klass = Class.new
|
347
|
-
lambda do
|
348
|
-
@creator.instance_of(klass, :foobar) {}
|
349
|
-
end.should raise_error(ArgumentError, "Cannot pass in a method name and a block")
|
350
|
-
end
|
351
|
-
end
|
352
|
-
|
353
|
-
describe "#create using no strategy" do
|
354
|
-
it "raises error" do
|
355
|
-
lambda do
|
356
|
-
creator.create(subject, :foobar, 1, 2)
|
357
|
-
end.should raise_error(
|
358
|
-
Errors::DoubleDefinitionError,
|
359
|
-
"This Double has no strategy"
|
360
|
-
)
|
361
|
-
end
|
362
|
-
end
|
363
|
-
|
364
|
-
describe "#create using mock strategy" do
|
365
|
-
before do
|
366
|
-
creator.mock
|
367
|
-
end
|
368
|
-
|
369
|
-
it "sets expectations on the subject" do
|
370
|
-
creator.create(subject, :foobar, 1, 2) {:baz}.twice
|
371
|
-
|
372
|
-
subject.foobar(1, 2).should == :baz
|
373
|
-
subject.foobar(1, 2).should == :baz
|
374
|
-
lambda {subject.foobar(1, 2)}.should raise_error(Errors::TimesCalledError)
|
375
|
-
end
|
376
|
-
end
|
377
|
-
|
378
|
-
describe "#create using stub strategy" do
|
379
|
-
before do
|
380
|
-
creator.stub
|
381
|
-
end
|
382
|
-
|
383
|
-
it "stubs the subject without any args" do
|
384
|
-
creator.create(subject, :foobar) {:baz}
|
385
|
-
subject.foobar.should == :baz
|
386
|
-
end
|
387
|
-
|
388
|
-
it "stubs the subject mapping passed in args with the output" do
|
389
|
-
creator.create(subject, :foobar, 1, 2) {:one_two}
|
390
|
-
creator.create(subject, :foobar, 1) {:one}
|
391
|
-
creator.create(subject, :foobar) {:nothing}
|
392
|
-
subject.foobar.should == :nothing
|
393
|
-
subject.foobar(1).should == :one
|
394
|
-
subject.foobar(1, 2).should == :one_two
|
395
|
-
end
|
396
|
-
end
|
397
|
-
|
398
|
-
describe "#create using dont_allow strategy" do
|
399
|
-
before do
|
400
|
-
creator.dont_allow
|
401
|
-
end
|
402
|
-
|
403
|
-
it "sets expectation for method to never be called with any arguments when on arguments passed in" do
|
404
|
-
creator.create(subject, :foobar)
|
405
|
-
lambda {subject.foobar}.should raise_error(Errors::TimesCalledError)
|
406
|
-
lambda {subject.foobar(1, 2)}.should raise_error(Errors::TimesCalledError)
|
407
|
-
end
|
408
|
-
|
409
|
-
it "sets expectation for method to never be called with passed in arguments" do
|
410
|
-
creator.create(subject, :foobar, 1, 2)
|
411
|
-
lambda {subject.foobar}.should raise_error(Errors::DoubleNotFoundError)
|
412
|
-
lambda {subject.foobar(1, 2)}.should raise_error(Errors::TimesCalledError)
|
413
|
-
end
|
414
|
-
|
415
|
-
it "sets expectation for method to never be called with no arguments when with_no_args is set" do
|
416
|
-
creator.create(subject, :foobar).with_no_args
|
417
|
-
lambda {subject.foobar}.should raise_error(Errors::TimesCalledError)
|
418
|
-
lambda {subject.foobar(1, 2)}.should raise_error(Errors::DoubleNotFoundError)
|
419
|
-
end
|
420
|
-
end
|
421
|
-
|
422
|
-
describe "#create using mock strategy with proxy" do
|
423
|
-
before do
|
424
|
-
creator.mock
|
425
|
-
creator.proxy
|
426
|
-
end
|
427
|
-
|
428
|
-
it "sets expectations on the subject while calling the original method" do
|
429
|
-
def subject.foobar(*args)
|
430
|
-
; :baz;
|
431
|
-
end
|
432
|
-
creator.create(subject, :foobar, 1, 2).twice
|
433
|
-
subject.foobar(1, 2).should == :baz
|
434
|
-
subject.foobar(1, 2).should == :baz
|
435
|
-
lambda {subject.foobar(1, 2)}.should raise_error(Errors::TimesCalledError)
|
436
|
-
end
|
437
|
-
|
438
|
-
it "sets after_call on the double when passed a block" do
|
439
|
-
real_value = Object.new
|
440
|
-
(class << subject; self; 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
|
-
|
454
|
-
describe "#create using stub strategy with proxy" do
|
455
|
-
before do
|
456
|
-
creator.stub
|
457
|
-
creator.proxy
|
458
|
-
end
|
459
|
-
|
460
|
-
it "sets up a double with passed in arguments" do
|
461
|
-
def subject.foobar(*args)
|
462
|
-
:baz
|
463
|
-
end
|
464
|
-
creator.create(subject, :foobar, 1, 2)
|
465
|
-
lambda do
|
466
|
-
subject.foobar
|
467
|
-
end.should raise_error(Errors::DoubleNotFoundError)
|
468
|
-
end
|
469
|
-
|
470
|
-
it "sets expectations on the subject while calling the original method" do
|
471
|
-
def subject.foobar(*args)
|
472
|
-
:baz
|
473
|
-
end
|
474
|
-
creator.create(subject, :foobar, 1, 2) {:new_value}
|
475
|
-
10.times do
|
476
|
-
subject.foobar(1, 2).should == :new_value
|
477
|
-
end
|
478
|
-
end
|
479
|
-
|
480
|
-
it "sets after_call on the double when passed a block" do
|
481
|
-
real_value = Object.new
|
482
|
-
(class << subject; self; end).class_eval do
|
483
|
-
define_method(:foobar) {real_value}
|
484
|
-
end
|
485
|
-
creator.create(subject, :foobar, 1, 2) do |value|
|
486
|
-
mock(value).a_method {99}
|
487
|
-
value
|
488
|
-
end
|
489
|
-
|
490
|
-
return_value = subject.foobar(1, 2)
|
491
|
-
return_value.should === return_value
|
492
|
-
return_value.a_method.should == 99
|
493
|
-
end
|
494
|
-
end
|
495
|
-
end
|
496
|
-
end
|