noSuchMethod method

  1. @override
  2. @visibleForTesting
dynamic noSuchMethod(
  1. Invocation invocation, {
  2. Object? returnValue,
  3. Object? returnValueForMissingStub = deferToDefaultResponse,
})
override

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,
    Object? returnValueForMissingStub = deferToDefaultResponse}) {
  // 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 {
    _ReturnsCannedResponse defaultResponse;
    if (returnValueForMissingStub == deferToDefaultResponse) {
      defaultResponse = _defaultResponse;
    } else {
      defaultResponse = () =>
          CallPair<Object?>.allInvocations((_) => returnValueForMissingStub);
    }
    _realCalls.add(RealCall(this, invocation));
    _invocationStreamController.add(invocation);
    final cannedResponse = _responses.lastWhere(
        (cr) => cr.call.matches(invocation, {}),
        orElse: defaultResponse);
    final response = cannedResponse.response(invocation);
    return response;
  }
}