File: at_least_spec.rb

package info (click to toggle)
ruby-rspec 3.9.0c2e2m1s3-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 6,612 kB
  • sloc: ruby: 67,456; sh: 1,572; makefile: 98
file content (155 lines) | stat: -rw-r--r-- 5,265 bytes parent folder | download | duplicates (4)
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
module RSpec
  module Mocks
    RSpec.describe "at_least" do
      before(:each) { @double = double }

      it "fails if method is never called" do
        expect(@double).to receive(:do_something).at_least(4).times
        expect {
          verify @double
        }.to raise_error(/expected: at least 4 times.*received: 0 times/m)
      end

      it "fails when called less than n times" do
        expect(@double).to receive(:do_something).at_least(4).times
        @double.do_something
        @double.do_something
        @double.do_something
        expect {
          verify @double
        }.to raise_error(/expected: at least 4 times.*received: 3 times/m)
      end

      it "fails when at least once method is never called" do
        expect(@double).to receive(:do_something).at_least(:once)
        expect {
          verify @double
        }.to raise_error(/expected: at least 1 time.*received: 0 times/m)
      end

      it "fails when at least twice method is called once" do
        expect(@double).to receive(:do_something).at_least(:twice)
        @double.do_something
        expect {
          verify @double
        }.to raise_error(/expected: at least 2 times.*received: 1 time/m)
      end

      it "fails when at least twice method is never called" do
        expect(@double).to receive(:do_something).at_least(:twice)
        expect {
          verify @double
        }.to raise_error(/expected: at least 2 times.*received: 0 times/m)
      end

      it "fails when at least thrice method is called less than three times" do
        expect(@double).to receive(:do_something).at_least(:thrice)
        @double.do_something
        @double.do_something
        expect {
          verify @double
        }.to raise_error(/expected: at least 3 times.*received: 2 times/m)
      end

      it "passes when at least n times method is called exactly n times" do
        expect(@double).to receive(:do_something).at_least(4).times
        @double.do_something
        @double.do_something
        @double.do_something
        @double.do_something
        verify @double
      end

      it "passes when at least n times method is called n plus 1 times" do
        expect(@double).to receive(:do_something).at_least(4).times
        @double.do_something
        @double.do_something
        @double.do_something
        @double.do_something
        @double.do_something
        verify @double
      end

      it "passes when at least once method is called once" do
        expect(@double).to receive(:do_something).at_least(:once)
        @double.do_something
        verify @double
      end

      it "passes when at least once method is called twice" do
        expect(@double).to receive(:do_something).at_least(:once)
        @double.do_something
        @double.do_something
        verify @double
      end

      it "passes when at least twice method is called three times" do
        expect(@double).to receive(:do_something).at_least(:twice)
        @double.do_something
        @double.do_something
        @double.do_something
        verify @double
      end

      it "passes when at least twice method is called twice" do
        expect(@double).to receive(:do_something).at_least(:twice)
        @double.do_something
        @double.do_something
        verify @double
      end

      it "passes when at least thrice method is called three times" do
        expect(@double).to receive(:do_something).at_least(:thrice)
        @double.do_something
        @double.do_something
        @double.do_something
        verify @double
      end

      it "passes when at least thrice method is called four times" do
        expect(@double).to receive(:do_something).at_least(:thrice)
        @double.do_something
        @double.do_something
        @double.do_something
        @double.do_something
        verify @double
      end

      it "returns the value given by a block when the at least once method is called" do
        expect(@double).to receive(:to_s).at_least(:once) { "testing" }
        expect(@double.to_s).to eq "testing"
        verify @double
      end

      context "when sent with 0" do
        it "outputs a removal message" do
          expect {
            expect(@double).to receive(:do_something).at_least(0).times
          }.to raise_error(/has been removed/)
        end
      end

      it "uses a stub value if no value set" do
        allow(@double).to receive_messages(:do_something => 'foo')
        expect(@double).to receive(:do_something).at_least(:once)
        expect(@double.do_something).to eq 'foo'
        expect(@double.do_something).to eq 'foo'
      end

      it "prefers its own return value over a stub" do
        allow(@double).to receive_messages(:do_something => 'foo')
        expect(@double).to receive(:do_something).at_least(:once).and_return('bar')
        expect(@double.do_something).to eq 'bar'
        expect(@double.do_something).to eq 'bar'
      end

      context "when called with negative expectation" do
        it "raises an error" do
          expect {
            expect(@double).not_to receive(:do_something).at_least(:thrice)
          }.to raise_error(/`count` is not supported with negative message expectations/)
        end
      end
    end
  end
end