File: and_invoke_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 (45 lines) | stat: -rw-r--r-- 1,487 bytes parent folder | download | duplicates (2)
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
module RSpec
  module Mocks
    RSpec.describe 'and_invoke' do
      let(:obj) { double('obj') }

      context 'when a block is passed' do
        it 'raises ArgumentError' do
          expect {
            allow(obj).to receive(:foo).and_invoke('bar') { 'baz' }
          }.to raise_error(ArgumentError, /implementation block/i)
        end
      end

      context 'when no argument is passed' do
        it 'raises ArgumentError' do
          expect { allow(obj).to receive(:foo).and_invoke }.to raise_error(ArgumentError)
        end
      end

      context 'when a non-callable are passed in any position' do
        let(:non_callable) { nil }
        let(:callable) { lambda { nil } }

        it 'raises ArgumentError' do
          error = [ArgumentError, "Arguments to `and_invoke` must be callable."]

          expect { allow(obj).to receive(:foo).and_invoke(non_callable) }.to raise_error(*error)
          expect { allow(obj).to receive(:foo).and_invoke(callable, non_callable) }.to raise_error(*error)
        end
      end

      context 'when calling passed callables' do
        let(:dbl) { double }

        it 'passes the arguments into the callable' do
          expect(dbl).to receive(:square_then_cube).and_invoke(lambda { |i| i ** 2 },
                                                               lambda { |i| i ** 3 })

          expect(dbl.square_then_cube(2)).to eq 4
          expect(dbl.square_then_cube(2)).to eq 8
        end
      end
    end
  end
end