debugReplaceApis function

void Function() debugReplaceApis({
  1. NfcHostApi? nfc,
  2. NfcAndroidHostApi? android,
  3. NfcIosHostApi? ios,
})

Replaces the host APIs with fakes, and returns a function that puts the real ones back.

late void Function() restore;
setUp(() => restore = debugReplaceApis(nfc: FakeNfcHostApi()));
tearDown(() => restore());

Deliberately not @visibleForTesting. That annotation confines a member to files the analyzer recognises as tests, and this one also has to be reachable from an integration_test/ directory and from the shared fixtures a package keeps under lib/ -- both of which would be errors under it. package:nfc_util/testing.dart is the boundary instead, and it says in its own documentation what it is for.

Implementation

void Function() debugReplaceApis({NfcHostApi? nfc, NfcAndroidHostApi? android, NfcIosHostApi? ios}) {
  final previousNfc = nfcApi;
  final previousAndroid = androidApi;
  final previousIos = iosApi;

  if (nfc != null) nfcApi = nfc;
  if (android != null) androidApi = android;
  if (ios != null) iosApi = ios;

  return () {
    nfcApi = previousNfc;
    androidApi = previousAndroid;
    iosApi = previousIos;
  };
}