Allow specifying receive count

Updated checks and failure output to support this.
This commit is contained in:
Michael Miller 2019-11-03 13:22:00 -07:00
parent b107511c9e
commit 2048267eef

View file

@ -3,7 +3,7 @@ require "./standard_matcher"
module Spectator::Matchers
struct ReceiveMatcher < StandardMatcher
def initialize(@expected : TestExpression(Symbol))
def initialize(@expected : TestExpression(Symbol), @count : Int32? = nil)
end
def description : String
@ -13,17 +13,23 @@ module Spectator::Matchers
def match?(actual : TestExpression(T)) : Bool forall T
double = actual.value.as(Mocks::Double)
calls = double.spectator_stub_calls(@expected.value)
if (count = @count)
count == calls.size
else
!calls.empty?
end
end
def failure_message(actual : TestExpression(T)) : String forall T
"#{actual.label} did not receive #{@expected.label}"
end
def values(actual : TestExpression(T)) forall T
double = actual.value.as(Mocks::Double)
calls = double.spectator_stub_calls(@expected.value)
{
expected: "1 time with any arguments",
received: "0 times with any arguments",
expected: "#{@count ? "#{@count} time(s)" : "At least once"} with any arguments",
received: "#{calls.size} time(s) with any arguments",
}
end
end