Flutter test Mocktail error : type 'Null' is not a subtype of type 'Future<Object?>'

Flutter test Mocktail error : type 'Null' is not a subtype of type 'Future<Object?>'

我想测试当一个按钮被点击 10 次时是否推送了一条路线。当被测试的widget调用Modular.to.pushNamed()方法时,抛出如下错误

The following _TypeError was thrown running a test:
type 'Null' is not a subtype of type 'Future<Object?>'

When the exception was thrown, this was the stack:
#0      ModularNavigateMock.pushNamed (package:flutter_modular/src/presenter/models/modular_navigator.dart:40:14)
#1      AgileSmellsState.goToResults (package:serious_agile_games/agile-smells/agile_smells.dart:37:16)
#2      AgileSmellsState.build.<anonymous closure> (package:serious_agile_games/agile-smells/agile_smells.dart:50:26)
#3      SwipingDeck.swipeRight (package:swiping_card_deck/swiping_card_deck.dart:119:38)
<asynchronous suspension>

AgileAmells widget 中抛出异常的调用是

void goToResults() {
    Modular.to.pushNamed(AgileSmellsResults.route, arguments: validatedSmells);
  }

其中 validatedSmells 不可为空 List.empty(growable: true)

我的测试是

testWidgets('Test validate smell 10 times', (tester) async {
      await tester.pumpWidget(TestModularApp(widgetToTest: AgileSmells(firestore: firestore)));

      await tester.pumpAndSettle();

      final deck = find.byType(SwipingDeck<SmellCard>);
      final validateButton = find.byIcon(Icons.check);

      expect(deck, findsOneWidget);
      expect(validateButton, findsOneWidget);

      await tester.tap(validateButton);
      await tester.pumpAndSettle();
      verifyNever(
        () => TestModularNavigation.mockedNavigator.pushNamed(AgileSmellsResults.route, arguments: any, forRoot: false),
      );

      for (int i = 1; i < 10; i++) {
        await tester.tap(validateButton);
        await tester.pumpAndSettle();
      }
      // Verify causing failure in the test
      verify(
        () => TestModularNavigation.mockedNavigator.pushNamed(AgileSmellsResults.route, arguments: any, forRoot: false),
      ).called(1); 
    });

我模拟的 ModularNavigate class

class ModularNavigateMock extends Mock implements IModularNavigator {}

class TestModularNavigation {
  static final mockedNavigator = ModularNavigateMock();

  static void setUp() {
    Modular.navigatorDelegate = mockedNavigator;
    when(() => mockedNavigator.pushNamed(AgileSmellsResults.route, arguments: any)).thenAnswer((_) async => const AgileSmellsResults());
  }
}

谢谢你帮助我

通过阅读解决 Mocktail FAQ and documentation on pub.dev

问题是 pushNamed 方法的存根错误。我将 arguments: any 更改为 arguments: any(named: 'arguments'),以便在我的 mockedNavigatorverify

中正确存根

我的固定mockedNavigator

class ModularNavigateMock extends Mock implements IModularNavigator {}

class TestModularNavigation {
  static final mockedNavigator = ModularNavigateMock();

  static void setUp() {
    Modular.navigatorDelegate = mockedNavigator;
    when(() => mockedNavigator.pushNamed(AgileSmellsResults.route, arguments: any(named: 'arguments'))).thenAnswer((_) async => const AgileSmellsResults(validatedSmellCards: []));
  }
}