noSuchMethod method

  1. @override
  2. @visibleForTesting
dynamic noSuchMethod (
  1. Invocation invocation,
  2. [Object returnValue]
)

Handles method stubbing, method call verification, and real method calls.

If passed, returnValue will be returned during method stubbing and method call verification. This is useful in cases where the method invocation which led to noSuchMethod being called has a non-nullable return type.

Implementation

@override
@visibleForTesting
dynamic noSuchMethod(Invocation invocation, [Object /*?*/ returnValue]) {
  // noSuchMethod is that 'magic' that allows us to ignore implementing fields
  // and methods and instead define them later at compile-time per instance.
  invocation = _useMatchedInvocationIfSet(invocation);
  if (_whenInProgress) {
    _whenCall = _WhenCall(this, invocation);
    return returnValue;
  } else if (_verificationInProgress) {
    _verifyCalls.add(_VerifyCall(this, invocation));
    return returnValue;
  } else if (_untilCalledInProgress) {
    _untilCall = _UntilCall(this, invocation);
    return returnValue;
  } else {
    _realCalls.add(RealCall(this, invocation));
    _invocationStreamController.add(invocation);
    var cannedResponse = _responses.lastWhere(
        (cr) => cr.call.matches(invocation, {}),
        orElse: _defaultResponse);
    var response = cannedResponse.response(invocation);
    return response;
  }
}