File: receive_message_chain_spec.rb

package info (click to toggle)
ruby-rspec 3.13.0c0e0m0s1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 6,856 kB
  • sloc: ruby: 70,868; sh: 1,423; makefile: 99
file content (248 lines) | stat: -rw-r--r-- 8,722 bytes parent folder | download
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
module RSpec::Mocks::Matchers
  RSpec.describe "receive_message_chain" do
    let(:object) { double(:object) }

    context "with only the expect syntax enabled" do
      include_context "with syntax", :expect

      it "errors with a negative allowance" do
        expect {
          allow(object).not_to receive_message_chain(:to_a)
        }.to raise_error(RSpec::Mocks::NegationUnsupportedError)
      end

      it "errors with a negative expectation" do
        expect {
          expect(object).not_to receive_message_chain(:to_a)
        }.to raise_error(RSpec::Mocks::NegationUnsupportedError)
      end

      it "errors with a negative any_instance expectation" do
        expect {
          expect_any_instance_of(Object).not_to receive_message_chain(:to_a)
        }.to raise_error(RSpec::Mocks::NegationUnsupportedError)
      end

      it "errors with a negative any_instance allowance" do
        expect {
          allow_any_instance_of(Object).not_to receive_message_chain(:to_a)
        }.to raise_error(RSpec::Mocks::NegationUnsupportedError)
      end

      it "works with a do block" do
        allow(object).to receive_message_chain(:to_a, :length) do
          3
        end

        expect(object.to_a.length).to eq(3)
      end

      it "works with a {} block" do
        allow(object).to receive_message_chain(:to_a, :length) { 3 }

        expect(object.to_a.length).to eq(3)
      end

      it "gives the { } block precedence over the do block" do
        allow(object).to receive_message_chain(:to_a, :length) { 3 } do
          4
        end

        expect(object.to_a.length).to eq(3)
      end

      it "works with and_return" do
        allow(object).to receive_message_chain(:to_a, :length).and_return(3)

        expect(object.to_a.length).to eq(3)
      end

      it "works with and_invoke" do
        allow(object).to receive_message_chain(:to_a, :length).and_invoke(lambda { raise "error" })

        expect { object.to_a.length }.to raise_error("error")
      end

      it "can constrain the return value by the argument to the last call" do
        allow(object).to receive_message_chain(:one, :plus).with(1) { 2 }
        allow(object).to receive_message_chain(:one, :plus).with(2) { 3 }
        expect(object.one.plus(1)).to eq(2)
        expect(object.one.plus(2)).to eq(3)
      end

      it "works with and_call_original", :pending => "See https://github.com/rspec/rspec-mocks/pull/467#issuecomment-28631621" do
        list = [1, 2, 3]
        expect(list).to receive_message_chain(:to_a, :length).and_call_original
        expect(list.to_a.length).to eq(3)
      end

      it "fails with and_call_original when the entire chain is not called", :pending => "See https://github.com/rspec/rspec-mocks/pull/467#issuecomment-28631621" do
        list = [1, 2, 3]
        expect(list).to receive_message_chain(:to_a, :length).and_call_original
        expect(list.to_a).to eq([1, 2, 3])
      end

      it "works with and_raise" do
        allow(object).to receive_message_chain(:to_a, :length).and_raise(StandardError.new("hi"))

        expect { object.to_a.length }.to raise_error(StandardError, "hi")
      end

      it "works with and_throw" do
        allow(object).to receive_message_chain(:to_a, :length).and_throw(:nope)

        expect { object.to_a.length }.to throw_symbol(:nope)
      end

      it "works with and_yield" do
        allow(object).to receive_message_chain(:to_a, :length).and_yield(3)

        expect { |blk| object.to_a.length(&blk) }.to yield_with_args(3)
      end

      it "works with a string of messages to chain" do
        allow(object).to receive_message_chain("to_a.length").and_yield(3)

        expect { |blk| object.to_a.length(&blk) }.to yield_with_args(3)
      end

      it "works with a hash return as the last argument in the chain" do
        allow(object).to receive_message_chain(:to_a, :length => 3)

        expect(object.to_a.length).to eq(3)
      end

      it "accepts any number of arguments to the stubbed messages" do
        allow(object).to receive_message_chain(:msg1, :msg2).and_return(:return_value)

        expect(object.msg1("nonsense", :value).msg2("another", :nonsense, 3.0, "value")).to eq(:return_value)
      end

      it "accepts any number of arguments to the stubbed messages with an inline hash return value" do
        allow(object).to receive_message_chain(:msg1, :msg2 => :return_value)

        expect(object.msg1("nonsense", :value).msg2("another", :nonsense, 3.0, "value")).to eq(:return_value)
      end

      it "raises when expect is used and some of the messages in the chain aren't called" do
        expect {
          expect(object).to receive_message_chain(:to_a, :farce, :length => 3)
          object.to_a
          verify_all
        }.to fail
      end

      it "raises when expect is used and all but the last message in the chain are called" do
        expect {
          expect(object).to receive_message_chain(:foo, :bar, :baz)
          object.foo.bar
          verify_all
        }.to fail
      end

      it "does not raise when expect is used and the entire chain is called" do
        expect {
          expect(object).to receive_message_chain(:to_a, :length => 3)
          object.to_a.length
          verify_all
        }.not_to raise_error
      end

      it "works with allow_any_instance" do
        o = Object.new

        allow_any_instance_of(Object).to receive_message_chain(:to_a, :length => 3)

        expect(o.to_a.length).to eq(3)
      end

      it "stubs already stubbed instances when using `allow_any_instance_of`" do
        o = Object.new
        allow(o).to receive(:foo).and_return(dbl = double)
        expect(o.foo).to be(dbl)

        allow_any_instance_of(Object).to receive_message_chain(:foo, :bar).and_return("bazz")
        expect(o.foo.bar).to eq("bazz")
      end

      it "fails when with expect_any_instance_of is used and the entire chain is not called" do
        expect {
          expect_any_instance_of(Object).to receive_message_chain(:to_a, :length => 3)
          verify_all
        }.to fail
      end

      it "affects previously stubbed instances when `expect_any_instance_of` is called" do
        o = Object.new
        allow(o).to receive(:foo).and_return(double)

        expect_any_instance_of(Object).to receive_message_chain(:foo, :bar => 3)
        expect(o.foo.bar).to eq(3)
      end

      it "passes when with expect_any_instance_of is used and the entire chain is called" do
        o = Object.new

        expect_any_instance_of(Object).to receive_message_chain(:to_a, :length => 3)
        o.to_a.length
      end

      it "works with expect where the first level of the chain is already expected" do
        o = Object.new
        expect(o).to receive(:foo).and_return(double)
        expect(o).to receive_message_chain(:foo, :bar, :baz)

        o.foo.bar.baz
      end

      it "works with allow where the first level of the chain is already expected" do
        o = Object.new
        expect(o).to receive(:foo).and_return(double)
        allow(o).to receive_message_chain(:foo, :bar, :baz).and_return(3)

        expect(o.foo.bar.baz).to eq(3)
      end

      it "works with expect where the first level of the chain is already stubbed" do
        o = Object.new
        allow(o).to receive(:foo).and_return(double)
        expect(o).to receive_message_chain(:foo, :bar, :baz)

        o.foo.bar.baz
      end

      it "works with allow where the first level of the chain is already stubbed" do
        o = Object.new
        allow(o).to receive(:foo).and_return(double)
        allow(o).to receive_message_chain(:foo, :bar, :baz).and_return(3)

        expect(o.foo.bar.baz).to eq(3)
      end

      it "provides a matcher description (when passing a string)" do
        matcher = receive_message_chain("foo.bar.bazz")
        expect(matcher.description).to eq("receive message chain foo.bar.bazz")
      end

      it "provides a matcher description (when passing symbols)" do
        matcher = receive_message_chain(:foo, :bar, :bazz)
        expect(matcher.description).to eq("receive message chain foo.bar.bazz")
      end

      it "provides a matcher description (when passing symbols and a hash)" do
        matcher = receive_message_chain(:foo, :bar, :bazz => 3)
        expect(matcher.description).to eq("receive message chain foo.bar.bazz")
      end
    end

    context "when the expect and should syntaxes are enabled" do
      include_context "with syntax", [:expect, :should]

      it "stubs the message correctly" do
        allow(object).to receive_message_chain(:to_a, :length)

        expect { object.to_a.length }.not_to raise_error
      end
    end
  end
end