verifyInOrder top-level property
List<VerificationResult> Function<T>(List<T Function()> recordedInvocations)
get
verifyInOrder
Verifies that a list of methods on a mock object have been called with the given arguments. For example:
verifyInOrder([
() => cat.eatFood("Milk"),
() => cat.sound(),
() => cat.eatFood(any),
]);
This verifies that eatFood was called with "Milk", sound was called
with no arguments, and eatFood was then called with some argument.
Returns a list of verification results, one for each call which was verified.
For example, if verifyInOrder is given these calls to verify:
final verification = verifyInOrder([
() => cat.eatFood(captureAny),
() => cat.chew(),
() => cat.eatFood(captureAny),
]);
then verification is a list which contains a captured getter which
returns three lists:
- a list containing the argument passed to
eatFoodin the first verifiedeatFoodcall, - an empty list, as nothing was captured in the verified
chewcall, - a list containing the argument passed to
eatFoodin the second verifiedeatFoodcall.
Note: verifyInOrder only verifies that each call was made in the order
given, but not that those were the only calls. In the example above, if
other calls were made to eatFood or sound between the three given
calls, or before or after them, the verification will still succeed.
Implementation
List<VerificationResult> Function<T>(
List<T Function()> recordedInvocations,
) get verifyInOrder {
if (_verifyCalls.isNotEmpty) {
throw StateError(_verifyCalls.join());
}
_verificationInProgress = true;
return <T>(List<T Function()> invocations) {
for (final invocation in invocations) {
try {
invocation();
} catch (e) {
if (e is! TypeError) rethrow;
}
}
_verificationInProgress = false;
final verificationResults = <VerificationResult>[];
final tmpVerifyCalls = List<_VerifyCall>.from(_verifyCalls);
var time = DateTime.fromMillisecondsSinceEpoch(0);
_verifyCalls.clear();
final matchedCalls = <RealCall>[];
for (final verifyCall in tmpVerifyCalls) {
try {
final matched = verifyCall._findAfter(time);
matchedCalls.add(matched.realCall);
verificationResults.add(VerificationResult._(1, matched.capturedArgs));
time = matched.realCall.timeStamp;
// ignore: avoid_catching_errors
} on StateError {
final mocks = tmpVerifyCalls.map((vc) => vc.mock).toSet();
final allInvocations = mocks
.expand((m) => m._realCalls)
.toList(growable: false)
..sort((inv1, inv2) => inv1.timeStamp.compareTo(inv2.timeStamp));
var otherCalls = '';
if (allInvocations.isNotEmpty) {
otherCalls = " All calls: ${allInvocations.join(", ")}";
}
fail(
'Matching call #${tmpVerifyCalls.indexOf(verifyCall)} '
'not found.$otherCalls',
);
}
}
for (final call in matchedCalls) {
call.verified = true;
}
return verificationResults;
};
}