rr 1.0.5 → 1.1.0.rc1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +6 -14
- data/CHANGES.md +24 -0
- data/LICENSE +2 -2
- data/README.md +124 -741
- data/VERSION +1 -1
- data/lib/rr.rb +2 -103
- data/lib/rr/adapters/minitest.rb +21 -13
- data/lib/rr/adapters/minitest_active_support.rb +34 -0
- data/lib/rr/adapters/none.rb +17 -0
- data/lib/rr/adapters/{rspec.rb → rspec/invocation_matcher.rb} +2 -27
- data/lib/rr/adapters/rspec_1.rb +42 -0
- data/lib/rr/adapters/rspec_2.rb +24 -0
- data/lib/rr/adapters/test_unit_1.rb +54 -0
- data/lib/rr/adapters/test_unit_2.rb +13 -0
- data/lib/rr/adapters/test_unit_2_active_support.rb +35 -0
- data/lib/rr/autohook.rb +43 -0
- data/lib/rr/core_ext/array.rb +12 -0
- data/lib/rr/core_ext/enumerable.rb +16 -0
- data/lib/rr/core_ext/hash.rb +20 -0
- data/lib/rr/core_ext/range.rb +8 -0
- data/lib/rr/core_ext/regexp.rb +8 -0
- data/lib/rr/double.rb +4 -4
- data/lib/rr/double_definitions/double_definition.rb +9 -3
- data/lib/rr/errors.rb +21 -0
- data/lib/rr/expectations/argument_equality_expectation.rb +10 -7
- data/lib/rr/expectations/times_called_expectation.rb +2 -8
- data/lib/rr/injections/double_injection.rb +1 -1
- data/lib/rr/method_dispatches/base_method_dispatch.rb +1 -1
- data/lib/rr/recorded_calls.rb +12 -12
- data/lib/rr/space.rb +5 -3
- data/lib/rr/times_called_matchers/never_matcher.rb +2 -2
- data/lib/rr/wildcard_matchers/anything.rb +2 -2
- data/lib/rr/wildcard_matchers/boolean.rb +3 -7
- data/lib/rr/wildcard_matchers/duck_type.rb +11 -15
- data/lib/rr/wildcard_matchers/hash_including.rb +14 -13
- data/lib/rr/wildcard_matchers/is_a.rb +6 -7
- data/lib/rr/wildcard_matchers/satisfy.rb +8 -8
- data/lib/rr/without_autohook.rb +112 -0
- data/rr.gemspec +28 -0
- data/spec/global_helper.rb +12 -0
- data/spec/suite.rb +93 -0
- data/spec/suites/common/adapter_tests.rb +37 -0
- data/spec/suites/common/rails_integration_test.rb +175 -0
- data/spec/suites/common/test_unit_tests.rb +25 -0
- data/spec/suites/minitest/integration/minitest_test.rb +13 -0
- data/spec/suites/minitest/test_helper.rb +3 -0
- data/spec/suites/rspec_1/integration/rspec_1_spec.rb +20 -0
- data/spec/suites/rspec_1/integration/test_unit_1_rails_spec.rb +19 -0
- data/spec/suites/rspec_1/integration/test_unit_2_rails_spec.rb +18 -0
- data/spec/suites/rspec_1/spec_helper.rb +24 -0
- data/spec/suites/rspec_2/functional/any_instance_of_spec.rb +47 -0
- data/spec/suites/rspec_2/functional/dont_allow_spec.rb +12 -0
- data/spec/suites/rspec_2/functional/dsl_spec.rb +13 -0
- data/spec/suites/rspec_2/functional/instance_of_spec.rb +14 -0
- data/spec/suites/rspec_2/functional/mock_spec.rb +241 -0
- data/spec/suites/rspec_2/functional/proxy_spec.rb +136 -0
- data/spec/suites/rspec_2/functional/spy_spec.rb +41 -0
- data/spec/suites/rspec_2/functional/strong_spec.rb +79 -0
- data/spec/suites/rspec_2/functional/stub_spec.rb +190 -0
- data/spec/suites/rspec_2/functional/wildcard_matchers_spec.rb +128 -0
- data/spec/suites/rspec_2/integration/minitest_rails_spec.rb +15 -0
- data/spec/suites/rspec_2/integration/rspec_2_spec.rb +20 -0
- data/spec/suites/rspec_2/integration/test_unit_rails_spec.rb +14 -0
- data/spec/suites/rspec_2/spec_helper.rb +27 -0
- data/spec/suites/rspec_2/support/matchers/wildcard_matcher_matchers.rb +32 -0
- data/spec/suites/rspec_2/support/shared_examples/space.rb +13 -0
- data/spec/suites/rspec_2/support/shared_examples/times_called_expectation.rb +9 -0
- data/spec/suites/rspec_2/unit/adapters/rr_methods/double_creators_spec.rb +135 -0
- data/spec/suites/rspec_2/unit/adapters/rr_methods/space_spec.rb +101 -0
- data/spec/suites/rspec_2/unit/adapters/rr_methods/wildcard_matchers_spec.rb +69 -0
- data/spec/suites/rspec_2/unit/adapters/rspec/invocation_matcher_spec.rb +297 -0
- data/spec/suites/rspec_2/unit/adapters/rspec_spec.rb +85 -0
- data/spec/suites/rspec_2/unit/core_ext/array_spec.rb +39 -0
- data/spec/suites/rspec_2/unit/core_ext/enumerable_spec.rb +81 -0
- data/spec/suites/rspec_2/unit/core_ext/hash_spec.rb +55 -0
- data/spec/suites/rspec_2/unit/core_ext/range_spec.rb +41 -0
- data/spec/suites/rspec_2/unit/core_ext/regexp_spec.rb +41 -0
- data/spec/suites/rspec_2/unit/double_definitions/child_double_definition_create_spec.rb +114 -0
- data/spec/suites/rspec_2/unit/double_definitions/double_definition_create_blank_slate_spec.rb +93 -0
- data/spec/suites/rspec_2/unit/double_definitions/double_definition_create_spec.rb +446 -0
- data/spec/suites/rspec_2/unit/errors/rr_error_spec.rb +67 -0
- data/spec/suites/rspec_2/unit/expectations/any_argument_expectation_spec.rb +48 -0
- data/spec/suites/rspec_2/unit/expectations/anything_argument_equality_expectation_spec.rb +14 -0
- data/spec/suites/rspec_2/unit/expectations/argument_equality_expectation_spec.rb +135 -0
- data/spec/suites/rspec_2/unit/expectations/boolean_argument_equality_expectation_spec.rb +30 -0
- data/spec/suites/rspec_2/unit/expectations/hash_including_argument_equality_expectation_spec.rb +82 -0
- data/spec/suites/rspec_2/unit/expectations/satisfy_argument_equality_expectation_spec.rb +61 -0
- data/spec/suites/rspec_2/unit/expectations/times_called_expectation/any_times_matcher_spec.rb +22 -0
- data/spec/suites/rspec_2/unit/expectations/times_called_expectation/at_least_matcher_spec.rb +37 -0
- data/spec/suites/rspec_2/unit/expectations/times_called_expectation/at_most_matcher_spec.rb +43 -0
- data/spec/suites/rspec_2/unit/expectations/times_called_expectation/integer_matcher_spec.rb +58 -0
- data/spec/suites/rspec_2/unit/expectations/times_called_expectation/proc_matcher_spec.rb +35 -0
- data/spec/suites/rspec_2/unit/expectations/times_called_expectation/range_matcher_spec.rb +39 -0
- data/spec/suites/rspec_2/unit/hash_with_object_id_key_spec.rb +88 -0
- data/spec/suites/rspec_2/unit/injections/double_injection/double_injection_spec.rb +545 -0
- data/spec/suites/rspec_2/unit/injections/double_injection/double_injection_verify_spec.rb +32 -0
- data/spec/suites/rspec_2/unit/proc_from_block_spec.rb +14 -0
- data/spec/suites/rspec_2/unit/rr_spec.rb +28 -0
- data/spec/suites/rspec_2/unit/space_spec.rb +595 -0
- data/spec/suites/rspec_2/unit/spy_verification_spec.rb +133 -0
- data/spec/suites/rspec_2/unit/times_called_matchers/any_times_matcher_spec.rb +46 -0
- data/spec/suites/rspec_2/unit/times_called_matchers/at_least_matcher_spec.rb +54 -0
- data/spec/suites/rspec_2/unit/times_called_matchers/at_most_matcher_spec.rb +69 -0
- data/spec/suites/rspec_2/unit/times_called_matchers/integer_matcher_spec.rb +69 -0
- data/spec/suites/rspec_2/unit/times_called_matchers/proc_matcher_spec.rb +54 -0
- data/spec/suites/rspec_2/unit/times_called_matchers/range_matcher_spec.rb +75 -0
- data/spec/suites/rspec_2/unit/times_called_matchers/times_called_matcher_spec.rb +117 -0
- data/spec/suites/rspec_2/unit/wildcard_matchers/anything_spec.rb +33 -0
- data/spec/suites/rspec_2/unit/wildcard_matchers/boolean_spec.rb +45 -0
- data/spec/suites/rspec_2/unit/wildcard_matchers/duck_type_spec.rb +64 -0
- data/spec/suites/rspec_2/unit/wildcard_matchers/hash_including_spec.rb +64 -0
- data/spec/suites/rspec_2/unit/wildcard_matchers/is_a_spec.rb +55 -0
- data/spec/suites/rspec_2/unit/wildcard_matchers/numeric_spec.rb +46 -0
- data/spec/suites/rspec_2/unit/wildcard_matchers/satisfy_spec.rb +57 -0
- data/spec/suites/test_unit_1/integration/test_unit_1_test.rb +6 -0
- data/spec/suites/test_unit_1/test_helper.rb +7 -0
- data/spec/suites/test_unit_2/integration/test_unit_2_test.rb +6 -0
- data/spec/suites/test_unit_2/test_helper.rb +3 -0
- metadata +183 -19
- data/Gemfile +0 -9
- data/Rakefile +0 -34
- data/lib/rr/adapters/rspec2.rb +0 -30
- data/lib/rr/adapters/test_unit.rb +0 -33
- data/lib/rr/errors/argument_equality_error.rb +0 -6
- data/lib/rr/wildcard_matchers/range.rb +0 -7
- data/lib/rr/wildcard_matchers/regexp.rb +0 -7
- data/spec/runner.rb +0 -41
@@ -0,0 +1,136 @@
|
|
1
|
+
require File.expand_path('../../spec_helper', __FILE__)
|
2
|
+
|
3
|
+
describe '#proxy' do
|
4
|
+
subject { Object.new }
|
5
|
+
|
6
|
+
it "proxies via inline call" do
|
7
|
+
expected_to_s_value = subject.to_s
|
8
|
+
mock.proxy(subject).to_s
|
9
|
+
expect(subject.to_s).to eq expected_to_s_value
|
10
|
+
expect { subject.to_s }.to raise_error
|
11
|
+
RR.reset
|
12
|
+
end
|
13
|
+
|
14
|
+
it "proxy allows ordering" do
|
15
|
+
def subject.to_s(arg)
|
16
|
+
"Original to_s with arg #{arg}"
|
17
|
+
end
|
18
|
+
mock.proxy(subject).to_s(:foo).ordered
|
19
|
+
mock.proxy(subject).to_s(:bar).twice.ordered
|
20
|
+
|
21
|
+
expect(subject.to_s(:foo)).to eq "Original to_s with arg foo"
|
22
|
+
expect(subject.to_s(:bar)).to eq "Original to_s with arg bar"
|
23
|
+
expect(subject.to_s(:bar)).to eq "Original to_s with arg bar"
|
24
|
+
expect { subject.to_s(:bar) }.to raise_error(RR::Errors::TimesCalledError)
|
25
|
+
|
26
|
+
RR.reset
|
27
|
+
end
|
28
|
+
|
29
|
+
it "proxy allows ordering" do
|
30
|
+
def subject.to_s(arg)
|
31
|
+
"Original to_s with arg #{arg}"
|
32
|
+
end
|
33
|
+
mock.proxy(subject).to_s(:foo).ordered
|
34
|
+
mock.proxy(subject).to_s(:bar).twice.ordered
|
35
|
+
|
36
|
+
expect(subject.to_s(:foo)).to eq "Original to_s with arg foo"
|
37
|
+
expect(subject.to_s(:bar)).to eq "Original to_s with arg bar"
|
38
|
+
expect(subject.to_s(:bar)).to eq "Original to_s with arg bar"
|
39
|
+
expect { subject.to_s(:bar) }.to raise_error(RR::Errors::TimesCalledError)
|
40
|
+
|
41
|
+
RR.reset
|
42
|
+
end
|
43
|
+
|
44
|
+
it "proxies via block with argument" do
|
45
|
+
def subject.foobar_1(*args)
|
46
|
+
:original_value_1
|
47
|
+
end
|
48
|
+
|
49
|
+
def subject.foobar_2
|
50
|
+
:original_value_2
|
51
|
+
end
|
52
|
+
|
53
|
+
mock.proxy subject do |c|
|
54
|
+
c.foobar_1(1)
|
55
|
+
c.foobar_2
|
56
|
+
end
|
57
|
+
expect(subject.foobar_1(1)).to eq :original_value_1
|
58
|
+
expect { subject.foobar_1(:blah) }.to raise_error
|
59
|
+
|
60
|
+
expect(subject.foobar_2).to eq :original_value_2
|
61
|
+
expect { subject.foobar_2(:blah) }.to raise_error
|
62
|
+
|
63
|
+
RR.reset
|
64
|
+
end
|
65
|
+
|
66
|
+
it "proxies via block without argument" do
|
67
|
+
def subject.foobar_1(*args)
|
68
|
+
:original_value_1
|
69
|
+
end
|
70
|
+
|
71
|
+
def subject.foobar_2
|
72
|
+
:original_value_2
|
73
|
+
end
|
74
|
+
|
75
|
+
mock.proxy subject do
|
76
|
+
foobar_1(1)
|
77
|
+
foobar_2
|
78
|
+
end
|
79
|
+
expect(subject.foobar_1(1)).to eq :original_value_1
|
80
|
+
expect { subject.foobar_1(:blah) }.to raise_error
|
81
|
+
|
82
|
+
expect(subject.foobar_2).to eq :original_value_2
|
83
|
+
expect { subject.foobar_2(:blah) }.to raise_error
|
84
|
+
|
85
|
+
RR.reset
|
86
|
+
end
|
87
|
+
|
88
|
+
# bug #44
|
89
|
+
describe 'when wrapped in an array that is then flattened' do
|
90
|
+
subject {
|
91
|
+
Object.new.tap do |o|
|
92
|
+
def o.foo; end
|
93
|
+
end
|
94
|
+
}
|
95
|
+
|
96
|
+
context 'when the method being mocked is not defined' do
|
97
|
+
it "does not raise an error" do
|
98
|
+
mock.proxy(subject).foo
|
99
|
+
subject.foo
|
100
|
+
expect([subject].flatten).to eq [subject]
|
101
|
+
end
|
102
|
+
|
103
|
+
it "honors a #to_ary that already exists" do
|
104
|
+
subject.instance_eval do
|
105
|
+
def to_ary; []; end
|
106
|
+
end
|
107
|
+
mock.proxy(subject).foo
|
108
|
+
subject.foo
|
109
|
+
expect([subject].flatten).to eq []
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
context 'when the method being mocked is defined' do
|
114
|
+
before do
|
115
|
+
subject.instance_eval do
|
116
|
+
def foo; end
|
117
|
+
end
|
118
|
+
end
|
119
|
+
|
120
|
+
it "does not raise an error" do
|
121
|
+
mock.proxy(subject).foo
|
122
|
+
subject.foo
|
123
|
+
expect([subject].flatten).to eq [subject]
|
124
|
+
end
|
125
|
+
|
126
|
+
it "honors a #to_ary that already exists" do
|
127
|
+
eigen(subject).class_eval do
|
128
|
+
def to_ary; []; end
|
129
|
+
end
|
130
|
+
mock.proxy(subject).foo
|
131
|
+
subject.foo
|
132
|
+
expect([subject].flatten).to eq []
|
133
|
+
end
|
134
|
+
end
|
135
|
+
end
|
136
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
require File.expand_path('../../spec_helper', __FILE__)
|
2
|
+
|
3
|
+
describe '#spy' do
|
4
|
+
subject { Object.new }
|
5
|
+
|
6
|
+
it "should record all method invocations" do
|
7
|
+
subject = Object.new
|
8
|
+
|
9
|
+
def subject.something
|
10
|
+
end
|
11
|
+
|
12
|
+
def subject.something_else
|
13
|
+
end
|
14
|
+
|
15
|
+
spy(subject)
|
16
|
+
|
17
|
+
subject.something
|
18
|
+
subject.something_else
|
19
|
+
subject.to_s
|
20
|
+
|
21
|
+
received(subject).something.call
|
22
|
+
received(subject).something_else.call
|
23
|
+
received(subject).to_s.call
|
24
|
+
end
|
25
|
+
|
26
|
+
describe "RR recorded_calls" do
|
27
|
+
it "should verify method calls after the fact" do
|
28
|
+
stub(subject).pig_rabbit
|
29
|
+
subject.pig_rabbit("bacon", "bunny meat")
|
30
|
+
#expect(subject).to have_received.pig_rabitt("bacon", "bunny meat")
|
31
|
+
received(subject).pig_rabbit("bacon", "bunny meat").call
|
32
|
+
end
|
33
|
+
|
34
|
+
it "should verify method calls after the fact" do
|
35
|
+
stub(subject).pig_rabbit
|
36
|
+
expect {
|
37
|
+
received(subject).pig_rabbit("bacon", "bunny meat").call
|
38
|
+
}.to raise_error(RR::Errors::SpyVerificationErrors::SpyVerificationError)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,79 @@
|
|
1
|
+
require File.expand_path('../../spec_helper', __FILE__)
|
2
|
+
|
3
|
+
class StrongSpecFixture
|
4
|
+
def method_with_no_arguments
|
5
|
+
end
|
6
|
+
|
7
|
+
def method_with_one_argument(string)
|
8
|
+
end
|
9
|
+
|
10
|
+
def method_with_two_arguments(string, integer)
|
11
|
+
end
|
12
|
+
|
13
|
+
def method_with_three_arguments_including_varargs(string, integer, *args)
|
14
|
+
end
|
15
|
+
|
16
|
+
def method_with_varargs(*args)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
describe '#strong' do
|
21
|
+
subject { Object.new }
|
22
|
+
|
23
|
+
context "when the method does not exist" do
|
24
|
+
it "raises an exception" do
|
25
|
+
expect {
|
26
|
+
strong.stub(StrongSpecFixture.new).something
|
27
|
+
}.to raise_error(RR::Errors::SubjectDoesNotImplementMethodError)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
context "when the method exists with no arguments" do
|
32
|
+
it "does not raise an exception" do
|
33
|
+
strong.stub(StrongSpecFixture.new).method_with_no_arguments
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
context "when the method has a different arity" do
|
38
|
+
it "raises an exception" do
|
39
|
+
expect {
|
40
|
+
strong.stub(StrongSpecFixture.new).method_with_one_argument
|
41
|
+
}.to raise_error(RR::Errors::SubjectHasDifferentArityError)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
context "when the method has accepts a variable number of arguments" do
|
46
|
+
it "does not raise an exception" do
|
47
|
+
strong.stub(StrongSpecFixture.new).method_with_varargs
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
context "when the method does not provide the required parameters before varargs" do
|
52
|
+
it "raises an exception" do
|
53
|
+
expect {
|
54
|
+
strong.stub(StrongSpecFixture.new).method_with_three_arguments_including_varargs
|
55
|
+
}.to raise_error(RR::Errors::SubjectHasDifferentArityError)
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
context "when the minimum number of parameters are provided" do
|
60
|
+
it "does not raise an exception" do
|
61
|
+
strong.stub(StrongSpecFixture.new).method_with_three_arguments_including_varargs("one", 2)
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
context "when using instance_of and the method does not exist" do
|
66
|
+
it "raises an exception" do
|
67
|
+
expect {
|
68
|
+
strong.stub.instance_of(StrongSpecFixture).something
|
69
|
+
StrongSpecFixture.new
|
70
|
+
}.to raise_error(RR::Errors::SubjectDoesNotImplementMethodError)
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
context "when using instance_of and the method does exist" do
|
75
|
+
it "does not raise an exception" do
|
76
|
+
strong.stub.instance_of(StrongSpecFixture).method_with_no_arguments
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
@@ -0,0 +1,190 @@
|
|
1
|
+
require File.expand_path('../../spec_helper', __FILE__)
|
2
|
+
|
3
|
+
class StubSpecFixture
|
4
|
+
attr_reader :initialize_arguments
|
5
|
+
|
6
|
+
def initialize(*args)
|
7
|
+
@initialize_arguments = args
|
8
|
+
yield if block_given?
|
9
|
+
method_run_in_initialize
|
10
|
+
end
|
11
|
+
|
12
|
+
def method_run_in_initialize
|
13
|
+
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
describe '#stub' do
|
18
|
+
subject { Object.new }
|
19
|
+
|
20
|
+
it "creates a stub DoubleInjection Double" do
|
21
|
+
stub(subject).foobar {:baz}
|
22
|
+
expect(subject.foobar("any", "thing")).to eq :baz
|
23
|
+
end
|
24
|
+
|
25
|
+
it "stubs via inline call" do
|
26
|
+
stub(subject).to_s {"a value"}
|
27
|
+
expect(subject.to_s).to eq "a value"
|
28
|
+
end
|
29
|
+
|
30
|
+
describe ".once.ordered" do
|
31
|
+
it "returns the values in the ordered called" do
|
32
|
+
stub(subject).to_s {"value 1"}.once.ordered
|
33
|
+
stub(subject).to_s {"value 2"}.once.ordered
|
34
|
+
|
35
|
+
expect(subject.to_s).to eq "value 1"
|
36
|
+
expect(subject.to_s).to eq "value 2"
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
context "when the subject is a proxy for the object with the defined method" do
|
41
|
+
it "stubs the method on the proxy object" do
|
42
|
+
proxy_target = Class.new {def foobar; :original_foobar; end}.new
|
43
|
+
proxy = Class.new do
|
44
|
+
def initialize(target)
|
45
|
+
@target = target
|
46
|
+
end
|
47
|
+
|
48
|
+
instance_methods.each do |m|
|
49
|
+
unless m =~ /^_/ || m.to_s == 'object_id' || m.to_s == 'method_missing'
|
50
|
+
alias_method "__blank_slated_#{m}", m
|
51
|
+
undef_method m
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
def method_missing(method_name, *args, &block)
|
56
|
+
@target.send(method_name, *args, &block)
|
57
|
+
end
|
58
|
+
end.new(proxy_target)
|
59
|
+
expect(proxy.methods).to match_array(proxy_target.methods)
|
60
|
+
|
61
|
+
stub(proxy).foobar {:new_foobar}
|
62
|
+
expect(proxy.foobar).to eq :new_foobar
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
it "stubs via block with argument" do
|
67
|
+
stub subject do |d|
|
68
|
+
d.to_s {"a value"}
|
69
|
+
d.to_sym {:crazy}
|
70
|
+
end
|
71
|
+
expect(subject.to_s).to eq "a value"
|
72
|
+
expect(subject.to_sym).to eq :crazy
|
73
|
+
end
|
74
|
+
|
75
|
+
it "stubs via block without argument" do
|
76
|
+
stub subject do
|
77
|
+
to_s {"a value"}
|
78
|
+
to_sym {:crazy}
|
79
|
+
end
|
80
|
+
expect(subject.to_s).to eq "a value"
|
81
|
+
expect(subject.to_sym).to eq :crazy
|
82
|
+
end
|
83
|
+
|
84
|
+
it "stubs instance_of" do
|
85
|
+
stub.instance_of(StubSpecFixture) do |o|
|
86
|
+
o.to_s {"High Level Spec"}
|
87
|
+
end
|
88
|
+
expect(StubSpecFixture.new.to_s).to eq "High Level Spec"
|
89
|
+
end
|
90
|
+
|
91
|
+
it "stubs methods without letters" do
|
92
|
+
stub(subject).__send__(:==) {:equality}
|
93
|
+
expect((subject == 55)).to eq :equality
|
94
|
+
end
|
95
|
+
|
96
|
+
it "stubs methods invoked in #initialize while passing along the #initialize arg" do
|
97
|
+
method_run_in_initialize_stubbed = false
|
98
|
+
stub.instance_of(StubSpecFixture) do |o|
|
99
|
+
o.method_run_in_initialize {method_run_in_initialize_stubbed = true}
|
100
|
+
end
|
101
|
+
StubSpecFixture.new
|
102
|
+
expect(method_run_in_initialize_stubbed).to be_true
|
103
|
+
end
|
104
|
+
|
105
|
+
it "passed the arguments and block passed to #initialize" do
|
106
|
+
block_called = false
|
107
|
+
stub.instance_of(StubSpecFixture) do |o|
|
108
|
+
o.method_run_in_initialize
|
109
|
+
end
|
110
|
+
instance = StubSpecFixture.new(1, 2) {block_called = true}
|
111
|
+
expect(instance.initialize_arguments).to eq [1, 2]
|
112
|
+
expect(block_called).to be_true
|
113
|
+
end
|
114
|
+
|
115
|
+
context "mock then stub" do
|
116
|
+
it "stubs any calls not matching the mock" do
|
117
|
+
mock(subject).foobar(3) {:baz3}
|
118
|
+
stub(subject).foobar {:baz}
|
119
|
+
expect(subject.foobar(3)).to eq :baz3
|
120
|
+
expect(subject.foobar(4)).to eq :baz
|
121
|
+
end
|
122
|
+
end
|
123
|
+
|
124
|
+
context "stub that yields" do
|
125
|
+
context "when yields called without any arguments" do
|
126
|
+
it "yields only once" do
|
127
|
+
called_from_block = mock!.foo.once.subject
|
128
|
+
block_caller = stub!.bar.yields.subject
|
129
|
+
block_caller.bar { called_from_block.foo }
|
130
|
+
end
|
131
|
+
end
|
132
|
+
|
133
|
+
context "when yields called with an argument" do
|
134
|
+
it "yields only once" do
|
135
|
+
called_from_block = mock!.foo(1).once.subject
|
136
|
+
block_caller = stub!.bar.yields(1).subject
|
137
|
+
block_caller.bar { |argument| called_from_block.foo(argument) }
|
138
|
+
end
|
139
|
+
end
|
140
|
+
|
141
|
+
context "when yields calls are chained" do
|
142
|
+
it "yields several times" do
|
143
|
+
pending "This test is failing with a TimesCalledError"
|
144
|
+
|
145
|
+
called_from_block = mock!.foo(1).once.then.foo(2).once.subject
|
146
|
+
block_caller = stub!.bar.yields(1).yields(2).subject
|
147
|
+
block_caller.bar { |argument| called_from_block.foo(argument) }
|
148
|
+
end
|
149
|
+
end
|
150
|
+
end
|
151
|
+
|
152
|
+
# bug #44
|
153
|
+
describe 'when wrapped in an array that is then flattened' do
|
154
|
+
context 'when the method being stubbed is not defined' do
|
155
|
+
it "does not raise an error" do
|
156
|
+
stub(subject).foo
|
157
|
+
expect([subject].flatten).to eq [subject]
|
158
|
+
end
|
159
|
+
|
160
|
+
it "honors a #to_ary that already exists" do
|
161
|
+
subject.instance_eval do
|
162
|
+
def to_ary; []; end
|
163
|
+
end
|
164
|
+
stub(subject).foo
|
165
|
+
expect([subject].flatten).to eq []
|
166
|
+
end
|
167
|
+
end
|
168
|
+
|
169
|
+
context 'when the method being stubbed is defined' do
|
170
|
+
before do
|
171
|
+
subject.instance_eval do
|
172
|
+
def foo; end
|
173
|
+
end
|
174
|
+
end
|
175
|
+
|
176
|
+
it "does not raise an error" do
|
177
|
+
stub(subject).foo
|
178
|
+
expect([subject].flatten).to eq [subject]
|
179
|
+
end
|
180
|
+
|
181
|
+
it "honors a #to_ary that already exists" do
|
182
|
+
eigen(subject).class_eval do
|
183
|
+
def to_ary; []; end
|
184
|
+
end
|
185
|
+
stub(subject).foo
|
186
|
+
expect([subject].flatten).to eq []
|
187
|
+
end
|
188
|
+
end
|
189
|
+
end
|
190
|
+
end
|
@@ -0,0 +1,128 @@
|
|
1
|
+
require File.expand_path('../../spec_helper', __FILE__)
|
2
|
+
require 'ostruct'
|
3
|
+
|
4
|
+
describe 'wildcard matchers' do
|
5
|
+
subject { Object.new }
|
6
|
+
|
7
|
+
describe '#anything' do
|
8
|
+
it "works outside a container" do
|
9
|
+
mock(subject).foo(anything)
|
10
|
+
subject.foo(:whatever)
|
11
|
+
end
|
12
|
+
|
13
|
+
it "works within a container too" do
|
14
|
+
mock(subject).foo([anything])
|
15
|
+
subject.foo([:whatever])
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
describe '#boolean' do
|
20
|
+
it "works outside a container" do
|
21
|
+
mock(subject).foo(boolean)
|
22
|
+
subject.foo(false)
|
23
|
+
end
|
24
|
+
|
25
|
+
it "works within a container too" do
|
26
|
+
mock(subject).foo([boolean])
|
27
|
+
subject.foo([false])
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
describe '#duck_type' do
|
32
|
+
it "works outside a container" do
|
33
|
+
mock(subject).foo(duck_type(:bar))
|
34
|
+
subject.foo(OpenStruct.new(:bar => 3))
|
35
|
+
end
|
36
|
+
|
37
|
+
it "works within a container too" do
|
38
|
+
mock(subject).foo([duck_type(:bar)])
|
39
|
+
subject.foo([OpenStruct.new(:bar => 3)])
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
describe '#hash_including' do
|
44
|
+
it "works outside a container" do
|
45
|
+
mock(subject).foo(hash_including(:c => 'd'))
|
46
|
+
subject.foo(:a => 'b', :c => 'd')
|
47
|
+
end
|
48
|
+
|
49
|
+
it "works within a container too" do
|
50
|
+
mock(subject).foo([hash_including(:c => 'd')])
|
51
|
+
subject.foo([{:a => 'b', :c => 'd'}])
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
describe '#is_a' do
|
56
|
+
context 'when outside a container' do
|
57
|
+
it "matches a simple value" do
|
58
|
+
mock(subject).foo(is_a(Symbol))
|
59
|
+
subject.foo(:symbol)
|
60
|
+
end
|
61
|
+
|
62
|
+
it "matches a container value" do
|
63
|
+
mock(subject).foo(is_a(Array))
|
64
|
+
subject.foo(['x', 'y'])
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
context 'within a container' do
|
69
|
+
it "matches a simple value" do
|
70
|
+
mock(subject).foo([is_a(Symbol)])
|
71
|
+
subject.foo([:symbol])
|
72
|
+
end
|
73
|
+
|
74
|
+
it "matches a container" do
|
75
|
+
mock(subject).foo([is_a(Hash)])
|
76
|
+
subject.foo([{:x => 'y'}])
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
describe '#numeric' do
|
82
|
+
it "works outside a container" do
|
83
|
+
mock(subject).foo(numeric)
|
84
|
+
subject.foo(3)
|
85
|
+
end
|
86
|
+
|
87
|
+
it "works within a container too" do
|
88
|
+
mock(subject).foo([numeric])
|
89
|
+
subject.foo([3])
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
describe 'range' do
|
94
|
+
it "works outside a container" do
|
95
|
+
mock(subject).foo(1..5)
|
96
|
+
subject.foo(3)
|
97
|
+
end
|
98
|
+
|
99
|
+
it "works within a container too" do
|
100
|
+
mock(subject).foo([1..5])
|
101
|
+
subject.foo([3])
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
describe 'regexp' do
|
106
|
+
it "works outside a container" do
|
107
|
+
mock(subject).foo(/foo/)
|
108
|
+
subject.foo('foobar')
|
109
|
+
end
|
110
|
+
|
111
|
+
it "works within a container too" do
|
112
|
+
mock(subject).foo([/foo/])
|
113
|
+
subject.foo(['foobar'])
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
117
|
+
describe '#satisfy' do
|
118
|
+
it "works outside a container" do
|
119
|
+
mock(subject).foo(satisfy {|x| x == 'x' })
|
120
|
+
subject.foo('x')
|
121
|
+
end
|
122
|
+
|
123
|
+
it "works within a container too" do
|
124
|
+
mock(subject).foo([satisfy {|x| x == 'x' }])
|
125
|
+
subject.foo(['x'])
|
126
|
+
end
|
127
|
+
end
|
128
|
+
end
|