rspec-mocks 3.0.0.beta2 → 3.0.0.rc1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- checksums.yaml.gz.sig +0 -0
- data.tar.gz.sig +0 -0
- data/Changelog.md +104 -36
- data/README.md +35 -4
- data/lib/rspec/mocks.rb +52 -13
- data/lib/rspec/mocks/any_instance.rb +10 -0
- data/lib/rspec/mocks/any_instance/chain.rb +17 -0
- data/lib/rspec/mocks/any_instance/expect_chain_chain.rb +1 -1
- data/lib/rspec/mocks/any_instance/expectation_chain.rb +9 -6
- data/lib/rspec/mocks/any_instance/message_chains.rb +10 -1
- data/lib/rspec/mocks/any_instance/proxy.rb +112 -0
- data/lib/rspec/mocks/any_instance/recorder.rb +49 -18
- data/lib/rspec/mocks/any_instance/stub_chain.rb +5 -3
- data/lib/rspec/mocks/any_instance/stub_chain_chain.rb +1 -1
- data/lib/rspec/mocks/argument_list_matcher.rb +5 -3
- data/lib/rspec/mocks/argument_matchers.rb +28 -12
- data/lib/rspec/mocks/configuration.rb +29 -0
- data/lib/rspec/mocks/error_generator.rb +2 -5
- data/lib/rspec/mocks/example_methods.rb +122 -13
- data/lib/rspec/mocks/instance_method_stasher.rb +26 -2
- data/lib/rspec/mocks/marshal_extension.rb +41 -0
- data/lib/rspec/mocks/matchers/expectation_customization.rb +20 -0
- data/lib/rspec/mocks/matchers/receive.rb +6 -20
- data/lib/rspec/mocks/matchers/receive_message_chain.rb +7 -5
- data/lib/rspec/mocks/matchers/receive_messages.rb +1 -2
- data/lib/rspec/mocks/message_chain.rb +9 -9
- data/lib/rspec/mocks/message_expectation.rb +6 -5
- data/lib/rspec/mocks/method_double.rb +67 -10
- data/lib/rspec/mocks/method_reference.rb +1 -1
- data/lib/rspec/mocks/mutate_const.rb +10 -14
- data/lib/rspec/mocks/object_reference.rb +7 -4
- data/lib/rspec/mocks/proxy.rb +95 -8
- data/lib/rspec/mocks/space.rb +58 -9
- data/lib/rspec/mocks/syntax.rb +128 -210
- data/lib/rspec/mocks/targets.rb +6 -8
- data/lib/rspec/mocks/test_double.rb +16 -25
- data/lib/rspec/mocks/verifying_double.rb +15 -12
- data/lib/rspec/mocks/verifying_message_expecation.rb +3 -3
- data/lib/rspec/mocks/verifying_proxy.rb +23 -17
- data/lib/rspec/mocks/version.rb +3 -1
- metadata +29 -257
- metadata.gz.sig +0 -0
- data/features/README.md +0 -75
- data/features/Scope.md +0 -17
- data/features/Upgrade.md +0 -22
- data/features/argument_matchers/README.md +0 -27
- data/features/argument_matchers/explicit.feature +0 -59
- data/features/argument_matchers/general_matchers.feature +0 -85
- data/features/argument_matchers/type_matchers.feature +0 -26
- data/features/message_expectations/README.md +0 -75
- data/features/message_expectations/allow_any_instance_of.feature +0 -26
- data/features/message_expectations/any_instance.feature +0 -43
- data/features/message_expectations/block_local_expectations.feature.pending +0 -55
- data/features/message_expectations/call_original.feature +0 -23
- data/features/message_expectations/expect_message_using_expect.feature +0 -107
- data/features/message_expectations/expect_message_using_should_receive.feature +0 -118
- data/features/message_expectations/message_chains_using_expect.feature +0 -49
- data/features/message_expectations/receive_counts.feature +0 -209
- data/features/message_expectations/warn_when_expectation_is_set_on_nil.feature +0 -50
- data/features/method_stubs/README.md +0 -77
- data/features/method_stubs/allow_any_instance_of.feature +0 -136
- data/features/method_stubs/as_null_object.feature +0 -40
- data/features/method_stubs/receive_message_chain.feature +0 -51
- data/features/method_stubs/simple_return_value_with_allow.feature +0 -44
- data/features/method_stubs/simple_return_value_with_stub.feature +0 -64
- data/features/method_stubs/stub_implementation.feature +0 -48
- data/features/method_stubs/to_ary.feature +0 -51
- data/features/mutating_constants/README.md +0 -82
- data/features/mutating_constants/hiding_defined_constant.feature +0 -64
- data/features/mutating_constants/stub_defined_constant.feature +0 -78
- data/features/mutating_constants/stub_undefined_constant.feature +0 -50
- data/features/outside_rspec/standalone.feature +0 -33
- data/features/spies/spy_partial_mock_method.feature +0 -34
- data/features/spies/spy_pure_mock_method.feature +0 -76
- data/features/spies/spy_unstubbed_method.feature +0 -18
- data/features/step_definitions/additional_cli_steps.rb +0 -11
- data/features/support/env.rb +0 -22
- data/features/support/rubinius.rb +0 -6
- data/features/test_frameworks/test_unit.feature +0 -54
- data/features/verifying_doubles/README.md +0 -17
- data/features/verifying_doubles/class_doubles.feature +0 -73
- data/features/verifying_doubles/dynamic_classes.feature +0 -71
- data/features/verifying_doubles/instance_doubles.feature +0 -103
- data/features/verifying_doubles/object_doubles.feature +0 -65
- data/features/verifying_doubles/partial_doubles.feature +0 -34
- data/lib/rspec/mocks/extensions/marshal.rb +0 -17
- data/lib/rspec/mocks/framework.rb +0 -35
- data/lib/rspec/mocks/method_signature_verifier.rb +0 -207
- data/lib/rspec/mocks/ruby_features.rb +0 -24
- data/spec/rspec/mocks/and_call_original_spec.rb +0 -268
- data/spec/rspec/mocks/and_return_spec.rb +0 -23
- data/spec/rspec/mocks/and_yield_spec.rb +0 -126
- data/spec/rspec/mocks/any_instance/message_chains_spec.rb +0 -41
- data/spec/rspec/mocks/any_instance_spec.rb +0 -1045
- data/spec/rspec/mocks/array_including_matcher_spec.rb +0 -41
- data/spec/rspec/mocks/at_least_spec.rb +0 -123
- data/spec/rspec/mocks/at_most_spec.rb +0 -90
- data/spec/rspec/mocks/before_all_spec.rb +0 -132
- data/spec/rspec/mocks/block_return_value_spec.rb +0 -70
- data/spec/rspec/mocks/combining_implementation_instructions_spec.rb +0 -204
- data/spec/rspec/mocks/configuration_spec.rb +0 -242
- data/spec/rspec/mocks/double_spec.rb +0 -874
- data/spec/rspec/mocks/example_methods_spec.rb +0 -13
- data/spec/rspec/mocks/extensions/marshal_spec.rb +0 -54
- data/spec/rspec/mocks/failing_argument_matchers_spec.rb +0 -184
- data/spec/rspec/mocks/hash_excluding_matcher_spec.rb +0 -67
- data/spec/rspec/mocks/hash_including_matcher_spec.rb +0 -94
- data/spec/rspec/mocks/instance_method_stasher_spec.rb +0 -74
- data/spec/rspec/mocks/matchers/have_received_spec.rb +0 -347
- data/spec/rspec/mocks/matchers/receive_message_chain_spec.rb +0 -198
- data/spec/rspec/mocks/matchers/receive_messages_spec.rb +0 -140
- data/spec/rspec/mocks/matchers/receive_spec.rb +0 -418
- data/spec/rspec/mocks/method_signature_verifier_spec.rb +0 -272
- data/spec/rspec/mocks/methods_spec.rb +0 -26
- data/spec/rspec/mocks/mock_expectation_error_spec.rb +0 -22
- data/spec/rspec/mocks/mock_ordering_spec.rb +0 -114
- data/spec/rspec/mocks/multiple_return_value_spec.rb +0 -132
- data/spec/rspec/mocks/mutate_const_spec.rb +0 -542
- data/spec/rspec/mocks/nil_expectation_warning_spec.rb +0 -52
- data/spec/rspec/mocks/null_object_mock_spec.rb +0 -133
- data/spec/rspec/mocks/once_counts_spec.rb +0 -52
- data/spec/rspec/mocks/options_hash_spec.rb +0 -35
- data/spec/rspec/mocks/order_group_spec.rb +0 -27
- data/spec/rspec/mocks/partial_double_spec.rb +0 -308
- data/spec/rspec/mocks/partial_double_using_mocks_directly_spec.rb +0 -95
- data/spec/rspec/mocks/passing_argument_matchers_spec.rb +0 -145
- data/spec/rspec/mocks/precise_counts_spec.rb +0 -68
- data/spec/rspec/mocks/record_messages_spec.rb +0 -26
- data/spec/rspec/mocks/serialization_spec.rb +0 -90
- data/spec/rspec/mocks/space_spec.rb +0 -244
- data/spec/rspec/mocks/stash_spec.rb +0 -46
- data/spec/rspec/mocks/stub_chain_spec.rb +0 -168
- data/spec/rspec/mocks/stub_implementation_spec.rb +0 -81
- data/spec/rspec/mocks/stub_spec.rb +0 -327
- data/spec/rspec/mocks/stubbed_message_expectations_spec.rb +0 -58
- data/spec/rspec/mocks/syntax_agnostic_message_matchers_spec.rb +0 -101
- data/spec/rspec/mocks/syntax_spec.rb +0 -19
- data/spec/rspec/mocks/test_double_spec.rb +0 -49
- data/spec/rspec/mocks/to_ary_spec.rb +0 -54
- data/spec/rspec/mocks/twice_counts_spec.rb +0 -66
- data/spec/rspec/mocks/verifying_double_spec.rb +0 -590
- data/spec/rspec/mocks/verifying_message_expecation_spec.rb +0 -69
- data/spec/rspec/mocks_spec.rb +0 -192
- data/spec/spec_helper.rb +0 -121
@@ -1,50 +0,0 @@
|
|
1
|
-
Feature: warn when expectation is set on nil
|
2
|
-
|
3
|
-
Scenario: nil instance variable
|
4
|
-
Given a file named "example_spec.rb" with:
|
5
|
-
"""ruby
|
6
|
-
RSpec.configure {|c| c.mock_with :rspec}
|
7
|
-
describe "something" do
|
8
|
-
it "does something" do
|
9
|
-
expect(@i_do_not_exist).to receive(:foo)
|
10
|
-
@i_do_not_exist.foo
|
11
|
-
end
|
12
|
-
end
|
13
|
-
"""
|
14
|
-
When I run `rspec example_spec.rb`
|
15
|
-
Then the output should contain "An expectation of :foo was set on nil"
|
16
|
-
|
17
|
-
Scenario: allow
|
18
|
-
Given a file named "example_spec.rb" with:
|
19
|
-
"""ruby
|
20
|
-
RSpec.configure {|c| c.mock_with :rspec}
|
21
|
-
describe "something" do
|
22
|
-
it "does something" do
|
23
|
-
allow_message_expectations_on_nil
|
24
|
-
expect(nil).to receive(:foo)
|
25
|
-
nil.foo
|
26
|
-
end
|
27
|
-
end
|
28
|
-
"""
|
29
|
-
When I run `rspec example_spec.rb`
|
30
|
-
Then the output should not contain "An expectation"
|
31
|
-
|
32
|
-
Scenario: allow in one example, but not on another
|
33
|
-
Given a file named "example_spec.rb" with:
|
34
|
-
"""ruby
|
35
|
-
RSpec.configure {|c| c.mock_with :rspec}
|
36
|
-
describe "something" do
|
37
|
-
it "does something (foo)" do
|
38
|
-
allow_message_expectations_on_nil
|
39
|
-
nil.should_receive(:foo)
|
40
|
-
nil.foo
|
41
|
-
end
|
42
|
-
it "does something (bar)" do
|
43
|
-
expect(nil).to receive(:bar)
|
44
|
-
nil.bar
|
45
|
-
end
|
46
|
-
end
|
47
|
-
"""
|
48
|
-
When I run `rspec example_spec.rb`
|
49
|
-
Then the output should contain "An expectation of :bar"
|
50
|
-
And the output should not contain "An expectation of :foo"
|
@@ -1,77 +0,0 @@
|
|
1
|
-
### Stub return values
|
2
|
-
|
3
|
-
# create a double
|
4
|
-
obj = double()
|
5
|
-
|
6
|
-
# specify a return value using `:expect` syntax
|
7
|
-
allow(obj).to receive(:message) { :value }
|
8
|
-
allow(obj).to receive(:message).and_return(:value)
|
9
|
-
|
10
|
-
# specify a return value using `:should` syntax
|
11
|
-
obj.stub(:message) { :value }
|
12
|
-
obj.stub(:message => :value)
|
13
|
-
obj.stub(:message).and_return(:value)
|
14
|
-
|
15
|
-
These forms are somewhat interchangeable. The difference is that the
|
16
|
-
block contents are evaluated lazily when the `obj` receives the
|
17
|
-
`message` message, whereas the others are evaluated as they are read.
|
18
|
-
|
19
|
-
### Fake implementation
|
20
|
-
|
21
|
-
allow(obj).to receive(:message) do |arg1, arg2|
|
22
|
-
# set expectations about the args in this block
|
23
|
-
# and/or return value
|
24
|
-
end
|
25
|
-
|
26
|
-
obj.stub(:message) do |arg1, arg2|
|
27
|
-
# set expectations about the args in this block
|
28
|
-
# and/or return a value
|
29
|
-
end
|
30
|
-
|
31
|
-
### Raising/Throwing
|
32
|
-
|
33
|
-
allow(obj).to receive(:message).and_raise("this error")
|
34
|
-
allow(obj).to receive(:message).and_throw(:this_symbol)
|
35
|
-
|
36
|
-
obj.stub(:message).and_raise("this error")
|
37
|
-
obj.stub(:message).and_throw(:this_symbol)
|
38
|
-
|
39
|
-
You can also use the block format:
|
40
|
-
|
41
|
-
allow(obj).to receive(:message) { raise "this error" }
|
42
|
-
allow(obj).to receive(:message) { throw :this_symbol }
|
43
|
-
|
44
|
-
obj.stub(:message) { raise "this error" }
|
45
|
-
obj.stub(:message) { throw :this_symbol }
|
46
|
-
|
47
|
-
### Argument constraints
|
48
|
-
|
49
|
-
#### Explicit arguments
|
50
|
-
|
51
|
-
allow(obj).to receive(:message).with('an argument') { ... }
|
52
|
-
allow(obj).to receive(:message).with('more than', 'an argument') { ... }
|
53
|
-
|
54
|
-
obj.stub(:message).with('an argument') { ... }
|
55
|
-
obj.stub(:message).with('more_than', 'one_argument') { ... }
|
56
|
-
|
57
|
-
#### Argument matchers
|
58
|
-
|
59
|
-
allow(obj).to receive(:message).with(anything()) { ... }
|
60
|
-
allow(obj).to receive(:message).with(an_instance_of(Money)) { ... }
|
61
|
-
allow(obj).to receive(:message).with(hash_including(:a => 'b')) { ... }
|
62
|
-
allow(obj).to receive(:message).with(array_including(1,2,3)) { ... }
|
63
|
-
# or
|
64
|
-
allow(obj).to receive(:message).with(array_including([1,2,3])) { ... }
|
65
|
-
|
66
|
-
obj.stub(:message).with(anything()) { ... }
|
67
|
-
obj.stub(:message).with(an_instance_of(Money)) { ... }
|
68
|
-
obj.stub(:message).with(hash_including(:a => 'b')) { ... }
|
69
|
-
obj.stub(:message).with(array_including(1,2,3)) { ... }
|
70
|
-
# or
|
71
|
-
obj.stub(:message).with(array_including([1,2,3])) { ... }
|
72
|
-
|
73
|
-
#### Regular expressions
|
74
|
-
|
75
|
-
allow(obj).to receive(:message).with(/abc/) { ... }
|
76
|
-
|
77
|
-
obj.stub(:message).with(/abc/) { ... }
|
@@ -1,136 +0,0 @@
|
|
1
|
-
Feature: stub on any instance of a class
|
2
|
-
|
3
|
-
Use `allow_any_instance_of` on a class to tell any instance of that class to
|
4
|
-
return a value (or values) in response to a given message. If no instance
|
5
|
-
receives the message, nothing happens.
|
6
|
-
|
7
|
-
Messages can be stubbed on any class, including those in Ruby's core library.
|
8
|
-
|
9
|
-
Note: You can use `allow_any_instance_of` when you don't have a reference
|
10
|
-
to the object that receives a message in your test. For more information,
|
11
|
-
see the message_expectations/allow_any_instance_of feature.
|
12
|
-
|
13
|
-
Scenario: Stubbing any instance of an object with a single return value
|
14
|
-
Given a file named "example_spec.rb" with:
|
15
|
-
"""ruby
|
16
|
-
describe "stubbing any instance" do
|
17
|
-
it "returns the specified value on any instance of the class" do
|
18
|
-
allow_any_instance_of(Object).to receive(:foo).and_return(:return_value)
|
19
|
-
|
20
|
-
o = Object.new
|
21
|
-
expect(o.foo).to eq(:return_value)
|
22
|
-
end
|
23
|
-
end
|
24
|
-
"""
|
25
|
-
When I run `rspec example_spec.rb`
|
26
|
-
Then the examples should all pass
|
27
|
-
|
28
|
-
Scenario: any_instance stub with a hash
|
29
|
-
Given a file named "example_spec.rb" with:
|
30
|
-
"""ruby
|
31
|
-
describe "any_instance.stub" do
|
32
|
-
context "with a hash" do
|
33
|
-
it "returns the hash values on any instance of the class" do
|
34
|
-
allow_any_instance_of(Object).to receive_messages(:foo => 'foo', :bar => 'bar')
|
35
|
-
|
36
|
-
o = Object.new
|
37
|
-
expect(o.foo).to eq('foo')
|
38
|
-
expect(o.bar).to eq('bar')
|
39
|
-
end
|
40
|
-
end
|
41
|
-
end
|
42
|
-
"""
|
43
|
-
When I run `rspec example_spec.rb`
|
44
|
-
Then the examples should all pass
|
45
|
-
|
46
|
-
Scenario: Stubbing any instance of an object with specific arguments matchers
|
47
|
-
Given a file named "example_spec.rb" with:
|
48
|
-
"""ruby
|
49
|
-
describe "stubbing any instance" do
|
50
|
-
context "with arguments" do
|
51
|
-
it "returns the stubbed value when arguments match" do
|
52
|
-
allow_any_instance_of(Object).to receive(:foo).with(:param_one, :param_two).and_return(:result_one)
|
53
|
-
allow_any_instance_of(Object).to receive(:foo).with(:param_three, :param_four).and_return(:result_two)
|
54
|
-
|
55
|
-
o = Object.new
|
56
|
-
expect(o.foo(:param_one, :param_two)).to eq(:result_one)
|
57
|
-
expect(o.foo(:param_three, :param_four)).to eq(:result_two)
|
58
|
-
end
|
59
|
-
end
|
60
|
-
end
|
61
|
-
"""
|
62
|
-
When I run `rspec example_spec.rb`
|
63
|
-
Then the examples should all pass
|
64
|
-
|
65
|
-
Scenario: any_instance unstub
|
66
|
-
Given a file named "example_spec.rb" with:
|
67
|
-
"""ruby
|
68
|
-
describe "any_instance.unstub" do
|
69
|
-
it "unstubs a stubbed method" do
|
70
|
-
class Object
|
71
|
-
def foo
|
72
|
-
:foo
|
73
|
-
end
|
74
|
-
end
|
75
|
-
|
76
|
-
Object.any_instance.stub(:foo)
|
77
|
-
Object.any_instance.unstub(:foo)
|
78
|
-
|
79
|
-
expect(Object.new.foo).to eq(:foo)
|
80
|
-
end
|
81
|
-
end
|
82
|
-
"""
|
83
|
-
When I run `rspec example_spec.rb`
|
84
|
-
Then the examples should all pass
|
85
|
-
|
86
|
-
Scenario: any_instance unstub
|
87
|
-
Given a file named "example_spec.rb" with:
|
88
|
-
"""ruby
|
89
|
-
describe "any_instance.unstub" do
|
90
|
-
context "with both an expectation and a stub already set" do
|
91
|
-
it "only removes the stub" do
|
92
|
-
class Object
|
93
|
-
def foo
|
94
|
-
:foo
|
95
|
-
end
|
96
|
-
end
|
97
|
-
expect_any_instance_of(Object).to receive(:foo).and_return(:bar)
|
98
|
-
Object.any_instance.stub(:foo)
|
99
|
-
Object.any_instance.unstub(:foo)
|
100
|
-
|
101
|
-
expect(Object.new.foo).to eq(:bar)
|
102
|
-
end
|
103
|
-
end
|
104
|
-
end
|
105
|
-
"""
|
106
|
-
When I run `rspec example_spec.rb`
|
107
|
-
Then the examples should all pass
|
108
|
-
|
109
|
-
Scenario: stub a chain of methods an any instance
|
110
|
-
Given a file named "stub_chain_spec.rb" with:
|
111
|
-
"""ruby
|
112
|
-
describe "stubbing a chain of methods" do
|
113
|
-
context "given symbols representing methods" do
|
114
|
-
it "returns the correct value" do
|
115
|
-
allow_any_instance_of(Object).to receive_message_chain(:one, :two, :three).and_return(:four)
|
116
|
-
expect(Object.new.one.two.three).to eq(:four)
|
117
|
-
end
|
118
|
-
end
|
119
|
-
|
120
|
-
context "given a hash at the end" do
|
121
|
-
it "returns the correct value" do
|
122
|
-
allow_any_instance_of(Object).to receive_message_chain(:one, :two, :three=> :four)
|
123
|
-
expect(Object.new.one.two.three).to eq(:four)
|
124
|
-
end
|
125
|
-
end
|
126
|
-
|
127
|
-
context "given a string of methods separated by dots" do
|
128
|
-
it "returns the correct value" do
|
129
|
-
allow_any_instance_of(Object).to receive_message_chain("one.two.three").and_return(:four)
|
130
|
-
expect(Object.new.one.two.three).to eq(:four)
|
131
|
-
end
|
132
|
-
end
|
133
|
-
end
|
134
|
-
"""
|
135
|
-
When I run `rspec stub_chain_spec.rb`
|
136
|
-
Then the examples should all pass
|
@@ -1,40 +0,0 @@
|
|
1
|
-
Feature: as_null_object
|
2
|
-
|
3
|
-
Use the `as_null_object` method to ignore any messages that aren't explicitly
|
4
|
-
set as stubs or message expectations.
|
5
|
-
|
6
|
-
EXCEPTION: `to_ary` will raise a NoMethodError unless explicitly stubbed in
|
7
|
-
order to support `flatten` on an Array containing a double.
|
8
|
-
|
9
|
-
Scenario: double acting as_null_object
|
10
|
-
Given a file named "as_null_object_spec.rb" with:
|
11
|
-
"""ruby
|
12
|
-
describe "a double with as_null_object called" do
|
13
|
-
let(:null_object) { double('null object').as_null_object }
|
14
|
-
|
15
|
-
it "responds to any method that is not defined" do
|
16
|
-
expect(null_object).to respond_to(:an_undefined_method)
|
17
|
-
end
|
18
|
-
|
19
|
-
it "allows explicit stubs using expect syntax" do
|
20
|
-
allow(null_object).to receive(:foo) { "bar" }
|
21
|
-
expect(null_object.foo).to eq("bar")
|
22
|
-
end
|
23
|
-
|
24
|
-
it "allows explicit stubs using should syntax" do
|
25
|
-
null_object.stub(:foo) { "bar" }
|
26
|
-
expect(null_object.foo).to eq("bar")
|
27
|
-
end
|
28
|
-
|
29
|
-
it "allows explicit expectations" do
|
30
|
-
expect(null_object).to receive(:something)
|
31
|
-
null_object.something
|
32
|
-
end
|
33
|
-
|
34
|
-
it "supports Array#flatten" do
|
35
|
-
expect([null_object].flatten).to eq([null_object])
|
36
|
-
end
|
37
|
-
end
|
38
|
-
"""
|
39
|
-
When I run `rspec as_null_object_spec.rb`
|
40
|
-
Then the examples should all pass
|
@@ -1,51 +0,0 @@
|
|
1
|
-
Feature: stub a chain of methods
|
2
|
-
|
3
|
-
Use the `stub_chain` method to stub a chain of two or more methods in one
|
4
|
-
statement. Method chains are considered a design smell, but it's not really
|
5
|
-
the method chain itself that is the problem - it's the dependency chain
|
6
|
-
represented by a chain of messages to different objects:
|
7
|
-
|
8
|
-
foo.get_bar.get_baz
|
9
|
-
|
10
|
-
This is a Law of Demeter violation if `get_bar` returns an object other than
|
11
|
-
`foo`, and `get_baz` returns yet another object.
|
12
|
-
|
13
|
-
Fluent interfaces look similar from a caller's perspective, but don't
|
14
|
-
represent a dependency chain (the caller depends only on the object it is
|
15
|
-
calling). Consider this common example from Ruby on Rails:
|
16
|
-
|
17
|
-
Article.recent.by(current_user)
|
18
|
-
|
19
|
-
The `recent` and `by` methods return the same object, so this is not a Law of
|
20
|
-
Demeter violation.
|
21
|
-
|
22
|
-
Scenario: stub a chain of methods
|
23
|
-
Given a file named "stub_chain_spec.rb" with:
|
24
|
-
"""ruby
|
25
|
-
describe "stubbing a chain of methods" do
|
26
|
-
subject { Object.new }
|
27
|
-
|
28
|
-
context "given symbols representing methods" do
|
29
|
-
it "returns the correct value" do
|
30
|
-
allow(subject).to receive_message_chain(:one, :two, :three).and_return(:four)
|
31
|
-
expect(subject.one.two.three).to eq(:four)
|
32
|
-
end
|
33
|
-
end
|
34
|
-
|
35
|
-
context "given a hash at the end" do
|
36
|
-
it "returns the correct value" do
|
37
|
-
allow(subject).to receive_message_chain(:one, :two, :three => :four)
|
38
|
-
expect(subject.one.two.three).to eq(:four)
|
39
|
-
end
|
40
|
-
end
|
41
|
-
|
42
|
-
context "given a string of methods separated by dots" do
|
43
|
-
it "returns the correct value" do
|
44
|
-
allow(subject).to receive_message_chain("one.two.three").and_return(:four)
|
45
|
-
expect(subject.one.two.three).to eq(:four)
|
46
|
-
end
|
47
|
-
end
|
48
|
-
end
|
49
|
-
"""
|
50
|
-
When I run `rspec stub_chain_spec.rb`
|
51
|
-
Then the examples should all pass
|
@@ -1,44 +0,0 @@
|
|
1
|
-
Feature: allow with a simple return value
|
2
|
-
|
3
|
-
Use the `allow` method with the `receive` matcher on a test double or a real
|
4
|
-
object to tell the object to return a value (or values) in response to a given
|
5
|
-
message. Nothing happens if the message is never received.
|
6
|
-
|
7
|
-
Scenario: stub with no return value
|
8
|
-
Given a file named "example_spec.rb" with:
|
9
|
-
"""ruby
|
10
|
-
describe "a stub with no return value specified" do
|
11
|
-
let(:collaborator) { double("collaborator") }
|
12
|
-
|
13
|
-
it "returns nil" do
|
14
|
-
allow(collaborator).to receive(:message)
|
15
|
-
expect(collaborator.message).to be(nil)
|
16
|
-
end
|
17
|
-
end
|
18
|
-
"""
|
19
|
-
When I run `rspec example_spec.rb`
|
20
|
-
Then the examples should all pass
|
21
|
-
|
22
|
-
Scenario: stubs with return values
|
23
|
-
Given a file named "example_spec.rb" with:
|
24
|
-
"""ruby
|
25
|
-
describe "a stub with a return value" do
|
26
|
-
context "specified in a block" do
|
27
|
-
it "returns the specified value" do
|
28
|
-
collaborator = double("collaborator")
|
29
|
-
allow(collaborator).to receive(:message) { :value }
|
30
|
-
expect(collaborator.message).to eq(:value)
|
31
|
-
end
|
32
|
-
end
|
33
|
-
|
34
|
-
context "specified with #and_return" do
|
35
|
-
it "returns the specified value" do
|
36
|
-
collaborator = double("collaborator")
|
37
|
-
allow(collaborator).to receive(:message).and_return(:value)
|
38
|
-
expect(collaborator.message).to eq(:value)
|
39
|
-
end
|
40
|
-
end
|
41
|
-
end
|
42
|
-
"""
|
43
|
-
When I run `rspec example_spec.rb`
|
44
|
-
Then the examples should all pass
|
@@ -1,64 +0,0 @@
|
|
1
|
-
Feature: stub with a simple return value
|
2
|
-
|
3
|
-
Use the `stub` method on a test double or a real object to tell the object to
|
4
|
-
return a value (or values) in response to a given message. Nothing happens if
|
5
|
-
the message is never received.
|
6
|
-
|
7
|
-
Scenario: stub with no return value
|
8
|
-
Given a file named "example_spec.rb" with:
|
9
|
-
"""ruby
|
10
|
-
describe "a stub with no return value specified" do
|
11
|
-
let(:collaborator) { double("collaborator") }
|
12
|
-
|
13
|
-
it "returns nil" do
|
14
|
-
collaborator.stub(:message)
|
15
|
-
expect(collaborator.message).to be(nil)
|
16
|
-
end
|
17
|
-
end
|
18
|
-
"""
|
19
|
-
When I run `rspec example_spec.rb`
|
20
|
-
Then the examples should all pass
|
21
|
-
|
22
|
-
Scenario: stubs with return values
|
23
|
-
Given a file named "example_spec.rb" with:
|
24
|
-
"""ruby
|
25
|
-
describe "a stub with a return value" do
|
26
|
-
context "specified in a block" do
|
27
|
-
it "returns the specified value" do
|
28
|
-
collaborator = double("collaborator")
|
29
|
-
collaborator.stub(:message) { :value }
|
30
|
-
expect(collaborator.message).to eq(:value)
|
31
|
-
end
|
32
|
-
end
|
33
|
-
|
34
|
-
context "specified with #and_return" do
|
35
|
-
it "returns the specified value" do
|
36
|
-
collaborator = double("collaborator")
|
37
|
-
collaborator.stub(:message).and_return(:value)
|
38
|
-
expect(collaborator.message).to eq(:value)
|
39
|
-
end
|
40
|
-
end
|
41
|
-
|
42
|
-
context "specified with a hash passed to #stub" do
|
43
|
-
it "returns the specified value" do
|
44
|
-
collaborator = double("collaborator")
|
45
|
-
collaborator.stub(:message_1 => :value_1, :message_2 => :value_2)
|
46
|
-
expect(collaborator.message_1).to eq(:value_1)
|
47
|
-
expect(collaborator.message_2).to eq(:value_2)
|
48
|
-
end
|
49
|
-
end
|
50
|
-
|
51
|
-
context "specified with a hash passed to #double" do
|
52
|
-
it "returns the specified value" do
|
53
|
-
collaborator = double("collaborator",
|
54
|
-
:message_1 => :value_1,
|
55
|
-
:message_2 => :value_2
|
56
|
-
)
|
57
|
-
expect(collaborator.message_1).to eq(:value_1)
|
58
|
-
expect(collaborator.message_2).to eq(:value_2)
|
59
|
-
end
|
60
|
-
end
|
61
|
-
end
|
62
|
-
"""
|
63
|
-
When I run `rspec example_spec.rb`
|
64
|
-
Then the examples should all pass
|