File: at_least_spec.rb

package info (click to toggle)
ruby-rspec-mocks 2.14.3-1~bpo70%2B1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy-backports
  • size: 868 kB
  • sloc: ruby: 8,131; makefile: 4
file content (151 lines) | stat: -rw-r--r-- 5,041 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
require 'spec_helper'

module RSpec
  module Mocks
    describe "at_least" do
      before(:each) { @double = double }

      it "fails if method is never called" do
        @double.should_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
        @double.should_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
        @double.should_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
        @double.should_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
        @double.should_receive(:do_something).at_least(:twice)
        expect {
          verify @double
        }.to raise_error(/expected: at least 2 times.*received: 0 times/m)
      end

      it "passes when at least n times method is called exactly n times" do
        @double.should_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
        @double.should_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
        @double.should_receive(:do_something).at_least(:once)
        @double.do_something
        verify @double
      end

      it "passes when at least once method is called twice" do
        @double.should_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
        @double.should_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
        @double.should_receive(:do_something).at_least(:twice)
        @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
        @double.should_receive(:to_s).at_least(:once) { "testing" }
        expect(@double.to_s).to eq "testing"
        verify @double
      end

      context "when sent with 0" do
        before { RSpec.stub(:deprecate) }

        it "outputs a deprecation warning" do
          expect(RSpec).to receive(:deprecate).with("at_least\(0\) with should_receive", :replacement => "stub")
          expect(@double).to receive(:do_something).at_least(0).times
        end

        it "passes with no return if called once" do
          @double.should_receive(:do_something).at_least(0).times
          @double.do_something
        end

        it "passes with return block if called once" do
          @double.should_receive(:do_something).at_least(0).times { true }
          @double.do_something
        end

        it "passes with and_return if called once" do
          @double.should_receive(:do_something).at_least(0).times.and_return true
          @double.do_something
        end

        it "passes with no return if never called" do
          @double.should_receive(:do_something).at_least(0).times
        end

        it "passes with return block if never called" do
          @double.should_receive(:do_something).at_least(0).times { true }
        end

        it "passes with and_return if never called" do
          @double.should_receive(:do_something).at_least(0).times.and_return true
        end
      end

      it "uses a stub value if no value set" do
        @double.stub(:do_something => 'foo')
        @double.should_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
        @double.stub(:do_something => 'foo')
        @double.should_receive(:do_something).at_least(:once).and_return('bar')
        expect(@double.do_something).to eq 'bar'
        expect(@double.do_something).to eq 'bar'
      end
    end
  end
end