verifyInOrder top-level property Null safety

List<VerificationResult> Function<T>(List<T Function()> recordedInvocations) 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:

  1. a list containing the argument passed to eatFood in the first verified eatFood call,
  2. an empty list, as nothing was captured in the verified chew call,
  3. a list containing the argument passed to eatFood in the second verified eatFood call.

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()> _) {
    for (final invocation in _) {
      try {
        invocation();
      } catch (_) {
        if (_ 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;
      } 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 (var call in matchedCalls) {
      call.verified = true;
    }
    return verificationResults;
  };
}