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
|
module RSpec
module Mocks
RSpec.describe "at_most" do
before(:each) do
@double = double
end
it "passes when at_most(n) is called exactly 1 time" do
expect(@double).to receive(:do_something).at_most(1).time
@double.do_something
verify @double
end
it "passes when at_most(n) is called exactly n times" do
expect(@double).to receive(:do_something).at_most(2).times
@double.do_something
@double.do_something
verify @double
end
it "passes when at_most(n) is called less than n times" do
expect(@double).to receive(:do_something).at_most(2).times
@double.do_something
verify @double
end
it "passes when at_most(n) is never called" do
expect(@double).to receive(:do_something).at_most(2).times
verify @double
end
it "passes when at_most(:once) is called once" do
expect(@double).to receive(:do_something).at_most(:once)
@double.do_something
verify @double
end
it "passes when at_most(:once) is never called" do
expect(@double).to receive(:do_something).at_most(:once)
verify @double
end
it "passes when at_most(:twice) is called once" do
expect(@double).to receive(:do_something).at_most(:twice)
@double.do_something
verify @double
end
it "passes when at_most(:twice) is called twice" do
expect(@double).to receive(:do_something).at_most(:twice)
@double.do_something
@double.do_something
verify @double
end
it "passes when at_most(:twice) is never called" do
expect(@double).to receive(:do_something).at_most(:twice)
verify @double
end
it "passes when at_most(:thrice) is called less than three times" do
expect(@double).to receive(:do_something).at_most(:thrice)
@double.do_something
verify @double
end
it "passes when at_most(:thrice) is called thrice" do
expect(@double).to receive(:do_something).at_most(:thrice)
@double.do_something
@double.do_something
@double.do_something
verify @double
end
it "returns the value given by a block when at_most(:once) method is called" do
expect(@double).to receive(:to_s).at_most(:once) { "testing" }
expect(@double.to_s).to eq "testing"
verify @double
end
it "fails fast when at_most(n) times method is called n plus 1 times" do
expect(@double).to receive(:do_something).at_most(2).times
@double.do_something
@double.do_something
expect_fast_failure_from(@double, /expected: at most 2 times.*received: 3 times/m) do
@double.do_something
end
end
it "fails fast when at_most(n) times method is called n plus 1 time" do
expect(@double).to receive(:do_something).at_most(1).time
@double.do_something
expect_fast_failure_from(@double, /expected: at most 1 time.*received: 2 times/m) do
@double.do_something
end
end
it "fails fast when at_most(:once) and is called twice" do
expect(@double).to receive(:do_something).at_most(:once)
@double.do_something
expect_fast_failure_from(@double, /expected: at most 1 time.*received: 2 times/m) do
@double.do_something
end
end
it "fails fast when at_most(:twice) and is called three times" do
expect(@double).to receive(:do_something).at_most(:twice)
@double.do_something
@double.do_something
expect_fast_failure_from(@double, /expected: at most 2 times.*received: 3 times/m) do
@double.do_something
end
end
it "fails fast when at_most(:thrice) and is called four times" do
expect(@double).to receive(:do_something).at_most(:thrice)
@double.do_something
@double.do_something
@double.do_something
expect_fast_failure_from(@double, /expected: at most 3 times.*received: 4 times/m) do
@double.do_something
end
end
context "when called with negative expectation" do
it "raises an error" do
expect {
expect(@double).not_to receive(:do_something).at_most(:thrice)
}.to raise_error(/`count` is not supported with negative message expectations/)
end
end
end
end
end
|