mcmire-rr 1.0.5.rc1
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGES +269 -0
- data/Gemfile +16 -0
- data/Gemfile.lock +47 -0
- data/LICENSE +22 -0
- data/README.rdoc +390 -0
- data/Rakefile +49 -0
- data/VERSION +1 -0
- data/lib/rr.rb +101 -0
- data/lib/rr/adapters/minitest.rb +31 -0
- data/lib/rr/adapters/rr_methods.rb +146 -0
- data/lib/rr/adapters/rspec.rb +61 -0
- data/lib/rr/adapters/rspec2.rb +22 -0
- data/lib/rr/adapters/test_unit.rb +31 -0
- data/lib/rr/blank_slate.rb +17 -0
- data/lib/rr/class_instance_method_defined.rb +9 -0
- data/lib/rr/double.rb +154 -0
- data/lib/rr/double_definitions/child_double_definition_create.rb +27 -0
- data/lib/rr/double_definitions/double_definition.rb +365 -0
- data/lib/rr/double_definitions/double_definition_create.rb +139 -0
- data/lib/rr/double_definitions/double_definition_create_blank_slate.rb +26 -0
- data/lib/rr/double_definitions/double_injections/any_instance_of.rb +28 -0
- data/lib/rr/double_definitions/double_injections/instance.rb +16 -0
- data/lib/rr/double_definitions/strategies/double_injection/any_instance_of.rb +30 -0
- data/lib/rr/double_definitions/strategies/double_injection/double_injection_strategy.rb +10 -0
- data/lib/rr/double_definitions/strategies/double_injection/instance.rb +17 -0
- data/lib/rr/double_definitions/strategies/implementation/implementation_strategy.rb +10 -0
- data/lib/rr/double_definitions/strategies/implementation/proxy.rb +60 -0
- data/lib/rr/double_definitions/strategies/implementation/reimplementation.rb +14 -0
- data/lib/rr/double_definitions/strategies/implementation/strongly_typed_reimplementation.rb +15 -0
- data/lib/rr/double_definitions/strategies/strategy.rb +43 -0
- data/lib/rr/double_definitions/strategies/strategy_methods.rb +53 -0
- data/lib/rr/double_definitions/strategies/verification/dont_allow.rb +31 -0
- data/lib/rr/double_definitions/strategies/verification/mock.rb +42 -0
- data/lib/rr/double_definitions/strategies/verification/stub.rb +43 -0
- data/lib/rr/double_definitions/strategies/verification/verification_strategy.rb +10 -0
- data/lib/rr/double_matches.rb +42 -0
- data/lib/rr/errors/argument_equality_error.rb +6 -0
- data/lib/rr/errors/double_definition_error.rb +6 -0
- data/lib/rr/errors/double_not_found_error.rb +6 -0
- data/lib/rr/errors/double_order_error.rb +6 -0
- data/lib/rr/errors/rr_error.rb +20 -0
- data/lib/rr/errors/spy_verification_errors/double_injection_not_found_error.rb +8 -0
- data/lib/rr/errors/spy_verification_errors/invocation_count_error.rb +8 -0
- data/lib/rr/errors/spy_verification_errors/spy_verification_error.rb +8 -0
- data/lib/rr/errors/subject_does_not_implement_method_error.rb +6 -0
- data/lib/rr/errors/subject_has_different_arity_error.rb +6 -0
- data/lib/rr/errors/times_called_error.rb +6 -0
- data/lib/rr/expectations/any_argument_expectation.rb +21 -0
- data/lib/rr/expectations/argument_equality_expectation.rb +41 -0
- data/lib/rr/expectations/times_called_expectation.rb +57 -0
- data/lib/rr/hash_with_object_id_key.rb +46 -0
- data/lib/rr/injections/double_injection.rb +220 -0
- data/lib/rr/injections/injection.rb +33 -0
- data/lib/rr/injections/method_missing_injection.rb +73 -0
- data/lib/rr/injections/singleton_method_added_injection.rb +72 -0
- data/lib/rr/method_dispatches/base_method_dispatch.rb +84 -0
- data/lib/rr/method_dispatches/method_dispatch.rb +59 -0
- data/lib/rr/method_dispatches/method_missing_dispatch.rb +61 -0
- data/lib/rr/proc_from_block.rb +7 -0
- data/lib/rr/recorded_calls.rb +103 -0
- data/lib/rr/space.rb +119 -0
- data/lib/rr/spy_verification.rb +48 -0
- data/lib/rr/spy_verification_proxy.rb +13 -0
- data/lib/rr/times_called_matchers/any_times_matcher.rb +18 -0
- data/lib/rr/times_called_matchers/at_least_matcher.rb +15 -0
- data/lib/rr/times_called_matchers/at_most_matcher.rb +23 -0
- data/lib/rr/times_called_matchers/integer_matcher.rb +19 -0
- data/lib/rr/times_called_matchers/never_matcher.rb +23 -0
- data/lib/rr/times_called_matchers/non_terminal.rb +27 -0
- data/lib/rr/times_called_matchers/proc_matcher.rb +11 -0
- data/lib/rr/times_called_matchers/range_matcher.rb +21 -0
- data/lib/rr/times_called_matchers/terminal.rb +20 -0
- data/lib/rr/times_called_matchers/times_called_matcher.rb +44 -0
- data/lib/rr/wildcard_matchers.rb +158 -0
- data/lib/rr/wildcard_matchers/anything.rb +18 -0
- data/lib/rr/wildcard_matchers/boolean.rb +23 -0
- data/lib/rr/wildcard_matchers/duck_type.rb +32 -0
- data/lib/rr/wildcard_matchers/hash_including.rb +29 -0
- data/lib/rr/wildcard_matchers/is_a.rb +25 -0
- data/lib/rr/wildcard_matchers/numeric.rb +13 -0
- data/lib/rr/wildcard_matchers/range.rb +7 -0
- data/lib/rr/wildcard_matchers/regexp.rb +7 -0
- data/lib/rr/wildcard_matchers/satisfy.rb +26 -0
- data/spec/api/any_instance_of/all_instances_of_spec.rb +12 -0
- data/spec/api/any_instance_of/any_instance_of_spec.rb +47 -0
- data/spec/api/any_instance_of/instance_of_spec.rb +12 -0
- data/spec/api/dont_allow/dont_allow_after_stub_spec.rb +14 -0
- data/spec/api/mock/mock_spec.rb +193 -0
- data/spec/api/proxy/proxy_spec.rb +86 -0
- data/spec/api/spy/spy_spec.rb +49 -0
- data/spec/api/strong/strong_spec.rb +87 -0
- data/spec/api/stub/stub_spec.rb +152 -0
- data/spec/core_spec_suite.rb +18 -0
- data/spec/environment_fixture_setup.rb +7 -0
- data/spec/minitest_spec_suite.rb +21 -0
- data/spec/proc_from_block_spec.rb +14 -0
- data/spec/rr/adapters/rr_methods_argument_matcher_spec.rb +67 -0
- data/spec/rr/adapters/rr_methods_creator_spec.rb +137 -0
- data/spec/rr/adapters/rr_methods_space_spec.rb +98 -0
- data/spec/rr/adapters/rr_methods_spec_helper.rb +7 -0
- data/spec/rr/adapters/rr_methods_times_matcher_spec.rb +13 -0
- data/spec/rr/double_definitions/child_double_definition_creator_spec.rb +112 -0
- data/spec/rr/double_definitions/double_definition_create_blank_slate_spec.rb +91 -0
- data/spec/rr/double_definitions/double_definition_create_spec.rb +443 -0
- data/spec/rr/double_injection/double_injection_spec.rb +546 -0
- data/spec/rr/double_injection/double_injection_verify_spec.rb +29 -0
- data/spec/rr/errors/rr_error_spec.rb +67 -0
- data/spec/rr/expectations/any_argument_expectation_spec.rb +47 -0
- data/spec/rr/expectations/anything_argument_equality_expectation_spec.rb +14 -0
- data/spec/rr/expectations/argument_equality_expectation_spec.rb +135 -0
- data/spec/rr/expectations/boolean_argument_equality_expectation_spec.rb +34 -0
- 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 +22 -0
- data/spec/rr/expectations/times_called_expectation/times_called_expectation_at_least_spec.rb +37 -0
- data/spec/rr/expectations/times_called_expectation/times_called_expectation_at_most_spec.rb +43 -0
- data/spec/rr/expectations/times_called_expectation/times_called_expectation_helper.rb +11 -0
- data/spec/rr/expectations/times_called_expectation/times_called_expectation_integer_spec.rb +58 -0
- data/spec/rr/expectations/times_called_expectation/times_called_expectation_proc_spec.rb +35 -0
- data/spec/rr/expectations/times_called_expectation/times_called_expectation_range_spec.rb +39 -0
- data/spec/rr/minitest/minitest_integration_test.rb +59 -0
- data/spec/rr/minitest/test_helper.rb +7 -0
- data/spec/rr/rspec/invocation_matcher_spec.rb +279 -0
- data/spec/rr/rspec/rspec_adapter_spec.rb +63 -0
- data/spec/rr/rspec/rspec_backtrace_tweaking_spec.rb +21 -0
- data/spec/rr/rspec/rspec_backtrace_tweaking_spec_fixture.rb +11 -0
- data/spec/rr/rspec/rspec_usage_spec.rb +86 -0
- data/spec/rr/space/hash_with_object_id_key_spec.rb +88 -0
- data/spec/rr/space/space_spec.rb +596 -0
- data/spec/rr/test_unit/test_helper.rb +7 -0
- data/spec/rr/test_unit/test_unit_backtrace_test.rb +36 -0
- data/spec/rr/test_unit/test_unit_integration_test.rb +59 -0
- 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/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/rspec_spec_suite.rb +16 -0
- data/spec/spec_helper.rb +40 -0
- data/spec/spec_suite.rb +50 -0
- data/spec/spy_verification_spec.rb +129 -0
- data/spec/test_unit_spec_suite.rb +20 -0
- metadata +220 -0
@@ -0,0 +1,25 @@
|
|
1
|
+
module RR
|
2
|
+
module WildcardMatchers
|
3
|
+
class IsA
|
4
|
+
attr_reader :klass
|
5
|
+
|
6
|
+
def initialize(klass)
|
7
|
+
@klass = klass
|
8
|
+
end
|
9
|
+
|
10
|
+
def wildcard_match?(other)
|
11
|
+
self == other || other.is_a?(klass)
|
12
|
+
end
|
13
|
+
|
14
|
+
def inspect
|
15
|
+
"is_a(#{klass})"
|
16
|
+
end
|
17
|
+
|
18
|
+
def ==(other)
|
19
|
+
return false unless other.is_a?(self.class)
|
20
|
+
self.klass == other.klass
|
21
|
+
end
|
22
|
+
alias_method :eql?, :==
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
module RR
|
2
|
+
module WildcardMatchers
|
3
|
+
class Satisfy
|
4
|
+
attr_reader :expectation_proc
|
5
|
+
|
6
|
+
def initialize(expectation_proc)
|
7
|
+
@expectation_proc = expectation_proc
|
8
|
+
end
|
9
|
+
|
10
|
+
def wildcard_match?(other)
|
11
|
+
return true if self == other
|
12
|
+
!!expectation_proc.call(other)
|
13
|
+
end
|
14
|
+
|
15
|
+
def inspect
|
16
|
+
"satisfy {block}"
|
17
|
+
end
|
18
|
+
|
19
|
+
def ==(other)
|
20
|
+
return false unless other.is_a?(self.class)
|
21
|
+
self.expectation_proc == other.expectation_proc
|
22
|
+
end
|
23
|
+
alias_method :eql?, :==
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
require File.expand_path("#{File.dirname(__FILE__)}/../../spec_helper")
|
2
|
+
|
3
|
+
describe "all_instances_of" do
|
4
|
+
it "applies to instances instantiated before the Double expection was created" do
|
5
|
+
subject_class = Class.new
|
6
|
+
subject = subject_class.new
|
7
|
+
all_instances_of(subject_class) do |o|
|
8
|
+
o.to_s {"Subject is stubbed"}
|
9
|
+
end
|
10
|
+
subject.to_s.should == "Subject is stubbed"
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
require File.expand_path("#{File.dirname(__FILE__)}/../../spec_helper")
|
2
|
+
|
3
|
+
describe "any_instance_of" do
|
4
|
+
context "when passed a block" do
|
5
|
+
it "applies to instances instantiated before the Double expection was created" do
|
6
|
+
subject_class = Class.new
|
7
|
+
subject = subject_class.new
|
8
|
+
class_called = false
|
9
|
+
any_instance_of(subject_class) do |o|
|
10
|
+
stub(o).to_s {"Subject is stubbed"}
|
11
|
+
stub.proxy(o).class {|klass| class_called = true; klass}
|
12
|
+
stub(o).foobar {:baz}
|
13
|
+
end
|
14
|
+
|
15
|
+
subject.to_s.should == "Subject is stubbed"
|
16
|
+
subject.class.should == subject_class
|
17
|
+
class_called.should == true
|
18
|
+
subject.foobar.should == :baz
|
19
|
+
|
20
|
+
RR.reset
|
21
|
+
|
22
|
+
subject.to_s.should_not == "Subject is stubbed"
|
23
|
+
class_called = false
|
24
|
+
subject.class.should == subject_class
|
25
|
+
class_called.should == false
|
26
|
+
subject.should_not respond_to(:baz)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
context "when passed a Hash" do
|
31
|
+
it "stubs methods (key) with the value on instances instantiated before the Double expection was created" do
|
32
|
+
subject_class = Class.new
|
33
|
+
subject = subject_class.new
|
34
|
+
subject.should_not respond_to(:baz)
|
35
|
+
|
36
|
+
any_instance_of(subject_class, :to_s => "Subject is stubbed", :foobar => lambda {:baz})
|
37
|
+
|
38
|
+
subject.to_s.should == "Subject is stubbed"
|
39
|
+
subject.foobar.should == :baz
|
40
|
+
|
41
|
+
RR.reset
|
42
|
+
|
43
|
+
subject.to_s.should_not == "Subject is stubbed"
|
44
|
+
subject.should_not respond_to(:baz)
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
require File.expand_path("#{File.dirname(__FILE__)}/../../spec_helper")
|
2
|
+
|
3
|
+
describe "instance_of" do
|
4
|
+
it "applies to instances instantiated before the Double expection was created" do
|
5
|
+
subject_class = Class.new
|
6
|
+
subject = subject_class.new
|
7
|
+
instance_of(subject_class) do |o|
|
8
|
+
o.to_s {"Subject is stubbed"}
|
9
|
+
end
|
10
|
+
subject.to_s.should == "Subject is stubbed"
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
require File.expand_path("#{File.dirname(__FILE__)}/../../spec_helper")
|
2
|
+
|
3
|
+
describe "dont_allow called after stub" do
|
4
|
+
context "when the subject method is called" do
|
5
|
+
it "raises a TimesCalledError" do
|
6
|
+
subject = Object.new
|
7
|
+
stub(subject).foobar
|
8
|
+
dont_allow(subject).foobar
|
9
|
+
lambda do
|
10
|
+
subject.foobar
|
11
|
+
end.should raise_error(RR::Errors::TimesCalledError)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,193 @@
|
|
1
|
+
require File.expand_path("#{File.dirname(__FILE__)}/../../spec_helper")
|
2
|
+
|
3
|
+
describe "mock" do
|
4
|
+
attr_reader :subject
|
5
|
+
before(:each) do
|
6
|
+
@subject = Object.new
|
7
|
+
extend RR::Adapters::RRMethods
|
8
|
+
end
|
9
|
+
|
10
|
+
after(:each) do
|
11
|
+
RR.reset
|
12
|
+
end
|
13
|
+
|
14
|
+
it "mocks via inline call" do
|
15
|
+
mock(subject).to_s {"a value"}
|
16
|
+
subject.to_s.should == "a value"
|
17
|
+
lambda {subject.to_s}.should raise_error(RR::Errors::TimesCalledError)
|
18
|
+
end
|
19
|
+
|
20
|
+
describe ".once.ordered" do
|
21
|
+
it "returns the values in the ordered called" do
|
22
|
+
mock(subject).to_s {"value 1"}.ordered
|
23
|
+
mock(subject).to_s {"value 2"}.twice
|
24
|
+
subject.to_s.should == "value 1"
|
25
|
+
subject.to_s.should == "value 2"
|
26
|
+
subject.to_s.should == "value 2"
|
27
|
+
lambda {subject.to_s}.should raise_error(RR::Errors::TimesCalledError)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
context "when the subject is a proxy for the object with the defined method" do
|
32
|
+
it "stubs the method on the proxy object" do
|
33
|
+
proxy_target = Class.new {def foobar; :original_foobar; end}.new
|
34
|
+
proxy = Class.new do
|
35
|
+
def initialize(target)
|
36
|
+
@target = target
|
37
|
+
end
|
38
|
+
|
39
|
+
instance_methods.each do |m|
|
40
|
+
unless m =~ /^_/ || m.to_s == 'object_id' || m.to_s == 'method_missing'
|
41
|
+
alias_method "__blank_slated_#{m}", m
|
42
|
+
undef_method m
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
def method_missing(method_name, *args, &block)
|
47
|
+
@target.send(method_name, *args, &block)
|
48
|
+
end
|
49
|
+
end.new(proxy_target)
|
50
|
+
proxy.methods.should =~ proxy_target.methods
|
51
|
+
|
52
|
+
mock(proxy).foobar {:new_foobar}
|
53
|
+
proxy.foobar.should == :new_foobar
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
it 'allows terse chaining' do
|
58
|
+
mock(subject).first(1) {mock(Object.new).second(2) {mock(Object.new).third(3) {4}}}
|
59
|
+
subject.first(1).second(2).third(3).should == 4
|
60
|
+
|
61
|
+
mock(subject).first(1) {mock!.second(2) {mock!.third(3) {4}}}
|
62
|
+
subject.first(1).second(2).third(3).should == 4
|
63
|
+
|
64
|
+
mock(subject).first(1) {mock!.second(2).mock!.third(3) {4}}
|
65
|
+
subject.first(1).second(2).third(3).should == 4
|
66
|
+
|
67
|
+
mock(subject).first(1) {mock!.second(2).mock! {third(3) {4}}}
|
68
|
+
subject.first(1).second(2).third(3).should == 4
|
69
|
+
|
70
|
+
mock(subject).first(1).mock!.second(2).mock!.third(3) {4}
|
71
|
+
subject.first(1).second(2).third(3).should == 4
|
72
|
+
end
|
73
|
+
|
74
|
+
it 'allows chaining with proxy' do
|
75
|
+
find_return_value = Object.new
|
76
|
+
def find_return_value.child
|
77
|
+
:the_child
|
78
|
+
end
|
79
|
+
(class << subject; self; end).class_eval do
|
80
|
+
define_method(:find) do |id|
|
81
|
+
id == '1' ? find_return_value : raise(ArgumentError)
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
mock.proxy(subject).find('1').mock.proxy!.child
|
86
|
+
subject.find('1').child.should == :the_child
|
87
|
+
end
|
88
|
+
|
89
|
+
it 'allows branched chaining' do
|
90
|
+
mock(subject).first do
|
91
|
+
mock! do |expect|
|
92
|
+
expect.branch1 {mock!.branch11 {11}}
|
93
|
+
expect.branch2 {mock!.branch22 {22}}
|
94
|
+
end
|
95
|
+
end
|
96
|
+
o = subject.first
|
97
|
+
o.branch1.branch11.should == 11
|
98
|
+
o.branch2.branch22.should == 22
|
99
|
+
end
|
100
|
+
|
101
|
+
it 'allows chained ordering' do
|
102
|
+
mock(subject).to_s {"value 1"}.then.to_s {"value 2"}.twice.then.to_s {"value 3"}.once
|
103
|
+
subject.to_s.should == "value 1"
|
104
|
+
subject.to_s.should == "value 2"
|
105
|
+
subject.to_s.should == "value 2"
|
106
|
+
subject.to_s.should == "value 3"
|
107
|
+
lambda {subject.to_s}.should raise_error(RR::Errors::TimesCalledError)
|
108
|
+
end
|
109
|
+
|
110
|
+
it "mocks via block with argument" do
|
111
|
+
mock subject do |c|
|
112
|
+
c.to_s {"a value"}
|
113
|
+
c.to_sym {:crazy}
|
114
|
+
end
|
115
|
+
subject.to_s.should == "a value"
|
116
|
+
subject.to_sym.should == :crazy
|
117
|
+
end
|
118
|
+
|
119
|
+
it "mocks via block without argument" do
|
120
|
+
mock subject do
|
121
|
+
to_s {"a value"}
|
122
|
+
to_sym {:crazy}
|
123
|
+
end
|
124
|
+
subject.to_s.should == "a value"
|
125
|
+
subject.to_sym.should == :crazy
|
126
|
+
end
|
127
|
+
|
128
|
+
it "has wildcard matchers" do
|
129
|
+
mock(subject).foobar(
|
130
|
+
is_a(String),
|
131
|
+
anything,
|
132
|
+
numeric,
|
133
|
+
boolean,
|
134
|
+
duck_type(:to_s),
|
135
|
+
/abc/
|
136
|
+
) {"value 1"}.twice
|
137
|
+
subject.foobar(
|
138
|
+
'hello',
|
139
|
+
Object.new,
|
140
|
+
99,
|
141
|
+
false,
|
142
|
+
"My String",
|
143
|
+
"Tabcola"
|
144
|
+
).should == "value 1"
|
145
|
+
lambda do
|
146
|
+
subject.foobar(:failure)
|
147
|
+
end.should raise_error( RR::Errors::DoubleNotFoundError )
|
148
|
+
end
|
149
|
+
|
150
|
+
it "mocks methods without letters" do
|
151
|
+
mock(subject, :==).with(55)
|
152
|
+
|
153
|
+
subject == 55
|
154
|
+
lambda do
|
155
|
+
subject == 99
|
156
|
+
end.should raise_error(RR::Errors::DoubleNotFoundError)
|
157
|
+
end
|
158
|
+
|
159
|
+
it "expects a method call to a mock via another mock's block yield only once" do
|
160
|
+
cage = Object.new
|
161
|
+
cat = Object.new
|
162
|
+
mock(cat).miau # should be expected to be called only once
|
163
|
+
mock(cage).find_cat.yields(cat)
|
164
|
+
mock(cage).cats
|
165
|
+
cage.find_cat { |c| c.miau }
|
166
|
+
cage.cats
|
167
|
+
end
|
168
|
+
|
169
|
+
describe "on class method" do
|
170
|
+
class SampleClass1
|
171
|
+
def self.hello; "hello!"; end
|
172
|
+
end
|
173
|
+
|
174
|
+
class SampleClass2 < SampleClass1; end
|
175
|
+
|
176
|
+
it "can mock" do
|
177
|
+
mock(SampleClass1).hello { "hola!" }
|
178
|
+
|
179
|
+
SampleClass1.hello.should == "hola!"
|
180
|
+
end
|
181
|
+
|
182
|
+
it "does not override subclasses" do
|
183
|
+
mock(SampleClass1).hello { "hi!" }
|
184
|
+
|
185
|
+
SampleClass2.hello.should == "hello!"
|
186
|
+
end
|
187
|
+
|
188
|
+
it "should not get affected from a previous example" do
|
189
|
+
SampleClass2.hello.should == "hello!"
|
190
|
+
end
|
191
|
+
|
192
|
+
end
|
193
|
+
end
|
@@ -0,0 +1,86 @@
|
|
1
|
+
require File.expand_path("#{File.dirname(__FILE__)}/../../spec_helper")
|
2
|
+
|
3
|
+
describe "proxy" do
|
4
|
+
attr_reader :subject
|
5
|
+
before(:each) do
|
6
|
+
@subject = Object.new
|
7
|
+
extend RR::Adapters::RRMethods
|
8
|
+
end
|
9
|
+
|
10
|
+
after(:each) do
|
11
|
+
RR.reset
|
12
|
+
end
|
13
|
+
|
14
|
+
it "proxies via inline call" do
|
15
|
+
expected_to_s_value = subject.to_s
|
16
|
+
mock.proxy(subject).to_s
|
17
|
+
subject.to_s.should == expected_to_s_value
|
18
|
+
lambda {subject.to_s}.should raise_error
|
19
|
+
end
|
20
|
+
|
21
|
+
it "proxy allows ordering" do
|
22
|
+
def subject.to_s(arg)
|
23
|
+
"Original to_s with arg #{arg}"
|
24
|
+
end
|
25
|
+
mock.proxy(subject).to_s(:foo).ordered
|
26
|
+
mock.proxy(subject).to_s(:bar).twice.ordered
|
27
|
+
|
28
|
+
subject.to_s(:foo).should == "Original to_s with arg foo"
|
29
|
+
subject.to_s(:bar).should == "Original to_s with arg bar"
|
30
|
+
subject.to_s(:bar).should == "Original to_s with arg bar"
|
31
|
+
lambda {subject.to_s(:bar)}.should raise_error(RR::Errors::TimesCalledError)
|
32
|
+
end
|
33
|
+
|
34
|
+
it "proxy allows ordering" do
|
35
|
+
def subject.to_s(arg)
|
36
|
+
"Original to_s with arg #{arg}"
|
37
|
+
end
|
38
|
+
mock.proxy(subject).to_s(:foo).ordered
|
39
|
+
mock.proxy(subject).to_s(:bar).twice.ordered
|
40
|
+
|
41
|
+
subject.to_s(:foo).should == "Original to_s with arg foo"
|
42
|
+
subject.to_s(:bar).should == "Original to_s with arg bar"
|
43
|
+
subject.to_s(:bar).should == "Original to_s with arg bar"
|
44
|
+
lambda {subject.to_s(:bar)}.should raise_error(RR::Errors::TimesCalledError)
|
45
|
+
end
|
46
|
+
|
47
|
+
it "proxies via block with argument" do
|
48
|
+
def subject.foobar_1(*args)
|
49
|
+
:original_value_1
|
50
|
+
end
|
51
|
+
|
52
|
+
def subject.foobar_2
|
53
|
+
:original_value_2
|
54
|
+
end
|
55
|
+
|
56
|
+
mock.proxy subject do |c|
|
57
|
+
c.foobar_1(1)
|
58
|
+
c.foobar_2
|
59
|
+
end
|
60
|
+
subject.foobar_1(1).should == :original_value_1
|
61
|
+
lambda {subject.foobar_1(:blah)}.should raise_error
|
62
|
+
|
63
|
+
subject.foobar_2.should == :original_value_2
|
64
|
+
lambda {subject.foobar_2(:blah)}.should raise_error
|
65
|
+
end
|
66
|
+
|
67
|
+
it "proxies via block without argument" do
|
68
|
+
def subject.foobar_1(*args)
|
69
|
+
:original_value_1
|
70
|
+
end
|
71
|
+
|
72
|
+
def subject.foobar_2
|
73
|
+
:original_value_2
|
74
|
+
end
|
75
|
+
|
76
|
+
mock.proxy subject do
|
77
|
+
foobar_1(1)
|
78
|
+
foobar_2
|
79
|
+
end
|
80
|
+
subject.foobar_1(1).should == :original_value_1
|
81
|
+
lambda {subject.foobar_1(:blah)}.should raise_error
|
82
|
+
|
83
|
+
subject.foobar_2.should == :original_value_2
|
84
|
+
lambda {subject.foobar_2(:blah)}.should raise_error
|
85
|
+
end
|
86
|
+
end
|