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
|
module RSpec
module Mocks
RSpec.describe "a message expectation with multiple invoke handlers and no specified count" do
let(:a_double) { double }
before(:each) do
expect(a_double).to receive(:do_something).and_invoke(lambda { 1 }, lambda { raise "2" }, lambda { 3 })
end
it "invokes procs in order" do
expect(a_double.do_something).to eq 1
expect { a_double.do_something }.to raise_error("2")
expect(a_double.do_something).to eq 3
verify a_double
end
it "falls back to a previously stubbed value" do
allow(a_double).to receive_messages :do_something => :stub_result
expect(a_double.do_something).to eq 1
expect { a_double.do_something }.to raise_error("2")
expect(a_double.do_something).to eq 3
expect(a_double.do_something).to eq :stub_result
end
it "fails when there are too few calls (if there is no stub)" do
a_double.do_something
expect { a_double.do_something }.to raise_error("2")
expect { verify a_double }.to fail
end
it "fails when there are too many calls (if there is no stub)" do
a_double.do_something
expect { a_double.do_something }.to raise_error("2")
a_double.do_something
a_double.do_something
expect { verify a_double }.to fail
end
end
RSpec.describe "a message expectation with multiple invoke handlers with a specified count equal to the number of values" do
let(:a_double) { double }
before(:each) do
expect(a_double).to receive(:do_something).exactly(3).times.and_invoke(lambda { 1 }, lambda { raise "2" }, lambda { 3 })
end
it "returns values in order to consecutive calls" do
expect(a_double.do_something).to eq 1
expect { a_double.do_something }.to raise_error("2")
expect(a_double.do_something).to eq 3
verify a_double
end
end
RSpec.describe "a message expectation with multiple invoke handlers specifying at_least less than the number of values" do
let(:a_double) { double }
before { expect(a_double).to receive(:do_something).at_least(:twice).with(no_args).and_invoke(lambda { 11 }, lambda { 22 }) }
it "uses the last return value for subsequent calls" do
expect(a_double.do_something).to equal(11)
expect(a_double.do_something).to equal(22)
expect(a_double.do_something).to equal(22)
verify a_double
end
it "fails when called less than the specified number" do
expect(a_double.do_something).to equal(11)
expect { verify a_double }.to fail
end
context "when method is stubbed too" do
before { allow(a_double).to receive(:do_something).and_invoke lambda { :stub_result } }
it "uses the last value for subsequent calls" do
expect(a_double.do_something).to equal(11)
expect(a_double.do_something).to equal(22)
expect(a_double.do_something).to equal(22)
verify a_double
end
it "fails when called less than the specified number" do
expect(a_double.do_something).to equal(11)
expect { verify a_double }.to fail
end
end
end
RSpec.describe "a message expectation with multiple invoke handlers with a specified count larger than the number of values" do
let(:a_double) { double }
before { expect(a_double).to receive(:do_something).exactly(3).times.and_invoke(lambda { 11 }, lambda { 22 }) }
it "uses the last return value for subsequent calls" do
expect(a_double.do_something).to equal(11)
expect(a_double.do_something).to equal(22)
expect(a_double.do_something).to equal(22)
verify a_double
end
it "fails when called less than the specified number" do
a_double.do_something
a_double.do_something
expect { verify a_double }.to fail
end
it "fails fast when called greater than the specified number" do
a_double.do_something
a_double.do_something
a_double.do_something
expect_fast_failure_from(a_double) do
a_double.do_something
end
end
end
end
end
|