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
|
RSpec.describe "expection set on previously stubbed method" do
it "fails if message is not received after expectation is set" do
dbl = double(:msg => nil)
dbl.msg
expect(dbl).to receive(:msg)
expect { verify dbl }.to fail
end
it "outputs arguments of similar calls" do
dbl = double('double', :foo => true)
expect(dbl).to receive(:foo).with('first')
dbl.foo('second')
dbl.foo('third')
expect {
verify dbl
}.to raise_error(
RSpec::Mocks::MockExpectationError,
a_string_including(
%Q(#<Double "double"> received :foo with unexpected arguments),
"expected: (\"first\")",
"got:", "(\"second\")",
"(\"third\")"))
reset dbl
end
it 'handles concurrent validation of expectations' do
dbl = double('double', :foo => true)
concurrency = 4
repetition = 10
expect(dbl).to receive(:foo).with(anything).exactly(concurrency * repetition).times
concurrency.times.map do |thread|
Thread.new do
repetition.times do |index|
dbl.foo("#{thread}-#{index}")
end
end
end.map(&:join)
verify dbl
end
it 'indicates the site of expectation in the stacktrace when outputing arguments of similar calls' do
dbl = double('double', :foo => true)
expect(dbl).to receive(:foo).with('first'); line = __LINE__
dbl.foo('second')
dbl.foo('third')
expect {
verify dbl
}.to raise_error(an_object_having_attributes(
:backtrace => a_collection_starting_with(
a_string_including("#{__FILE__}:#{line}")
)
))
end
context "with argument constraint on stub" do
it "matches any args if no arg constraint set on expectation" do
dbl = double("mock")
allow(dbl).to receive(:foo).with(3).and_return("stub")
expect(dbl).to receive(:foo).at_least(:once).and_return("expectation")
dbl.foo
verify dbl
end
it "matches specific args set on expectation" do
dbl = double("mock")
allow(dbl).to receive(:foo).with(3).and_return("stub")
expect(dbl).to receive(:foo).at_least(:once).with(4).and_return("expectation")
dbl.foo(4)
verify dbl
end
it "fails if expectation's arg constraint is not met" do
dbl = double("mock")
allow(dbl).to receive(:foo).with(3).and_return("stub")
expect(dbl).to receive(:foo).at_least(:once).with(4).and_return("expectation")
dbl.foo(3)
expect { verify dbl }.to raise_error(/expected: \(4\)\s+got: \(3\)/)
end
it 'distinguishes between individual values and arrays properly' do
dbl = double
allow(dbl).to receive(:foo).with('a', ['b'])
expect {
dbl.foo(['a'], 'b')
}.to raise_error { |e|
expect(e.message).to include('expected: ("a", ["b"])', 'got: (["a"], "b")')
}
end
it 'distinguishes between duplicate individual values and arrays properly' do
dbl = double
allow(dbl).to receive(:foo).with('a', ['b'], 'b')
expect {
dbl.foo(['a'], 'b', 'b')
}.to raise_error { |e|
expect(e.message).to include('expected: ("a", ["b"], "b")', 'got: (["a"], "b", "b")')
}
end
end
end
|