/
and_yield_spec.rb
161 lines (123 loc) · 5.38 KB
/
and_yield_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
RSpec.describe RSpec::Mocks::Double do
let(:obj) { double }
describe "#and_yield" do
context 'when the method double has been constrained by `with`' do
it 'uses the default stub if the provided args do not match' do
allow(obj).to receive(:foo) { 15 }
allow(obj).to receive(:foo).with(:yield).and_yield
# should_receive is required to trigger the bug:
# https://github.com/rspec/rspec-mocks/issues/127
expect(obj).to receive(:foo)
expect(obj.foo(:dont_yield)).to eq(15)
end
end
context "with eval context as block argument" do
it "evaluates the supplied block as it is read" do
evaluated = false
allow(obj).to receive(:method_that_accepts_a_block).and_yield do |eval_context|
evaluated = true
end
expect(evaluated).to be_truthy
end
it "passes an eval context object to the supplied block" do
allow(obj).to receive(:method_that_accepts_a_block).and_yield do |eval_context|
expect(eval_context).not_to be_nil
end
end
it "evaluates the block passed to the stubbed method in the context of the supplied eval context" do
expected_eval_context = nil
actual_eval_context = nil
allow(obj).to receive(:method_that_accepts_a_block).and_yield do |eval_context|
expected_eval_context = eval_context
end
obj.method_that_accepts_a_block do
actual_eval_context = self
end
expect(actual_eval_context).to equal(expected_eval_context)
end
context "and no yielded arguments" do
it "passes when expectations set on the eval context are met" do
configured_eval_context = nil
allow(obj).to receive(:method_that_accepts_a_block).and_yield do |eval_context|
configured_eval_context = eval_context
expect(configured_eval_context).to receive(:foo)
end
obj.method_that_accepts_a_block do
foo
end
verify configured_eval_context
end
it "fails when expectations set on the eval context are not met" do
configured_eval_context = nil
allow(obj).to receive(:method_that_accepts_a_block).and_yield do |eval_context|
configured_eval_context = eval_context
expect(configured_eval_context).to receive(:foo)
end
obj.method_that_accepts_a_block do
# foo is not called here
end
expect { verify configured_eval_context }.to raise_error(RSpec::Mocks::MockExpectationError)
end
end
context "and yielded arguments" do
it "passes when expectations set on the eval context and yielded arguments are met" do
configured_eval_context = nil
yielded_arg = Object.new
allow(obj).to receive(:method_that_accepts_a_block).and_yield(yielded_arg) do |eval_context|
configured_eval_context = eval_context
expect(configured_eval_context).to receive(:foo)
expect(yielded_arg).to receive(:bar)
end
obj.method_that_accepts_a_block do |object|
object.bar
foo
end
verify configured_eval_context
verify yielded_arg
end
context "that are optional", :if => RSpec::Support::RubyFeatures.optional_and_splat_args_supported? do
it "yields the default argument when the argument is not given" do
pending "Not sure how to achieve this yet. See rspec/rspec-mocks#714 and rspec/rspec-support#85."
default_arg = Object.new
object = Object.new
allow(object).to receive(:a_message).and_yield
expect(default_arg).to receive(:bar)
eval("object.a_message { |receiver=default_arg| receiver.bar }")
end
it "yields given argument when the argument is given" do
default_arg = Object.new
allow(default_arg).to receive(:bar)
given_arg = Object.new
object = Object.new
allow(object).to receive(:a_message).and_yield(given_arg)
expect(given_arg).to receive(:bar)
eval("object.a_message { |receiver=default_arg| receiver.bar }")
end
end
it 'can yield to a block that uses `super`' do
klass = Class.new { def foo; 13; end }
subklass = Class.new(klass) { def foo(arg); arg.bar { super() }; end }
arg = double
expect(arg).to receive(:bar).and_yield
instance = subklass.new
instance.foo(arg)
end
it "fails when expectations set on the eval context and yielded arguments are not met" do
configured_eval_context = nil
yielded_arg = Object.new
allow(obj).to receive(:method_that_accepts_a_block).and_yield(yielded_arg) do |eval_context|
configured_eval_context = eval_context
expect(configured_eval_context).to receive(:foo)
expect(yielded_arg).to receive(:bar)
end
obj.method_that_accepts_a_block do |obj|
# obj.bar is not called here
# foo is not called here
end
expect { verify configured_eval_context }.to raise_error(RSpec::Mocks::MockExpectationError)
expect { verify yielded_arg }.to raise_error(RSpec::Mocks::MockExpectationError)
end
end
end
end
end