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
|
Feature: Receive Counts
When [expecting a message](../basics/expecting-messages), you can specify how many times you expect the message to be
received:
* `expect(...).to receive(...).once`
* `expect(...).to receive(...).twice`
* `expect(...).to receive(...).exactly(n).time`
* `expect(...).to receive(...).exactly(n).times`
* `expect(...).to receive(...).at_least(:once)`
* `expect(...).to receive(...).at_least(:twice)`
* `expect(...).to receive(...).at_least(n).time`
* `expect(...).to receive(...).at_least(n).times`
* `expect(...).to receive(...).at_most(:once)`
* `expect(...).to receive(...).at_most(:twice)`
* `expect(...).to receive(...).at_most(n).time`
* `expect(...).to receive(...).at_most(n).times`
If you don't specify an expected receive count, it defaults to `once`.
Background:
Given a file named "lib/account.rb" with:
"""ruby
class Account
def initialize(logger)
@logger = logger
end
def open
@logger.account_opened
end
end
"""
Scenario: Passing examples
Given a file named "spec/account_spec.rb" with:
"""ruby
require 'account'
RSpec.describe Account do
let(:logger) { double("Logger") }
let(:account) { Account.new(logger) }
example "once" do
expect(logger).to receive(:account_opened).once
account.open
end
example "twice" do
expect(logger).to receive(:account_opened).twice
account.open
account.open
end
example "exactly(n).time" do
expect(logger).to receive(:account_opened).exactly(1).time
account.open
end
example "exactly(n).times" do
expect(logger).to receive(:account_opened).exactly(3).times
account.open
account.open
account.open
end
example "at_least(:once)" do
expect(logger).to receive(:account_opened).at_least(:once)
account.open
account.open
end
example "at_least(:twice)" do
expect(logger).to receive(:account_opened).at_least(:twice)
account.open
account.open
account.open
end
example "at_least(n).time" do
expect(logger).to receive(:account_opened).at_least(1).time
account.open
end
example "at_least(n).times" do
expect(logger).to receive(:account_opened).at_least(3).times
account.open
account.open
account.open
account.open
end
example "at_most(:once)" do
expect(logger).to receive(:account_opened).at_most(:once)
end
example "at_most(:twice)" do
expect(logger).to receive(:account_opened).at_most(:twice)
account.open
end
example "at_most(n).time" do
expect(logger).to receive(:account_opened).at_most(1).time
account.open
end
example "at_most(n).times" do
expect(logger).to receive(:account_opened).at_most(3).times
account.open
account.open
end
end
"""
When I run `rspec spec/account_spec.rb`
Then the examples should all pass
Scenario: Failing examples
Given a file named "spec/account_spec.rb" with:
"""ruby
require 'account'
RSpec.describe Account do
let(:logger) { double("Logger") }
let(:account) { Account.new(logger) }
example "once" do
expect(logger).to receive(:account_opened).once
account.open
account.open
end
example "twice" do
expect(logger).to receive(:account_opened).twice
account.open
end
example "exactly(n).times" do
expect(logger).to receive(:account_opened).exactly(3).times
account.open
account.open
end
example "at_least(:once)" do
expect(logger).to receive(:account_opened).at_least(:once)
end
example "at_least(:twice)" do
expect(logger).to receive(:account_opened).at_least(:twice)
account.open
end
example "at_least(n).times" do
expect(logger).to receive(:account_opened).at_least(3).times
account.open
account.open
end
example "at_most(:once)" do
expect(logger).to receive(:account_opened).at_most(:once)
account.open
account.open
end
example "at_most(:twice)" do
expect(logger).to receive(:account_opened).at_most(:twice)
account.open
account.open
account.open
end
example "at_most(n).times" do
expect(logger).to receive(:account_opened).at_most(3).times
account.open
account.open
account.open
account.open
end
end
"""
When I run `rspec spec/account_spec.rb --order defined`
Then the examples should all fail, producing the following output:
| expected: 1 time with any arguments |
| received: 2 times |
| |
| expected: 2 times with any arguments |
| received: 1 time with any arguments |
| |
| expected: 3 times with any arguments |
| received: 2 times with any arguments |
| |
| expected: at least 1 time with any arguments |
| received: 0 times with any arguments |
| |
| expected: at least 2 times with any arguments |
| received: 1 time with any arguments |
| |
| expected: at least 3 times with any arguments |
| received: 2 times with any arguments |
| |
| expected: at most 1 time with any arguments |
| received: 2 times |
| |
| expected: at most 2 times with any arguments |
| received: 3 times |
| |
| expected: at most 3 times with any arguments |
| received: 4 times |
|