verifyInOrder top-level property

void Function<T>(List<T> recordedInvocations) verifyInOrder

Verify 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", soundwas called with no arguments, andeatFood` was then called with some argument.

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

_InOrderVerification get verifyInOrder {
  if (_verifyCalls.isNotEmpty) {
    throw StateError(_verifyCalls.join());
  }
  _verificationInProgress = true;
  return <T>(List<T> _) {
    _verificationInProgress = false;
    var dt = DateTime.fromMillisecondsSinceEpoch(0);
    var tmpVerifyCalls = List<_VerifyCall>.from(_verifyCalls);
    _verifyCalls.clear();
    var matchedCalls = <RealCall>[];
    for (var verifyCall in tmpVerifyCalls) {
      try {
        var matched = verifyCall._findAfter(dt);
        matchedCalls.add(matched);
        dt = matched.timeStamp;
      } on StateError {
        var mocks = tmpVerifyCalls.map((vc) => vc.mock).toSet();
        var allInvocations =
            mocks.expand((m) => m._realCalls).toList(growable: false);
        allInvocations
            .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;
    }
  };
}