File: combining_implementation_instructions_spec.rb

package info (click to toggle)
ruby-rspec-mocks 2.14.5-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 868 kB
  • ctags: 725
  • sloc: ruby: 8,227; makefile: 4
file content (205 lines) | stat: -rw-r--r-- 6,374 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
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
require 'spec_helper'

module RSpec
  module Mocks
    describe "Combining implementation instructions" do
      it 'can combine and_yield and and_return' do
        dbl = double
        dbl.stub(:foo).and_yield(5).and_return(3)

        expect { |b|
          expect(dbl.foo(&b)).to eq(3)
        }.to yield_with_args(5)
      end

      describe "combining and_yield, a block implementation and and_return" do
        def verify_combined_implementation
          dbl = double
          (yield dbl).and_yield(5).and_return(3)

          expect { |b|
            expect(dbl.foo(:arg, &b)).to eq(3)
          }.to yield_with_args(5)

          expect(@block_called).to be_true
        end

        it 'works when passing a block to `stub`' do
          verify_combined_implementation do |dbl|
            dbl.stub(:foo) { @block_called = true }
          end
        end

        it 'works when passing a block to `with`' do
          verify_combined_implementation do |dbl|
            dbl.stub(:foo).with(:arg) { @block_called = true }
          end
        end

        it 'works when passing a block to `exactly`' do
          verify_combined_implementation do |dbl|
            dbl.should_receive(:foo).exactly(:once) { @block_called = true }
          end
        end

        it 'works when passing a block to `at_least`' do
          verify_combined_implementation do |dbl|
            dbl.should_receive(:foo).at_least(:once) { @block_called = true }
          end
        end

        it 'works when passing a block to `at_most`' do
          verify_combined_implementation do |dbl|
            dbl.should_receive(:foo).at_most(:once) { @block_called = true }
          end
        end

        it 'works when passing a block to `times`' do
          verify_combined_implementation do |dbl|
            dbl.should_receive(:foo).exactly(1).times { @block_called = true }
          end
        end

        it 'works when passing a block to `any_number_of_times`' do
          verify_combined_implementation do |dbl|
            dbl.should_receive(:foo).any_number_of_times { @block_called = true }
          end
        end

        it 'works when passing a block to `once`' do
          verify_combined_implementation do |dbl|
            dbl.should_receive(:foo).once { @block_called = true }
          end
        end

        it 'works when passing a block to `twice`' do
          the_double = nil

          verify_combined_implementation do |dbl|
            the_double = dbl
            dbl.should_receive(:foo).twice { @block_called = true }
          end

          the_double.foo { |a| } # to ensure it is called twice
        end

        it 'works when passing a block to `ordered`' do
          verify_combined_implementation do |dbl|
            dbl.should_receive(:foo).ordered { @block_called = true }
          end
        end
      end

      it 'can combine and_yield and and_return with a block' do
        dbl = double
        dbl.stub(:foo).and_yield(5).and_return { :return }

        expect { |b|
          expect(dbl.foo(&b)).to eq(:return)
        }.to yield_with_args(5)
      end

      it 'can combine and_yield and and_raise' do
        dbl = double
        dbl.stub(:foo).and_yield(5).and_raise("boom")

        expect { |b|
          expect { dbl.foo(&b) }.to raise_error("boom")
        }.to yield_with_args(5)
      end

      it 'can combine and_yield, a block implementation and and_raise' do
        dbl = double
        block_called = false
        dbl.stub(:foo) { block_called = true }.and_yield(5).and_raise("boom")

        expect { |b|
          expect { dbl.foo(&b) }.to raise_error("boom")
        }.to yield_with_args(5)

        expect(block_called).to be_true
      end

      it 'can combine and_yield and and_throw' do
        dbl = double
        dbl.stub(:foo).and_yield(5).and_throw(:bar)

        expect { |b|
          expect { dbl.foo(&b) }.to throw_symbol(:bar)
        }.to yield_with_args(5)
      end

      it 'can combine and_yield, a block implementation and and_throw' do
        dbl = double
        block_called = false
        dbl.stub(:foo) { block_called = true }.and_yield(5).and_throw(:bar)

        expect { |b|
          expect { dbl.foo(&b) }.to throw_symbol(:bar)
        }.to yield_with_args(5)

        expect(block_called).to be_true
      end

      it 'returns `nil` from all terminal actions to discourage further configuration' do
        expect(double.stub(:foo).and_return(1)).to be_nil
        expect(double.stub(:foo).and_raise("boom")).to be_nil
        expect(double.stub(:foo).and_throw(:foo)).to be_nil
      end

      it 'allows the terminal action to be overriden' do
        dbl = double
        stubbed_double = dbl.stub(:foo)

        stubbed_double.and_return(1)
        expect(dbl.foo).to eq(1)

        stubbed_double.and_return(3)
        expect(dbl.foo).to eq(3)

        stubbed_double.and_raise("boom")
        expect { dbl.foo }.to raise_error("boom")

        stubbed_double.and_throw(:bar)
        expect { dbl.foo }.to throw_symbol(:bar)
      end

      it 'allows the inner implementation block to be overriden' do
        dbl = double
        stubbed_double = dbl.stub(:foo)

        stubbed_double.with(:arg) { :with_block }
        expect(dbl.foo(:arg)).to eq(:with_block)

        stubbed_double.at_least(:once) { :at_least_block }
        expect(dbl.foo(:arg)).to eq(:at_least_block)
      end

      it 'can combine and_call_original, with, and_return' do
        obj = Struct.new(:value).new('original')
        obj.stub(:value).and_call_original
        obj.stub(:value).with(:arg).and_return('value')
        expect(obj.value).to eq 'original'
        expect(obj.value(:arg)).to eq 'value'
      end

      it 'raises an error if `and_call_original` is followed by any other instructions' do
        dbl = [1, 2, 3]
        stubbed = dbl.stub(:size)
        stubbed.and_call_original

        msg_fragment = /cannot be modified further/

        expect { stubbed.and_yield }.to raise_error(msg_fragment)
        expect { stubbed.and_return(1) }.to raise_error(msg_fragment)
        expect { stubbed.and_raise("a") }.to raise_error(msg_fragment)
        expect { stubbed.and_throw(:bar) }.to raise_error(msg_fragment)

        expect { stubbed.once { } }.to raise_error(msg_fragment)

        expect(dbl.size).to eq(3)
      end
    end
  end
end