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
|
require 'spec_helper'
describe Bogus::HaveReceivedMatcher do
let(:verifies_stub_definition) { stub(verify!: nil) }
let(:records_double_interactions) { stub(record: nil) }
let(:have_received_matcher) { isolate(Bogus::HaveReceivedMatcher) }
let(:have_received) { have_received_matcher.method_call }
let(:fake) { Samples::FooFake.new }
before do
fake.foo("a", "b")
end
shared_examples_for "have_received_matcher" do
it "matches when the spy has received the message" do
expect(fake).to have_received(:foo, "a", "b")
end
it "does not match if the spy hasn't received the message" do
expect(fake).not_to have_received(:foo, "a", "c")
end
it "verifies that the method call has the right signature" do
mock(verifies_stub_definition).verify!(fake, :foo, ["a", "b"])
have_received(:foo, "a", "b")
have_received_matcher.matches?(fake)
end
it "records the interaction so that it can be checked by contract tests" do
mock(records_double_interactions).record(fake, :foo, ["a", "b"])
have_received(:foo, "a", "b")
have_received_matcher.matches?(fake)
end
it "returns a readable error message for object with no shadow" do
have_received(:upcase)
expect(have_received_matcher.matches?("foo")).to be_false
expect(have_received_matcher.failure_message_for_should).to eq(Bogus::HaveReceivedMatcher::NO_SHADOW)
expect(have_received_matcher.failure_message).to eq(Bogus::HaveReceivedMatcher::NO_SHADOW)
expect(have_received_matcher.failure_message_for_should_not).to eq(Bogus::HaveReceivedMatcher::NO_SHADOW)
expect(have_received_matcher.failure_message_when_negated).to eq(Bogus::HaveReceivedMatcher::NO_SHADOW)
end
it "returns a readable error message for fakes" do
have_received(:foo, "a", "c")
have_received_matcher.matches?(fake)
message =
%Q{Expected #{fake.inspect} to have received foo("a", "c"), but it didn't.\n\n}+
%Q{The recorded interactions were:\n} +
%Q{ - foo("a", "b")\n}
expect(have_received_matcher.failure_message_for_should).to eq(message)
expect(have_received_matcher.failure_message).to eq(message)
end
end
context "with method_missing builder" do
def have_received(method, *args)
have_received_matcher.build.__send__(method, *args)
end
include_examples "have_received_matcher"
end
context "with method call builder" do
def have_received(*args)
have_received_matcher.build(*args)
end
include_examples "have_received_matcher"
end
end
|